0

I'm fooling around with do.call.

I = iris
do.call(what = "plot", args = c(I$Sepal.Length ~ I$Sepal.Width))
# This seems fine

p = list(x = I$Sepal.Length, y = I$Sepal.Width)
do.call(what = "plot", args = p)
# This looks weird

p1 = list(x = I$Sepal.Length, y = I$Sepal.Width, xlab = "")
do.call(what = "plot", args = p1)
# A bit less weird


p2 = list(x = I$Sepal.Length, y = I$Sepal.Width, xlab = "", ylab = "")
do.call(what = "plot", args = p2)
# And this gives the same as the first do.call

So why is it, that I have to supply the axis labels to surpress all those numbers I get when using do.call ?

New_to_this
  • 135
  • 1
  • 1
  • 8

2 Answers2

3

First you need to understand that plot is an S3 generic that calls methods depending on the first parameter. If you do plot(y ~ x) this method is plot.formula and axis labels are inferred from the formula. If you do plot(x, y) (note the different order of x and y), the method is plot.default and axis labels are inferred from the symbols passed as parameters.

Now, if you do a <- 1:2; y <- 3:4; plot(x = a, y = b), the labels are a and b. However, if you use the do.call magic, do.call(plot, list(x = a, y = b) gets expanded to plot(x = 1:2, y = 3:4) and thus the labels are 1:2 and 3:4. I'd recommend using the formula method with the data parameter, i.e., for your example:

do.call(what = "plot", args = list(formula = Sepal.Length ~ Sepal.Width,
                                   data = I))
Community
  • 1
  • 1
Roland
  • 127,288
  • 10
  • 191
  • 288
1

What you are seeing is what R puts on the axis labels when it can't get any other naming info from the arguments. If you do:

plot(x=c(1,2,3,4,5,6,7,8),y=c(1,2,3,4,3,2,3,4))

then the plot will have to use the vector values as the axis labels.

When using do.call, the names in the list arguments are matched to the names of the arguments of the function called. So there's no names left for the axis labels, just the values. At that point the fact that the data came from I$Sepal.width is long gone, its just a vector of values.

Spacedman
  • 92,590
  • 12
  • 140
  • 224