expression() and related functions including
bquote are powerful tools for annotating figures with mathematical notation in R. This functionality is not obvious from their respective help files.
demo(plotmath) nicely shows the huge potential of
expression(), but does not help that much with getting the code need for many real cases.
I tend to get my expressions to work by trial and lots of errors (although having put this together, I now understand them at least temporarily). I’ve just searched through my code library and extracted and annotated some examples of
expression() being used. I hope someone finds it useful.
I’m going to use
title(), but the same expressions can be used with any of the functions (
legend(), etc) used for putting text on plots.
x11(width=4, height=5, point=14);par(mar=rep(0,4), cex.main=.8)
plot(1, type="n", axes=FALSE, ann=FALSE)
The simplest use of expression is take a character or string of characters and it will be added to the plot. If the string contains spaces, it must be enclosed in quotes (alternatively, the space can be replaced by a tilde
~, which probably gives better code – see comment from Gavin below).
This use of expression is entirely pointless, but is a useful starting point. Some strings have special meanings, for example infinity will draw the infinity symbol. If for some reason you want to have “infinity” written on your plot, it must be in quotes. Greek letters can be used by giving their name in lower-case or with the first letter capitalised to get the lower or upper case character respectively.
Subscript or superscript can be added to a string using ^ and  notation respectively.
If the string we want to have as sub- or superscript contains a space, the string must be in quotes. Braces can be used to force multiple elements to all be superscript.
Strings can be separated by mathematical operators.
To make more complicated expressions, build them up from separate parts by either using * or paste to join them together (if you want a multiplication symbol, use
%*%). The * notation gives nicer code.
title(line=-9, main=expression(Delta*"R yr"))
title(line=-10, main=expression(paste(Delta,"R yr")))
title(line=-11, main=expression(paste("Two Year Minimum ",O)))
title(line=-12, main=expression(paste("Coefficient ", beta)))
title(line=-13, main=expression(paste("TP ", mu,"g l"^-1)))
title(line=-15, main=expression(paste("Foram ", exp(H*minute[bc]))))
To start an
expression() with a superscript (or subscript), I use an empty string (you can also use
title(line=-16, main= expression(""^14*C*" years BP"))
#title(line=-16, main= expression(phantom()^14*C~years~BP))
So far so good. But sometimes, you want to use the value of an R-object in plot annotation.
For example, if we wanted to label a point with its x value, this will not work.
title(line=-17, main= expression(x==x))
Instead of using
expression(), we have to use
bquote(), with the object we want written out inside
title(line=-18, main= bquote(x==.(x)))
title(line=-19, main= bquote(x==.(x)~mu*g~l^-1))
Plot annotations with expression and bquote
If you understand these examples, you should be able to use the remainder of the functionality demonstrated by
demo(plotmath) and at