gpt4 book ai didi

r - ggplot2 和其他函数之间的加号 (R)

转载 作者:行者123 更新时间:2023-12-02 05:07:19 26 4
gpt4 key购买 nike

我正在尝试获取这个示例:

ggplot(mpg, aes(displ, hwy)) + geom_point()

有人可以解释一下这两个函数之间发生了什么吗?

ggplot2 是否重载“加”运算符?总结这2个结果是什么?它被分配给什么?它是 R 特定的功能,还是 ggplot2 特定的功能?它是一种管道吗?

最佳答案

@Richard Scriven 在注释中引用的函数定义定义于 plot-construction.r ,这可能会更清楚。您需要仔细查看源代码才能准确了解这两个(未导出的)函数的作用(调用的 LHS 是 theme 还是 ggplot 对象),但是这些名字应该会给你一个很好的主意。返回值是通过“添加”e2修改的e1

"+.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 对象)来说,+ 是重载的。

我认为“管道”(@alistaire 的评论)是一个误导性的类比;这非常符合标准运营组通用的风格。

关于r - ggplot2 和其他函数之间的加号 (R),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38166708/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com