[R] difficulty with R expressions in text/legend

Murat Tasan mmuurr at gmail.com
Sun Jul 11 05:37:10 CEST 2010


ok, here's a self-contained example.

topleft legend will plot 1 example of exactly what i want, but only 1
line.
bottomleft legend will plot what i had hoped would work... as it's the
logical extension of the topleft example to two cases.

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 = c(bquote(paste("foo bar ", R^2 == .
(2^2))), bquote(paste("bar foo ", R^2 == .(3^2)))), fill = c("red",
"blue"))

note that you cannot simply add the prefix string as is to bquote (as
suggested), as bquote expects an R expression.



On Jul 10, 11:22 pm, David Winsemius <dwinsem... at comcast.net> 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.
>
>
>
>
>
>
>
> > --
> > 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
>
> ______________________________________________
> 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