[R] Constructing titles from list of expressions
Thomas Lumley
tlumley at u.washington.edu
Wed Aug 7 18:06:19 CEST 2002
On Wed, 7 Aug 2002, Winfried Theis wrote:
> Hello!
>
> I have the following problem:
> I have a function to construct three surfaceplots with a marker for an optimum,
> each of the plots has as title paste("Estimated ",pred.var.lab," for
> ",var.lab[1]," vs. ",var.lab[2],sep="") with different var.lab[1,2] each time.
> My problem is now that I need to allow for plotmath expressions in the
> variables pred.var.lab and var.lab[1,2] and get these evaluated correctly.
>
> I tested the usage of parse(), paste(), substitute() in different combinations,
> but could not find a version, which worked. The problem seems to be the order
> of evaluation...
Let's work on this from the inside out.
Suppose you want the label "Estimated alpha for beta vs gamma^3" but with
alpha, beta, and gamma^3 replaced by the appropriate expressions.
plot(1:10,main=quote("Estimated "*alpha*" for "*beta*" vs."*gamma^3))
Now, suppose you might want to change the expressions. You need to get
them into the formula. This is what substitute() is good for.
substitute("Estimated "*a*" for "*b*" vs."*c,
list(a=quote(alpha),b=quote(beta),c=quote(gamma^3)))
gives
"Estimated " * alpha * " for " * beta * " vs." * gamma^3
so the plot command can be
plot(1:10,main=substitute("Estimated "*a*" for "*b*" vs."*c,
list(a=quote(alpha),b=quote(beta),c=quote(gamma^3))))
Now in fact you have the expressions a, b and c in variables
expr1<-quote(alpha)
expr2<-quote(beta)
expr3<-quote(gamma^3)
plot(1:10,main=substitute("Estimated "*a*" for "*b*" vs."*c,
list(a=expr1,b=expr2,c=expr3)))
In your example you wanted b and c to be elements of a vector. They
actually have to be elements of a list
> c(quote(beta),quote(gamma^3))
[[1]]
beta
[[2]]
gamma^3
So
expr1<-quote(alpha)
expr2<-c( quote(beta), quote(gamma^3))
plot(1:10,main=substitute("Estimated "*a*" for "*b*" vs."*c,
list(a=expr1,b=expr2[[1]],c=expr2[[2]])))
Finally, you could even make the expressions from text strings if you want
to
str1<-"alpha"
str2<-c("beta","gamma^3")
expr1<-parse(text=str1)[[1]]
expr2<-parse(text=str2)
plot(1:10,main=substitute("Estimated "*a*" for "*b*" vs."*c,
list(a=expr1,b=expr2[[1]],c=expr2[[2]])))
Finally, what's the difference between using quote() as I did and
expression() as you did? Not a lot. quote() produces a call and
expression() produces an expression (roughly a list of calls). In this
context it doesn't matter. The documentation for plotmath() talks about
expressions, but most of the examples use substitute() in a way that
produces a call.
-thomas
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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