0

How can I add a superscript to the y-axis label in R? I tried bquote, but it does not do anything.

ggplot(df, aes(Date)) + 
  geom_line(aes(y = OUT_SNOW_F, colour = "Flux")) +
  scale_color_manual(values = c("Orange")) +
  ylab("Snow Flux (W/m^2)") +
  labs(color = "Legend") +
  ggtitle("Title")
Ed_Gravy
  • 1,841
  • 2
  • 11
  • 34
  • 1
    pass an expression `ylab(expression(Flux~(W/m^2)))` – user20650 Nov 08 '21 at 10:18
  • 1
    Is it a superscript "2" you'd like to add? I find it easiest to use the unicode for the character - U+00B2. Look at this post if you want to see how to use unicode in ggplot - https://stackoverflow.com/questions/51039262/using-unicode-character-plus-minus-sign-inside-ggplot2 – Tech Commodities Nov 08 '21 at 10:19
  • @user20650, I get an error `non-numeric argument to binary operator`, which is fixed by `ylab(expression("Snow Flux"~(W/m^2)))` – Ed_Gravy Nov 08 '21 at 10:25
  • 1
    @Mandalorian; well you did edit the text ;) . As an alt. you can also join different words using the tilde operator, which is a way to add whitespace e.g. `ylab(expression(Snow~Flux~(W/m^2)))` – user20650 Nov 08 '21 at 13:40

0 Answers0