Home > Software design >  Super script within axis label using ggplot2 or ggpubr
Super script within axis label using ggplot2 or ggpubr

Time:09-22

I have been trying to raise the 2 in m^2 and get mu to look like the special character, but cant seem to figure it out. I've tried this and many others including the unicode, adding quotes isolating whats not supposed to be raised. Any help would be great. Thank you!

Here is my code:

ggboxplot(Photo_Mass_2021, x = "Species", y = "Pn_N", title= "Net Photosynthesis by Species", ylab= expression(paste("Net Photosynthesis ((mumol/m^2*/s)/ mass (g))")), color = "Species", add = "jitter", legend = "none")  
  geom_hline(yintercept = mean(Photo_Mass_2021$Pn_N), linetype = 2) 
  stat_compare_means(method = "anova", label.y = 170)  stat_compare_means(label = "p.signif", method = "t.test",ref.group = ".all.")

CodePudding user response:

When using expression() for labels check help(plotmath) and example(plotmath) for the syntax. It takes time to get used to this syntax, but two key things to remember are that * can be used to join things without inserting space and ~ adds a space (or rather a narrow space). You can protect parts of the text with quotation marks so that it is not interpreted as an expression. Subcripts are indicated by [ ] and superscripts by ^. I changed your label following the rule that quantities and units should not be mixed. (Some people disagree about this.) My answer uses the minimum of code necessary to answer your question. (This is also the recommended approach for questions.)

library(ggplot2)
ggplot()  
labs(y = expression("Net Photosynthesis per mass "*(mu*mol~m^{-2}~s^{-1}~g^{-1})))

Created on 2021-09-18 by the reprex package (v2.0.1)

  • Related