Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 951
dayne Avatar asked Aug 16 '13 19:08

dayne


3 Answers

You can use alist rather then list

p <- alist(xlab=expression(paste("Concentration (",mu,"M)")))
do.call(plot,c(y~x,p))
like image 184
jdharrison Avatar answered Nov 07 '22 21:11

jdharrison


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))
like image 34
Aaron left Stack Overflow Avatar answered Nov 07 '22 21:11

Aaron left Stack Overflow


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)
like image 7
Josh O'Brien Avatar answered Nov 07 '22 21:11

Josh O'Brien