ggplot2与其他功能(R)之间的加号

时间:2016-07-03 04:23:15

标签: r ggplot2 operators

我试图得到这个例子:

  

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

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

ggplot2是否重载“plus”运算符?总结这些2的结果是什么,它分配给了什么?它是特定于R的功能,还是特定于ggplot2的?这是管道吗?

1 个答案:

答案 0 :(得分:4)

@Richard Scriven在注释中引用的函数定义在plot-construction.r中定义,这可能会使其更清晰。您需要查看源代码以确切了解这两个(未导出的)函数的作用(调用的LHS是theme还是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个对象)的对象,ggplot2被重载。

我认为&#39; pipe&#39; (@ alistaire的评论)是一个误导性的比喻;这非常符合标准Ops组通用的风格。