How to combine do.call() plot() and expression()
Asked Answered
B

3

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

Bassoon answered 16/8, 2013 at 19:41 Comment(3)
+1 for your interesting observation about the behavior of do.call(plot,c(0,p)).Peonir
possible duplicate of plot() and do.call(): How to pass expressions to plot title when '...' is used otherwise?Hetaera
@Hetaera I did read that question before posting.Bassoon
G
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))
Gasser answered 16/8, 2013 at 19:46 Comment(8)
Thanks! Do you know why do.call(plot,c(0,p)) does not give an error?Bassoon
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.Gasser
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.Notochord
Nice catch @Dwin, sep removed.Gasser
@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.Bassoon
What "default space"? Spaces do not get interpreted as anything in R expressions.Notochord
Look at the documentation on ?plotmath. There is no sep argument to that function. Try putting in sep="+" and see what happens.Notochord
@Bassoon 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.Peonir
A
14

You can use alist rather then list

p <- alist(xlab=expression(paste("Concentration (",mu,"M)")))
do.call(plot,c(y~x,p))
Abrego answered 16/8, 2013 at 19:45 Comment(0)
G
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))
Gasser answered 16/8, 2013 at 19:46 Comment(8)
Thanks! Do you know why do.call(plot,c(0,p)) does not give an error?Bassoon
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.Gasser
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.Notochord
Nice catch @Dwin, sep removed.Gasser
@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.Bassoon
What "default space"? Spaces do not get interpreted as anything in R expressions.Notochord
Look at the documentation on ?plotmath. There is no sep argument to that function. Try putting in sep="+" and see what happens.Notochord
@Bassoon 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.Peonir
S
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)
Sarah answered 16/8, 2013 at 20:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.