代码之家  ›  专栏  ›  技术社区  ›  eladin

只返回绘图的函数

  •  1
  • eladin  · 技术社区  · 7 年前

    我想要的函数只是为了编程时的方便。 但是,如果我想在最后一行之后添加一个层,我必须在最后一行附加一个“+”,如果我想再次删除该层,我还必须再次删除“+”:

    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配合不好。

    1 回复  |  直到 7 年前
        1
  •  3
  •   zx8754    7 年前

    我想我们需要 geom_blank()

    library(ggplot2) # ggplot2_2.2.1
    
    ggplot(mtcars, aes(wt, mpg)) +
      geom_point() +
      geom_blank() # The blank geom draws nothing