[R] Expressions in factor labels on postscript plots

Duncan Murdoch murdoch at stats.uwo.ca
Fri Jan 4 10:47:33 CET 2008


On 03/01/2008 6:29 PM, kyselad wrote:
> I'm trying to put Greek letters in factor labels on a boxplot. Using the
> expression() format works great in axis labels, but seems not to cooperate
> when applied to factor labels. More precisely, I'm assigning the labels from
> a factor variable using:
> 
> var.order <- ordered(var, levels = c("A", "B"), labels=c(C, D) )
> 
> Then I create a boxplot with postscript output. If I assign C and D as
> quoted text strings, they show up fine as regular text. When I try
> expression() for C or D, it chokes.
> 
> Any ideas here? As a potential complication, I need mixed Greek and
> non-Greek characters in the labels. Of course, that's easy enough if I can
> get expression() working for me.
> 
> Thanks in advance for any suggestions.

You need to use the names=expression() argument to boxplot.  There isn't 
an easy way to save the expressions as factor labels (which must be 
character values, so you'd need to use parse() to convert them to 
expressions in the boxplot call), but you can do something like this:

x <- rnorm(100)
var <- sample(c("A", "B"), 100, rep=TRUE)
var.order <- ordered(var, levels=c("A", "B"))
boxplot(x ~ var.order, names=expression("C", Delta))

If you really want to attach those expressions to var.order, you could 
define your own attribute and put them there, e.g.

attr(var.order, "namesexpr") <- expression("C", Delta)
boxplot(x ~ var.order, names=attr(var.order, "namesexpr"))

Be careful what attribute name you use; some of them (e.g. "names") are 
already used for other purposes.

Duncan Murdoch




More information about the R-help mailing list