[R] lost in plotmath expressions

S Ellison S.Ellison at LGCGroup.com
Fri Jan 30 14:29:17 CET 2015

> nams<-c("P2O5 [%]", "D50 [mum]")
These are strings; plotmath is invoked on expressions.
Also, in an expression, [] has special meaning (it's treated as a subscripted index), so a space followed by '[' causes an error and even if that didn't happen, [] would not give units in square brackets.

However, there is (perhaps surprisingly) nothing wrong with horrible constructions like
enams<-c(expression(P[2]*O[5]*" [%]"), expression("D50 ["*mu*"]"))

for (i in 1:2) plot(1, 1, xlab=nams[i])

# As far as plotmath is concerned, '*' is represented graphically as implicit multiplication, 
# and since it's just graphical, plotmath doesn’t care that some of the things being multiplied 
# are actually strings.

The plotmath page also has a lot of examples of programmatic substitution in plotmath expressions; they just have to be followed very carefully. 
And ?bquote is also useful for substitution in plotmath...

S Ellison

This email and any attachments are confidential. Any use, copying or
disclosure other than by the intended recipient is unauthorised. If 
you have received this message in error, please notify the sender 
immediately via +44(0)20 8943 7000 or notify postmaster at lgcgroup.com 
and delete this message and any copies from your computer and network. 
LGC Limited. Registered in England 2991879. 
Registered office: Queens Road, Teddington, Middlesex, TW11 0LY, UK

More information about the R-help mailing list