1

I would like to display physical units in an R plot. In order to have a better typography, I use the expression function this way:

plot(rnorm(10),rnorm(10),main=expression(µg.L^-1))

Suppose now that the unit is not statically known, and is given by a variable [unit]:

unit = 'µg.L^-1'
plot(rnorm(10),rnorm(10),main=expression(unit))

This of course does not work because [unit] is not substituted by its value. Is there some means to achieve that anyway?

Edit: I should stress that the main difficulty here is that the unit to be displayed is sent as a string to my plot function. So the contents of unit should be interpreted as an expression at some point (that is transformed from a string to an expression object), and this is where the answer by texb comes handy. So please unmark this question as duplicate, since the use of parse is fundamental here and is not even mentionned in the post you suggest.

pveber
  • 48
  • 7

3 Answers3

2

How about:

unit = 'µg.L^-1'
plot(rnorm(10),rnorm(10),main=parse(text=unit))
texb
  • 547
  • 2
  • 13
2

The bquote function gives you flexibility in creating expressions while inserting values from variables. Here is one example:

unit <- as.name('mu')
plot(rnorm(10), main=bquote( .(unit)*.L^-1 ) )
Greg Snow
  • 48,497
  • 6
  • 83
  • 110
  • Sorry if it was unclear but you are missing the point: I don't know in advance what the unit is. It could be mg.L^-1 as well as V.S^2; I want a function that can render any such physical unit coming as a string. The subsitution you describe is useless for that purpose as the contents of `unit` is not interpreted (and thus not correctly rendered). – pveber Jul 23 '13 at 11:07
  • @pveber, I took the word "part" in your title to imply that you may eventually expand to where the units are part of a larger expression to be used as a title, `bquote` and functions referred to on its help page help with building an expression from different parts. – Greg Snow Jul 23 '13 at 16:03
2

I think both answers are helpful but would like to suggest a more complete use of the plotmath syntax. The answer you accepted at the moment doesn't really parse the Greek mu separately and Greg Snow's answer doesn't illustrate how expressions can be used as values (but it does show how to substitute within expressions). So this is another alternative that also shows using a plotmath cdot operator as the separating "dot" which I think better addresses your interest in typography.

plot(1,1, main=expression(mu*g %.% L^-1) )

It's also possible to create fully formed expression and save by name:

micgmperL = expression(mu*g %.% L^-1) 
plot(1,1, main=micgmperL)
IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • Thanks this is interesting, but as I commented to Greg, I don't think this answers my question: when doing the plot, I only have a string variable containing the physical unit. The string could contain either "mg.L^2" or "expression(mu*g %.% L^2)" but this contents needs to be interpreted, that is transformed from a string to an expression. Your suggestion is indeed useful in combination with parse, as in: unit = 'expression(paste("Concentration in ", mu*g %.% L^-1))' plot(rnorm(10),rnorm(10),main = eval(parse(text=unit))) – pveber Jul 23 '13 at 11:45