Bill Dunlap TIBCO Software Inc - Spotfire Division wdunlap tibco.com
> -----Original Message----- > From: r-help-boun...@r-project.org > [mailto:r-help-boun...@r-project.org] On Behalf Of William Dunlap > Sent: Thursday, August 27, 2009 11:18 AM > To: Martin Maechler; Kenneth Roy Cabrera Torres > Cc: RHelp > Subject: Re: [R] math symbol + value of a variable in legend. > > > > -----Original Message----- > > From: r-help-boun...@r-project.org > > [mailto:r-help-boun...@r-project.org] On Behalf Of Martin Maechler > > Sent: Thursday, August 27, 2009 1:30 AM > > To: Kenneth Roy Cabrera Torres > > Cc: RHelp > > Subject: Re: [R] math symbol + value of a variable in legend. > > > > >>>>> "KRCT" == Kenneth Roy Cabrera Torres <krcab...@une.net.co> > > >>>>> on Tue, 25 Aug 2009 17:26:04 -0500 writes: > > > > KRCT> Thank you very much for your help. > > > > KRCT> To the R gurus: It will be better at the future to > > simplify this > > KRCT> options. > > > > KRCT> They are too cumbersome!!! > > > > The ones David showed, yes, are too cumbersome. > > > > There's a variant, which is even a bit more elegant, > > but really a small (?) change in R's handling of "symbols" could > > make it even more elegant. > > I'll talk about that on the dedicated list, R-devel. > > > > Here's the slightly more elegant code (for current versions of R): > > > > plot(1:5,1:5,type="n") > > legend("topleft", legend= > > c(as.expression( bquote(mu == .(m1)) ), > > as.expression( bquote(mu == .(m2)) )), lty = 1:2) > > Another version that is more easily extendable to longer legends is > > legend("topright", lty=1:2, legend= > as.expression(lapply(c(m1,m2), function(m)bquote(mu==.(m))))) > > Another example is: > m <- c(1,exp(1),pi) > plot(m, pch=seq_along(m), xlab=expression(iota), > ylab=quote(e^c(0,1,log(pi)))) > legend("bottom", pch=seq_along(m), > legend=as.expression(lapply(seq_along(m),function(i)bquote(m[. > (i)]==.(m[i]))))) > > Note that xlab and ylab can be either expressions or calls > (or, in general, language objects) but legend must be an > expression for > this to work. > > If legend is a list of calls, as in the more direct > legend("bottomright", pch=seq_along(m), > legend=lapply(seq_along(m),function(i)bquote(m[.(i)]==.(m[i])))) > then it looks like its elements are coerced to character > strings (via deparse). > If legend's legend= argument would interpret lists > differently than it does, > treating elements which are expressions or calls as plotmath requests > and other types as things to convert to strings, then I think > the syntax > would be simpler. E.g., your example could be > legend("bottomleft", legend=list(bquote(mu==.(m1)), > bquote(mu=.(m2))), lty=1:2) > You would not have to throw in the extraneous as.expression calls nor > have to redefine the c function. To try this out, redefine as.graphicsAnnot to process lists specially, before the existing check for is.language(x)||!is.object(x): as.graphicsAnnot <- function (x) if (is.list(x)) { as.expression(lapply(x, function(xi) { if(is.expression(xi)) xi[[1]] else if (is.language(xi)) xi else as.character(xi) })) } else if (is.language(x) || !is.object(x)) { x } else { as.character(x) } > > Bill Dunlap > TIBCO Software Inc - Spotfire Division > wdunlap tibco.com > > > ## > > ## or with subscripts : > > ## > > legend("top", legend = > > c(as.expression( bquote(mu[1] == .(m1)) ), > > as.expression( bquote(mu[2] == .(m2)) )), lty = 1:2) > > ## > > ## or, if you really need to have the subscript as a > > *variable* as well: > > ## > > i1 <- 11; i2 <- 20 > > legend("topright", legend = > > c(as.expression( bquote(mu[.(i1)] == .(m1)) ), > > as.expression( bquote(mu[.(i2)] == .(m2)) )), lty = 1:2) > > > > > > Martin Maechler, ETH Zurich > > > > > > KRCT> El mar, 25-08-2009 a las 18:16 -0400, David > > Winsemius escribió: > > >> On Aug 25, 2009, at 5:51 PM, David Winsemius wrote: > > >> > > >> > > > >> > On Aug 25, 2009, at 4:30 PM, Kenneth Roy Cabrera > > Torres wrote: > > >> > > > >> >> Hi R users: > > >> >> > > >> >> I will like to have a legend with math symbols > and also with > > >> >> the value of a variable. > > >> >> > > >> >> But I cannot obtain both at the same time (symbol + > > value of a > > >> >> variable): > > >> >> > > >> >> Here is a reproducible example: > > >> >> > > >> >> m1<-5 > > >> >> m2<-12 > > >> > > > >> > I think I am violating a fortune but this "worked": > > >> > > > >> > plot(1:5,1:5,type="n") > > >> > legend > > >> > ("topleft",legend=c(eval(substitute( > > expression(paste(mu,"=",m1)), > > >> > list(m1=m1) )) , eval(substitute( > > expression(paste(mu,"=",m2)), > > >> > list(m2=m2) ) )), lty=1:2) > > >> > > > >> > And efforts at simplification were at least partly > > successful: > > >> > > > >> > legend("topleft",legend=c(eval(substitute( > > expression(mu == m1), > > >> > list(m1=m1) )) , > > >> > eval(substitute( > > expression(mu == m2), > > >> > list(m2=m2) ) )), > > >> > lty=1:2) > > >> > > >> And this adds subscripts to the mu's: > > >> > > >> plot(1:5,1:5,type="n"); > > >> legend("topleft", > > >> legend=c( eval(substitute( expression(mu[i] == m1), > > >> list(i=1, m1=m1) )) , > > >> eval(substitute( expression(mu[i] == m2), > > >> list(i=2, m2=m2) )) ), > > >> lty=1:2) > > >> > > >> > > >> > > > >> > > > >> >> plot(1:5,1:5,type="n") > > >> >> legend > > >> >> ("topleft > > >> >> ",legend > > >> >> = > > >> >> > > > c(paste(expression(mu),"=",m1),expression(paste(mu,"=",m2))),lty=1:2) > > >> >> > > >> >> Thank you for your help. > > >> >> > > >> >> Kenneth > > >> > -- > > >> > > >> David Winsemius, MD > > >> Heritage Laboratories > > >> West Hartford, CT > > >> > > > > KRCT> ______________________________________________ > > KRCT> R-help@r-project.org mailing list > > KRCT> https://stat.ethz.ch/mailman/listinfo/r-help > > KRCT> PLEASE do read the posting guide > > http://www.R-project.org/posting-guide.html > > KRCT> and provide commented, minimal, self-contained, > > reproducible code. > > > > ______________________________________________ > > R-help@r-project.org mailing list > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide > > http://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, reproducible code. > > > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.