[R] problem with `legend' + `substitute'
Uwe Ligges
ligges at statistik.uni-dortmund.de
Tue Nov 12 19:06:10 CET 2002
Timur Elzhov wrote:
>
> Hello, R experts!
>
> I'd like to add the somewhat complicated expression as "legend"
> to the plot:
>
> x <- 1
> T <- 10
>
> plot(x)
> legend ( 1.0 ,1.0
> ,substitute ( paste ( Sr["2"] ,GaMnO["5.541"]
> ,", T = " ,T ,"K"
> )
> ,list (T = T)
> )
> ,bg = "white"
> ,xjust = 0.5
> ,yjust = 0.5
> )
>
> So, I get plot with not one, but *six* instances of the desired expression,
> one under another!
The structure of your S expression is the point:
temp <- substitute(paste(Sr["2"], GaMnO["5.541"],
", T = ", T, "K"), list (T = T))
length(temp) # ah! 6!
str(temp) # that's it! Try also:
temp[[1]]
temp[[2]] # ...
> Ok, trying to use `text' ..
>
> plot(x)
> text ( 1.0 ,1.0
> ,substitute ( paste ( Sr["2"] ,GaMnO["5.541"]
> ,", T = " ,T ,"K"
> )
> ,list (T = T)
> )
> ,adj = c(0.5, 0.5)
> )
>
> Well, `text' works properly! but why `legend' behaves so?
Because it plots one line per element of your S expression, which
consists of 6!
> (I'm running R v1.6.1 on Debian woody system for i386)
What you can do is:
x <- 1
Te <- 10
# substitute at first:
temp <- substitute(paste(Sr["2"] * GaMnO["5.541"], ",",
T == Te * K), list(Te = Te))
plot(x)
# and construct a single expression (length = 1) by using do.call():
legend(1, 1, do.call("expression", list(temp)),
bg = "white", xjust = 0.5, yjust = 0.5)
Uwe Ligges
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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