我在和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时得到的所有数字呢?
发布于 2016-11-21 13:11:17
当R不能从参数中获得任何其他命名信息时,你看到的是R在轴标签上放置的内容。如果你这样做了:
plot(x=c(1,2,3,4,5,6,7,8),y=c(1,2,3,4,3,2,3,4))然后,绘图将必须使用向量值作为轴标签。
使用do.call时,list参数中的名称与调用的函数的参数名称相匹配。因此,轴标签没有剩下的名称,只有值。在这一点上,数据来自I$Sepal.width的事实早已不复存在,它只是一个值的向量。
发布于 2016-11-21 13:13:45
首先,您需要理解plot是一个根据第一个参数调用方法的S3 generic。如果执行plot(y ~ x),则此方法为plot.formula,并从公式中推断出轴标签。如果您执行plot(x, y) (注意x和y的不同顺序),方法是plot.default,并从作为参数传递的符号中推断出轴标签。
现在,如果您执行a <- 1:2; y <- 3:4; plot(x = a, y = b),标签是a和b。但是,如果您使用do.call魔术,do.call(plot, list(x = a, y = b)会被扩展到plot(x = 1:2, y = 3:4),因此标签是1:2和3:4。我建议使用带有data参数的公式方法,例如,对于您的示例:
do.call(what = "plot", args = list(formula = Sepal.Length ~ Sepal.Width,
data = I))https://stackoverflow.com/questions/40720786
复制相似问题