Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use superscript with ggplot2

Tags:

r

ggplot2

How to print angstrom square in x axis? I tried as follows.

labs(x = "x axis" (Å^2)", y = "y axis")
like image 797
sara Avatar asked Jun 15 '16 03:06

sara


People also ask

How do you use superscript in R?

Superscript is “started” by the caret ^ character. Anything after ^ is superscript. The superscript continues until you use a * or ~ character. If you want more text as superscript, then enclose it in quotes.

How do you subscript in R?

Subscripts and Superscripts To indicate a subscript, use the underscore _ character. To indicate a superscript, use a single caret character ^ . Note: this can be confusing, because the R Markdown language delimits superscripts with two carets.


3 Answers

We can use bquote

library(ggplot2) ggplot(mtcars, aes(hp, mpg)) +         geom_point() +        labs(x = bquote('x axis'~(Å^2)), y = "y axis") +        #or        #labs(x = bquote('x axis'~(ring(A)^2)), y = "y axis")         theme_bw() 

enter image description here

like image 101
akrun Avatar answered Sep 28 '22 07:09

akrun


You should use expression, preferable combined with paste, as follow:

ggplot(mtcars, aes(hp, mpg)) + geom_point() + labs(x = expression(paste("x axis ", ring(A)^2)), y = "y axis")

enter image description here

like image 24
zyurnaidi Avatar answered Sep 28 '22 08:09

zyurnaidi


You could just simply use:

ggplot(mtcars, aes(hp, mpg)) +  geom_point() +  labs(x = x~axis~ring(A)^2, y = "y axis")
like image 20
jatalah Avatar answered Sep 28 '22 09:09

jatalah