2017-09-06 144 views
1

我要求的功能只是为了编程时的方便。 使用“+”运算符在ggplot2中添加图层非常棒。特别是在中间添加图层只是添加另一行代码。但是,如果我想在最后一行之后添加一个图层,则必须在最后一行附加一个“+”,如果我想再次删除此图层,则还必须再次删除“+”:函数只是返回图

ggplot(df, aes(x,y,...)) + 
    geom_X(...) +  # after this line, I can easily add layers 
    ... + 
    layer_Z(...)  # to add a layer after here, I have to modify also this line 

我在寻找一个功能ggidentity()刚刚返回情节本身使用它作为默认的最后一行,所以我可以很容易地添加更多的线条,在

ggplot(df, aes(x,y,...)) + 
    geom_X(...) +  # after this line, I can easily add layers 
    ... + 
    layer_Z(...) +  # now it's easy to add layers after this line 
    ggidentity()  # this doesn't change anything in the plot 

我尝试了用一个简单的功能

identity <- function(x) x 

它与magrittr包配合良好(并改善了我的探索性数据分析工作流程),但与ggplot2不兼容。

+0

打开'ggidentity()''到列表()'? – lukeA

回答

3

我认为我们需要geom_blank(),例如:

library(ggplot2) # ggplot2_2.2.1 

ggplot(mtcars, aes(wt, mpg)) + 
    geom_point() + 
    geom_blank() # The blank geom draws nothing