[R] Displaying a variable as an expression into a plot label, but the variable has brackets

David Winsemius dwinsemius at comcast.net
Tue Oct 12 19:26:04 CEST 2010


On Oct 12, 2010, at 12:53 PM, Jared Blashka wrote:

> I'm attempting to insert variables as expressions into labels for my  
> plots,
> but I'm running into the issue that characters in my labels aren't  
> allowed
> in expressions, notably '[' and ']'. An input string of "[^3H]5-CT"  
> needs to
> be displayed as [3H]5-CT with the 3 as superscript. But brackets  
> aren't
> particularly liked in expressions. I know I can do something like
>
> mtext(expression(paste("[",phantom()^3,"H]5-CT (0.8 nM)")),3,2)
>
> but "[^3H]5-CT" is saved as a single variable. I'm not really sure  
> what to
> do other than splitting the string into sections that can and can't be
> evaluated as expressions and then pasting as necessary.

bquote to the rescue!

 > varname <- "[^3H]5-CT"
 > plot(1,1,xlab=expression(varname))  # nope
 > plot(1,1,xlab=bquote(.(varname)) )  # Yes
 > plot(1,1,xlab=bquote(.(varname)*"(0.8 nM)") )
        # Yes, note need for "*" or "~" as plotmath separator.


==

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list