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.