0

我在胡闹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

那么,为什么我必须提供轴标签来抑制我在使用时获得的所有数字do.call

4

2 回答 2

3

首先,您需要了解这plot是一个S3 泛型,它根据第一个参数调用方法。如果执行plot(y ~ x)此方法plot.formula,则从公式中推断出轴标签。如果你这样做plot(x, y)(注意 x 和 y 的不同顺序),方法是plot.default,并且轴标签是从作为参数传递的符号中推断出来的。

现在,如果你这样做a <- 1:2; y <- 3:4; plot(x = a, y = b),标签是ab。但是,如果您使用do.call魔法,则会do.call(plot, list(x = a, y = b)扩展为plot(x = 1:2, y = 3:4),因此标签是1:2and 3:4。我建议使用带有data参数的公式方法,即,例如:

do.call(what = "plot", args = list(formula = Sepal.Length ~ Sepal.Width,
                                   data = I))
于 2016-11-21T13:13:45.077 回答
1

您所看到的是 R 在无法从参数中获取任何其他命名信息时放在轴标签上的内容。如果你这样做:

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

那么绘图将不得不使用向量值作为轴标签。

使用时do.call,列表参数中的名称与被调用函数的参数名称匹配。所以轴标签没有名字,只有值。那时,数据来自的事实I$Sepal.width早已不复存在,它只是一个值向量。

于 2016-11-21T13:11:17.727 回答