2017-09-06 141 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