Search code examples
rplotexpressiondo.call

How to combine do.call() plot() and expression()


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).


Solution

  • 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))