我试图得到这个例子:
ggplot(mpg, aes(displ, hwy)) + geom_point()
有人可以解释一下这两个函数之间发生了什么吗?
ggplot2 是否重载“加”运算符?总结这2个结果是什么?它被分配给什么?它是 R 特定的功能,还是 ggplot2 特定的功能?它是一种管道吗?
@Richard Scriven 在注释中引用的函数定义定义于plot-construction.r https://github.com/hadley/ggplot2/blob/master/R/plot-construction.r,这可能会更清楚。您需要浏览源代码才能确切地了解这两个(未导出的)函数的作用(调用的 LHS 是否是theme
or a ggplot
对象),但名称应该会给你一个很好的主意。返回值为e1
通过“添加”修改e2
.
"+.gg" <- function(e1, e2) {
# Get the name of what was passed in as e2, and pass along so that it
# can be displayed in error messages
e2name <- deparse(substitute(e2))
if (is.theme(e1)) add_theme(e1, e2, e2name)
else if (is.ggplot(e1)) add_ggplot(e1, e2, e2name)
}
所以,是的,+
对于继承类的对象是重载的gg
(all ggplot2
对象)。
我认为“管道”(@alistaire 的评论)是一个误导性的类比;这非常符合标准运营组通用的风格。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)