[R] difficulty with R expressions in text/legend

Murat Tasan mmuurr at gmail.com
Sun Jul 11 06:32:10 CEST 2010


well, figured it out, apparently placing the bquote results in a
vector doesn't result in a vector of expressions, so you have to
coerce it like so:

dev.new()
plot.new()
plot.window(xlim = c(0, 1), ylim = c(0, 1))
legend("topleft", legend = bquote(paste("foo bar ", R^2 == .(2^2))),
fill = "red")
legend("bottomleft", legend = as.expression(c(bquote(paste("foo bar ",
R^2 == .(2^2))), bquote(paste("bar foo ", R^2 == .(3^2))))), fill =
c("red", "blue"))

both the top and bottom legends now correctly have the prefix string
and the typographically-correct expression with an evaluated term.


On Jul 10, 11:54 pm, David Winsemius <dwinsem... at comcast.net> wrote:
> On Jul 10, 2010, at 11:22 PM, David Winsemius wrote:
>
>
>
>
>
>
>
> > On Jul 10, 2010, at 10:45 PM, David Winsemius wrote:
>
> >> On Jul 10, 2010, at 8:58 PM, Murat Tasan wrote:
>
> >>> hi, i'm trying to prepend some plain (i.e. unevalutated) text to a
> >>> typographically evaluated R expression in a legend(...) call.
>
> >>> here's a working legend(...) call that is close to what i'd like
> >>> (where x and y are returned from an lm(...) call):
>
> >>> legend("topleft", legend = c(bquote(R^2 == .(summary(x)$r.squared)),
> >>> bquote(R^2 == .(summary(y)$r.squared))), fill = c("blue", "red"))
>
> >>> this evaluates the R^2 values for my x and y objects, then displays
> >>> them in the plot legend with colored boxes adjacent to them.
>
> >>> now the hard part (for me, at least :-/), i'd like to add some text
> >>> (e.g. "data set 1" and "data set 2") to each line of the legend,  
> >>> right
> >>> before the typographically-displayed R^2 == some_value text.
>
> >> Would be nice (and would follow the guidance of the Posting Guide  
> >> if you were to offer a method for construction something like "x"  
> >> and "y" as well as stating explicitly what plotting function you  
> >> were using. My guess (untested) is that you can get what you want  
> >> by simply sticking in those strings (unquoted) into your "working"  
> >> bquote argument.
>
> >> legend("topleft", legend = c(bquote(data set 1 R^2 == .(summary(x)
> >> $r.squared)),
> >> bquote(data set 2 R^2 == .(summary(y)$r.squared))), fill =  
> >> c("blue", "red"))
>
> > Nah. didn't work with an example constructed from the lm and summary  
> > page examples.
>
> > e1 <- as.expression(paste("data set 1 R^2 == ",  
> > summary(lm.D90)$r.squared))
> > e2 <- as.expression(paste("data set 2 R^2 == ",  
> > summary(lm.D90)$r.squared ))
> > plot(1,1)
> > legend("topleft", legend = c( e1,e2 ), fill = c("blue", "red"))
>
> > This was mostly successful, although it did not get the ^2  
> > interpreted as a superscript. You may want to wrap an sprintf or a  
> > format call around that r.squared.
>
> I searched for better methods and found a thread where I had  
> demonstrated some success, but the code was rather convolute. Others  
> had more compact solutions. Try this modification of one offered by  
> Grothendieck:
>
> x=c(summary(lm.D90)$r.squared, summary(lm.D90)$r.squared-1)
> plot(0)
> ix <- as.double(1:3)
> legend("top",  as.expression(lapply(ix, function(i) bquote(data~set~.
> (i)~R^2==.(x[i])))))
>
> --
> David,
>
>
>
>
>
>
>
> >> --
> >> David.
>
> >>> basically, a legend that looks like so (with the expression part
> >>> evaluated and typed out correctly):
> >>> data set 1 R^2 == some_value
> >>> data set 2 R^2 == some_other_value
>
> >>> i've tried a seemingly countless number of paste/substitute/
> >>> expression
> >>> combinations, but cannot seem to get what i'd like.
> >>> anyone know of a nice call to create these two strings to display on
> >>> my plot?
>
> >>> thanks for any help!
>
> >>> -murat
>
> >>> ______________________________________________
> >>> R-h... at r-project.org mailing list
> >>>https://stat.ethz.ch/mailman/listinfo/r-help
> >>> PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html
> >>> and provide commented, minimal, self-contained, reproducible code.
>
> >> David Winsemius, MD
> >> West Hartford, CT
>
> >> ______________________________________________
> >> R-h... at r-project.org mailing list
> >>https://stat.ethz.ch/mailman/listinfo/r-help
> >> PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html
> >> and provide commented, minimal, self-contained, reproducible code.
>
> > David Winsemius, MD
> > West Hartford, CT
>
> David Winsemius, MD
> West Hartford, CT
>
> ______________________________________________
> R-h... at r-project.org mailing listhttps://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list