Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Squared" superscript in ggplot2 yaxis label in R

I am plotting a multiple line graph with ggplot2 and am having trouble labeling the y-axis with mile per second squared. I have tried the command:

ggplot(data.frame, aes(id)) + labs(y = "Acceleration in m/s^2")

and( after searching Stack Overflow):

ggplot(data.frame, aes(id)) + labs(y = expression ("Acceleration in m/s[2]")

In both cases, the label does not create superscript, instead looking exactly like "Acceleration m/s^2". How should this superscript command for a ggplot label be constructed?

like image 519
coollikeabreeze Avatar asked Apr 28 '16 16:04

coollikeabreeze


People also ask

How do you add a 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 change the axis titles in R studio?

Changing axis labels To alter the labels on the axis, add the code +labs(y= "y axis name", x = "x axis name") to your line of basic ggplot code. Note: You can also use +labs(title = "Title") which is equivalent to ggtitle .

Which arguments can be used to add labels in Ggplot?

Creating non-overlapping labels with ggrepel You can also use the same nudge_y arguments to create more space between the labels and the points.


1 Answers

Try this instead:

 + labs(y = expression ("Acceleration in"~m/s^2))
like image 165
joran Avatar answered Sep 28 '22 03:09

joran