Search code examples
rggplot2magrittr

Function that simply returns the plot


The function I'm asking for is just for convenience during programming. Adding layers in ggplot2 with the "+" operator is great. Especially adding layers in the middle amounts to just adding another line of code. However, if I want to try to add a layer after the last line, I have to append a "+" to the last row and if I want to remove this layer again, I also have to remove the "+" again:

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

I'm searching for a function ggidentity() which just returns the plot itself to use it as a default last line so I can easily add more lines, as in

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

I tried it with a simple function

identity <- function(x) x

which works well with the magrittr-package (and improves my workflow in exploratory data analysis), but not with ggplot2.


Solution

  • I think we need geom_blank(), example:

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