Superscript in R

user3170629 picture user3170629 · Jan 7, 2014 · Viewed 11.6k times · Source

How do I superscript the units this axis title. The "" represent the parts that I need to be superscripted: Photosynthetically available radiation (µE m"-2"d"-1").

I have used the formula and having no luck so far:

plot(PAR~SST,data=brazilw, pch=15,col="red", main ="Fig. 1. Relationship between photosynthically available radiation\n and sea surface temperature",
ylab=expression("Photosynthetically available radiation (µE m"^-2~d^-1))

Answer

Gavin Simpson picture Gavin Simpson · Jan 7, 2014

Whilst I don't see a real problem in this particular instance, I can see if being an issue with other labels. I tend to group the elements of the super/subscript in braces { }, LaTeX stylee.

Here is an example:

plot(1:10,
     ylab = expression("Photosynthetically available radiation" ~ 
                         (µE ~ m^{-2} ~ d^{-1})
                       )
     )

There are gotchas with your version and the one above; the bit in the braces also needs to be a valid expression, hence

plot(1:10,
     ylab = expression("Photosynthetically available radiation" ~ (µE ~ 
                         m^{2-} ~ d^{1-})))

fails with an error. (I sometimes need those forms for writing down formulae for ions for example). To solve this issue you really do need the braces { } and you need something to come after the - operator. This latter feature is handled by phantom(), which leaves space in the expression for its argument, but as we won;t specify one, it is just a placeholder for nothing that can go on the right hand side of -:

plot(1:10,
     ylab = expression("Photosynthetically available radiation" ~ (µE ~ 
                         m^{2-phantom()} ~ d^{1-phantom()})))

phantom() is also quite useful for placing a sub/superscript before a string, like you would with isotope notation

plot(1:10, ylab = expression(phantom()^{210} * Pb))