ggplot2 和其他函数之间的加号 (R) [英] Plus sign between ggplot2 and other function (R)
问题描述
我正在尝试获取此示例:
I'm trying to get this example:
ggplot(mpg, aes(displ, hwy)) + geom_point()
ggplot(mpg, aes(displ, hwy)) + geom_point()
有人能解释一下这两个函数之间发生了什么吗?
Can somebody explain me what's going on here between these 2 functions?
ggplot2 会重载plus"运算符吗?总结这两个的结果是什么,它分配给什么?它是特定于 R 的功能还是特定于 ggplot2 的功能?它是一种管道吗?
Does ggplot2 overload "plus" operator? What is the result of summarizing these 2, and what is it assigned to? Is it R-specific feature, or ggplot2-specific? Is it kind of pipe?
推荐答案
@Richard Scriven 在评论中引用的函数定义定义在 plot-construction.r
,这可能会使它更清楚.您需要查看源代码以确切了解这两个(未导出的)函数的作用(调用的 LHS 是 theme
还是 ggplot
对象)但是这些名字应该给你一个很好的主意.返回值是通过添加"e2
修改的e1
.
The function definition that @Richard Scriven refers to in comment is defined in plot-construction.r
, which might make it clearer. You'll need to go through the source to see exactly what those two (unexported) functions do (whether the LHS of the call is a theme
or a ggplot
object) but the names should give you a pretty good idea. The return value is e1
modified by "adding" 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
的对象(所有 ggplot2
对象),+
是重载的.
So, yes, +
is overloaded for objects inheriting class gg
(all ggplot2
objects).
我认为管道"(@alistaire 的评论)是一个误导性的比喻;这非常符合标准 Ops 组通用的风格.
I think 'pipe' (@alistaire's comment) is a misleading analogy; this is very much in the style of the standard Ops group generic.
这篇关于ggplot2 和其他函数之间的加号 (R)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!