[R] plotmath: how to create a vector of expressions?

Peter Ehlers ehlers at ucalgary.ca
Sat Feb 26 17:55:30 CET 2011


On 2011-02-25 15:57, Marius Hofert wrote:
> Dear expeRts,
>
> I would like to use LaTeX-like symbols in keys via plotmath. Below is a minimal
> example. I get:
> Error in fun(key = list(x = 0.5, y = 0.8, text = list(list(expression(paste("(",  :
>    first component of text must be vector of labels
> What's wrong? How can I create the required vector (of expressions)?
>
> Cheers,
>
> Marius
>
> library(lattice)
> val<- matrix(1, nrow = 10, ncol = 3)
> nplot<- 10
>
> labs<- sapply(1:3, function(i) substitute(expression(paste("(",i,"), ",l==len,"mm",sep="")), list(len=2*i)))
> myplot<- xyplot(0~0,
>                   panel=function(...){
>                       for(i in 1:3){
>                           panel.xyplot(1,val[,i],type="l")
>                           panel.text(2,val[nplot,i],label=paste("(",i,")",sep=""))
>                       }
>                   },
>                   key = list(x=0.5,y=0.8,text=list(labs),
>                   align=TRUE,transparent=TRUE))
> myplot

Try this:

   labs <- vector("expression", 3)
   for(i in 1:3) labs[i] <-
      substitute(expression(
                   group("(", k, ")") * "," ~~ l == len ~ italic("mm")
                ),
                list(len = 2*i, k=i)
      )[2]

The idea is to create an expression vector and then
fill its components appropriately. We need the second
component of the substitute call.
(I put in the 'italic' just for fun.)

Maybe there's a better way, but I don't know it.

Peter Ehlers



More information about the R-help mailing list