简体   繁体   English

ggplot2 的图层附加运算符

[英]Layer appending operator for ggplot2

I love ggplot2 , but I'd prefer not to write myplot <- myplot + each time I add a layer.我喜欢ggplot2 ,但我不想每次添加图层时都写myplot <- myplot + For example,例如,

# Load library
library(ggplot2)

# Create plot
myplot <- ggplot(iris) 
myplot <- myplot + geom_point(aes(Sepal.Length, Sepal.Width))
myplot <- myplot + ggtitle("This is a plot")
myplot <- myplot + theme_dark()
myplot

So, I wanted to create something similar to the additive compound assignment operator in C ( += ).因此,我想创建类似于 C 中的加法复合赋值运算符 ( += ) 的东西。

# Operator for appending layer
`%+=%` <- function(g, l){
  eval.parent(substitute(g <- g + l))
}

# Test appending function
myplot2 <- ggplot(iris) 
myplot2 %+=% geom_point(aes(Sepal.Length, Sepal.Width))
myplot2 %+=% ggtitle("This is a plot")
myplot2 %+=% theme_dark()
myplot2

Created on 2019-12-20 by the reprex package (v0.3.0)reprex 包(v0.3.0) 于 2019 年 12 月 20 日创建

As you can see, it gives the same result as the longhand syntax.如您所见,它给出了与普通语法相同的结果。 This is approach is adapted from a solution given here to a different question and is accompanied by a word of warning;这种方法改编自此处针对不同问题给出的解决方案,并附有警告; however, it's not clear to me why this is potentially problematic.但是,我不清楚为什么这可能有问题。

My question: what are the potential detrimental or unexpected side effects of defining an operator like above?我的问题是:定义上述运算符的潜在有害或意外副作用是什么? Is there a better way of achieving the same result?有没有更好的方法来达到同样的结果?

The only downside I can honestly say is that your constructor is not as flexible as ggplot's.老实说,唯一的缺点是您的构造函数不如 ggplot 的灵活。 ggplot already has an overloaded operator + which doesn't work intuitively with your function. ggplot 已经有一个重载的运算符+ ,它不能直观地与您的函数一起使用。 Take this for an example:以此为例:

gg <- ggplot(iris)
gg %+=% geom_point(aes(Sepal.Length, Sepal.Width)) + theme_dark() #plots similar to your answer
   # only geom_point was added to gg variable
   #in other words the expression above is doing this
gg <- ggplot(iris)
gg <- gg + geom_point(aes(Sepal.Length, Sepal.Width))
gg + theme_dark()

This is a result of Infix functions which are always evaluated left to right.这是中缀函数始终从左到右求值的结果。 Thus unless you were able to capture the rest of the expression then there is little hope with using your function with ggplot's +因此,除非您能够捕获表达式的其余部分,否则将您的函数与 ggplot 的+一起使用的希望不大

In it's current state you are only able to assign a layer one at a time which is arguably less efficient/tidy than doing在当前状态下,您一次只能分配第一层,这可能比执行效率/整洁要低

gg <- gg + geom_point(aes(Sepal.Length,Sepal.Width)) +
    ggtitle("This is a plot") +
    theme_dark()

It is also unusable with itself, but if you recode it as such:它本身也无法使用,但是如果您将其重新编码为:

`%+=%` <- function(g, l) {
  UseMethod("%+=%")
}

`%+=%.gg` <- function(g, l){
  gg.name <- deparse(substitute(g))
  gg.name <- strsplit(gg.name, split = "%+=%", fixed= T)[[1]][1]
  gg.name <- gsub(pattern = " ", replacement = "", x = gg.name)
  gg <- g +l
  assign(x = gg.name, value = gg, envir = parent.frame())
}

gg <- ggplot(iris)
gg %+=% geom_point(aes(Sepal.Length, Sepal.Width)) %+=% theme_dark() #will now save left to right

I am really just being nit picky, but if your intent is to use this by adding a layer once at a time, then by all means your original function seems to work fine.我真的只是挑剔,但如果您的意图是通过一次添加一个图层来使用它,那么无论如何您的原始功能似乎都可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM