ggplot2 plus运算符如何定义? [英] How is ggplot2 plus operator defined?

查看:113
本文介绍了ggplot2 plus运算符如何定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

+通常在提供数字以外的内容时抛出non-numeric argument to binary operator.但是它可以与ggplot2一起使用,将绘图粘贴在一起.同时,它在ggplot2上下文之外保留了其通常的功能(例如,作为算术运算符或公式运算符),因此其ggplot2版本与这两个版本均不冲突.

The + usually throws non-numeric argument to binary operator when provided with something other than a number. But it works with ggplot2, pasting the plot together. At the same time, it retains its usual function outside of the context of ggplot2 (e.g. as an arithmetic or formula operator), so its ggplot2 version is not in conflict with either of these.

我希望了解如何使+以这种方式运行.浏览ggplot2 github存储库时,我发现了+.gg%+%的函数定义,但对我而言并没有使事情变得更清楚.

I wish to understand how to make the + behave this way. Browsing the ggplot2 github repo, I have found function definitions for +.gg and %+% but it did not make things clearer for me.

我很高兴能找到执行此操作的ggplot2软件包中的代码的指针,或者对如何完成此操作的一般性解释.

I would be happy with a pointer to the code in ggplot2 package that does this, or a generalized explanation of how this is done.

推荐答案

如果您剖析+.gg,我们将:

> ggplot2:::`+.gg`
function (e1, e2) 
{
  e2name <- deparse(substitute(e2))
  if (is.theme(e1)) 
    add_theme(e1, e2, e2name)
  else if (is.ggplot(e1)) 
    add_ggplot(e1, e2, e2name)
}

此外,add_theme,您感兴趣的是add_ggplot,可以使用ggplot2:::add_ggplot进行访问.后者是一个长期但非常有条理的功能,它揭示了更多的级联"功能来分派要添加的内容.

Besides, add_theme, what you're interested in is is add_ggplot which can be accessed with ggplot2:::add_ggplot. The latter - a long yet very organized function - reveals more "cascading" functions to dispatch what's meant to be added.

话虽这么说,当在要应用功能的类gg的对象上使用"+"时,R会知道"(由于 ,我认为大多数ggplot2行为都取决于此.

That being said, R "knows" when using "+" on an object of class gg which function to apply (because of S3 classes). You can find the starting point in ggplot2 GitHub repos, in the ggproto.R on which I think most of ggplot2 behaviour depends on.

这是您要找的吗?

这篇关于ggplot2 plus运算符如何定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆