Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Superscript in axis labels in ggplot2 for ions

Tags:

r

ggplot2

I need to achieve superscript in an axis label within ggplot2, similiar to this question: Superscript and subscript axis labels in ggplot2

However, I will need to write something like this: Ca^2+. This seems not to work with this approach, even if I put the exponent in { } with the bquote command. I tried to read through help("plotmath") but couldn't find an example for my case. I tried escaping the + with \+ and ++ but was not successful.

Edit: I would like to not use any additional packages.

like image 475
stephanmg Avatar asked May 02 '26 07:05

stephanmg


2 Answers

Try to put the superscript as a literal, between quotes.

g <- ggplot(iris, aes(Species, Sepal.Length)) + geom_boxplot()
g + xlab(bquote('Superscript as a literal' ~~ Ca^'2+'))

ded an image.

like image 51
Rui Barradas Avatar answered May 03 '26 21:05

Rui Barradas


Using the example you provided, this works

library(ggplot2)
qplot(uptake, data = CO2) +
  xlab(bquote('Assimilation ('*mu~ 'mol' ~Ca^{"2+"} ~ m^-2~s^-1*')'))

enter image description here

like image 39
matushiq Avatar answered May 03 '26 21:05

matushiq