12

I am getting an error when I try and combine using expression with do.call and plot.

 x <- 1:10
 y <- x^1.5

I can get the plot I want by using only the plot function:

plot(y~x,xlab=expression(paste("Concentration (",mu,"M)")))

However, I would like to implement my plot using do.call. I have a really long list of parameters stored as a list, p. However, when I try and pass the list to do.call I get the following error:

p <- list(xlab=expression(paste("Concentration (",mu,"M)")))
do.call(plot,c(y~x,p))
Error in paste("Concentration (", mu, "M)") : 
  object 'mu' not found

I also tried defining the formula explicitly in the args passed to do.call. ie. do.call(plot,c(formula=y~x,p)). I do not understand why I am getting the error - especially because the following does not give an error:

do.call(plot,c(0,p))

(and gives the desired mu character in the xaxis).

dayne
  • 7,504
  • 6
  • 38
  • 56
  • +1 for your interesting observation about the behavior of `do.call(plot,c(0,p))`. – Josh O'Brien Aug 16 '13 at 21:06
  • possible duplicate of [plot() and do.call(): How to pass expressions to plot title when '...' is used otherwise?](http://stackoverflow.com/questions/13982856/plot-and-do-call-how-to-pass-expressions-to-plot-title-when-is-used-o) – Metrics Aug 16 '13 at 22:39
  • @Metrics I did read that question before posting. – dayne Aug 17 '13 at 03:26

3 Answers3

14

You can use alist rather then list

p <- alist(xlab=expression(paste("Concentration (",mu,"M)")))
do.call(plot,c(y~x,p))
jdharrison
  • 30,085
  • 4
  • 77
  • 89
12

do.call evaluates the parameters before running the function; try wrapping the expression in quote:

p <- list(xlab=quote(expression(paste("Concentration (",mu,"M)"))))
do.call("plot", c(y~x, p))
Aaron left Stack Overflow
  • 36,704
  • 7
  • 77
  • 142
  • 1
    Thanks! Do you know why `do.call(plot,c(0,p))` does not give an error? – dayne Aug 16 '13 at 19:49
  • 1
    Interesting find! Looks like my guess why it didn't work wasn't quite correct. One is using `graphics:::plot.default` and the other is using `graphics:::plot.formula`. I see that `graphics:::plot.formula` is doing some funky stuff with the `xlab` parameter using `enquote`; I'd have to look closer to understand why that causes it to fail. – Aaron left Stack Overflow Aug 16 '13 at 20:14
  • 2
    You should both stop using `sep` arguments in plotmath-`paste`. At best they are ignored, and at worst they get put at the end of the expression and not where you wanted them. – IRTFM Aug 16 '13 at 21:03
  • Nice catch @Dwin, `sep` removed. – Aaron left Stack Overflow Aug 16 '13 at 21:26
  • @DWin I also removed the `sep`. **BUT** I do not understand why it does not place the default space into the expression without the `sep` argument. – dayne Aug 16 '13 at 21:30
  • What "default space"? Spaces do not get interpreted as anything in R expressions. – IRTFM Aug 16 '13 at 21:54
  • Look at the documentation on `?plotmath`. There is no `sep` argument to that function. Try putting in sep="+" and see what happens. – IRTFM Aug 16 '13 at 22:06
  • 1
    @dayne Basically, as @DWin is pointing out, when it's inside of an `expression` object being interpreted by plotmath, `paste` does not refer at all to the familiar `base::paste`. Might be easier to grok this if you compare with something like `x[i]` which also has a very different meaning inside and outside of a plotmath call. – Josh O'Brien Aug 17 '13 at 00:05
7

Setting quote=TRUE also works. It in effect prevents do.call() from evaluating the elements of args before it passes them to the function given by what.

x <- 1:10
y <- x^1.5
p <- list(xlab=expression(paste("Concentration (",mu,"M)",sep="")))

do.call(what = "plot", args = c(y ~ x, p), quote = TRUE)
Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455