Skip to content
Prev 22477 / 398502 Next

Constructing titles from list of expressions

On Wed, 7 Aug 2002, Winfried Theis wrote:

            
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
[[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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._