Skip to content

Multiple expressions, when using substitute()

5 messages · John Maindonald, Marc Schwartz, Spencer Graves

#
expression() accepts multiple expressions as arguments, thus:

plot(1:2, 1:2)
legend("topleft",
               expression(y == a * x^b,
                                    "where "* paste(y=="wood; ",  
x=="dbh")))

Is there a way to do this when values are to be substituted
for a and b? i.e., the first element of the legend argument
to legend() becomes, effectively:
   substitute(y == a * x^b, list(a = B[1], b=B[2]))

John Maindonald             email: john.maindonald at anu.edu.au
phone : +61 2 (6125)3473    fax  : +61 2(6125)5549
Centre for Bioinformation Science, Room 1194,
John Dedman Mathematical Sciences Building (Building 27)
Australian National University, Canberra ACT 0200.
#
On Sat, 2005-10-01 at 20:32 +1000, John Maindonald wrote:
John,

Try this:

a <- 5
b <- 3

L <- list(bquote(y == .(a) * x^.(b)),
          "where y = wood; x = dbh")

plot(1:2, 1:2)

legend(legend = do.call("expression", L),
       "topleft")


Note the creation of the list 'L', which uses bquote() and then
the .(Var) construct, where 'Var' are your variables to be replaced.
Then in the legend() call, the use of do.call() to apply expression() to
the elements of list 'L'.

HTH,

Marc Schwartz
9 days later
#
Have you received a reply to this post?  I couldn't find one, and I 
couldn't find a solution, even though one must exist.  I can get the 
substitute to work in "main" but not "legend":

B <- 2:3
eB <- substitute(y==a*x^b, list(a=B[1], b=B[2]))
plot(1:2, 1:2, main=eB)

	  You should be able to construct it using "mtext", but I couldn't get 
the desired result using legend.

	  hope this helps.
	  spencer graves
John Maindonald wrote:

            

  
    
#
Yes, I did get a very helpful reply from Marc Schwartz.  I have
had substitute() working in legend(), when the legend argument
has length one.  The challenge was to find some way to do the
equivalent of substitute() when several expressions appear in
parallel, as may be required for legend().

The trick is to use bquote() to do the substitution.  The resulting
quoted expression (of mode "call") can then be an element in a
list, along with other quoted (or bquoted) expressions.   The
list elements, when passed to expression() via the args
argument of do.call(), become unquoted expressions.

Note that bquote() uses a syntax for the substitution of variables
that is different from that used by substitute().  It would be useful
to include some such example as below on the help page for
bquote():


library(DAAG)
Acmena <- subset(rainforest, species="Acmena")
plot(wood~dbh, data=Acmena)
Acmena.lm <- lm(log(wood) ~ log(dbh), data=Acmena)
b <- round(coef(Acmena.lm), 3)
arg1 <- bquote(italic(y) == .(A) * italic(x)^.(B),
                    list(A=b[1], B=b[2]))
arg2 <- quote("where " * italic(y) * "=wood; " *
                           italic(x) * "=dbh")
legend("topleft", legend=do.call("expression", c(arg1, arg2)),
                bty="n")

John Maindonald.
On 11 Oct 2005, at 11:41 AM, Spencer Graves wrote:

            
John Maindonald             email: john.maindonald at anu.edu.au
phone : +61 2 (6125)3473    fax  : +61 2(6125)5549
Centre for Bioinformation Science, Room 1194,
John Dedman Mathematical Sciences Building (Building 27)
Australian National University, Canberra ACT 0200.
#
Thanks.  spencer graves
John Maindonald wrote: