[R] difficulty with R expressions in text/legend

David Winsemius dwinsemius at comcast.net
Sun Jul 11 04:45:03 CEST 2010


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"))

-- 
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-help at 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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list