[R] Multiple expressions, when using substitute()

Marc Schwartz MSchwartz at mn.rr.com
Sat Oct 1 17:10:23 CEST 2005


On Sat, 2005-10-01 at 20:32 +1000, John Maindonald wrote:
> 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,

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




More information about the R-help mailing list