[R] using the function "expression" with "paste" in a plot

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Sat Nov 16 18:35:07 CET 2002

"Ahmad Abu Hammour" <hammour at msn.com> writes:

> Hi,
> I noticed that paste and expression in the following manner does not produce
> the desired result.
> vnames=c("pi","y","delta")
> vx=vn=3
> m=16
> # mydata any matrix that takes for the following form
> mydata=array(somedata,c(m,vn,vx))
> # producing plots
> for (j in 1:vx){
> for (i in 1:vn)
> {
> plot(mydata, type="l", main=expression(paste("Effect of",
> vnames[j],"on",vnames[i])), xlab="Lags (quarters)", ylab="Response")
> }
> }
> The title appears as "Effect of vnamesj on vnamesi". Without the function
> "expression" title for j=1 and i=2 appears as "Effect of pi on y". The
> desired outcome would be return the word "pi" rather than a Greek letter.
> Is there any simple way to overcome this situation.

Yes. Use substitute()

main=substitute(paste("Effect of", foo, "on", bar),
     list(foo=vnames[j], bar=vnames[i]))

If you want the Greek letters rather than the names, use

     list(foo=as.name(vnames[j]), bar=as.name(vnames[i])))

or perhaps something like

vnames <- expression(pi, y, delta)
main=substitute(paste("Effect of", foo, "on", bar),
     list(foo=vnames[[j]], bar=vnames[[i]]))
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list