Search code examples
rggplot2gtable

Saving a PNG file from a gtable of ggplot object


I have created a facet_grid, and as some of the panels had no data, I created a gtable and specified which ones I wanted to be blank (otherwise the panels could be showing very low values).

This is the code I have used to create a gtable (found on stack overflow) :)

myplot2 <-
myplot %>%
  # Generate gtable of ggplot object
  ggplot2::ggplot_build() %>% ggplot2::ggplot_gtable() %>%
  # Modify gtable by filtering out grobs based on name using a regex pattern
  # $ represents end of string. Otherwise 'panel-1-1' removes 'panel-1-10', too.
  gtable::gtable_filter(pattern = "panel-1-1$", invert = TRUE) %>%
  gtable::gtable_filter(pattern = "panel-1-5$", invert = TRUE) %>%
  gtable::gtable_filter(pattern = "panel-1-6$", invert = TRUE) %>%
  gtable::gtable_filter(pattern = "panel-2-1$", invert = TRUE) %>%
  gtable::gtable_filter(pattern = "panel-2-5$", invert = TRUE) %>%
  gtable::gtable_filter(pattern = "panel-2-6$", invert = TRUE) %>%
  gtable::gtable_filter(pattern = "panel-3-1$", invert = TRUE) %>%
  gtable::gtable_filter(pattern = "panel-3-3$", invert = TRUE) %>%
  gtable::gtable_filter(pattern = "panel-5-5$", invert = TRUE) %>%
  gtable::gtable_filter(pattern = "panel-5-5$", invert = TRUE) %>%
  gtable::gtable_filter(pattern = "panel-1-6$", invert = TRUE) %>%
  gtable::gtable_filter(pattern = "panel-6-1$", invert = TRUE) %>%
  gtable::gtable_filter(pattern = "panel-6-2$", invert = TRUE) %>%
  gtable::gtable_filter(pattern = "panel-6-5$", invert = TRUE) %>%
  gtable::gtable_filter(pattern = "panel-6-6$", invert = TRUE) %>%
    # Plot the modified gtable
  {grid::grid.newpage(); grid::grid.draw(.)} 

However I am now unable to save the resulting image. I am using rmarkdown, so it doesn't appear in my 'plot' tab. I have tried the following (again from stack overflow), as well as other combinations of the 'save' function, but its not working.

png("myfile.png"); plot(myplot2); dev.off()

Grateful if someone could help me save a png file. Thanks


Solution

  • Thanks all. @teunbrand your suggestion worked. I've added 'myplot' into the code as you suggested, and the png is saved.

    myplot2 <-
    myplot %>%
      # Generate gtable of ggplot object
      ggplot2::ggplot_build() %>% ggplot2::ggplot_gtable() %>%
      # Modify gtable by filtering out grobs based on name using a regex pattern
      # $ represents end of string. Otherwise 'panel-1-1' removes 'panel-1-10', too.
      gtable::gtable_filter(pattern = "panel-1-1$", invert = TRUE) %>%
      gtable::gtable_filter(pattern = "panel-1-5$", invert = TRUE) %>%
      gtable::gtable_filter(pattern = "panel-1-6$", invert = TRUE) %>%
      gtable::gtable_filter(pattern = "panel-2-1$", invert = TRUE) %>%
      gtable::gtable_filter(pattern = "panel-2-5$", invert = TRUE) %>%
      gtable::gtable_filter(pattern = "panel-2-6$", invert = TRUE) %>%
      gtable::gtable_filter(pattern = "panel-3-1$", invert = TRUE) %>%
      gtable::gtable_filter(pattern = "panel-3-3$", invert = TRUE) %>%
      gtable::gtable_filter(pattern = "panel-5-5$", invert = TRUE) %>%
      gtable::gtable_filter(pattern = "panel-5-5$", invert = TRUE) %>%
      gtable::gtable_filter(pattern = "panel-1-6$", invert = TRUE) %>%
      gtable::gtable_filter(pattern = "panel-6-1$", invert = TRUE) %>%
      gtable::gtable_filter(pattern = "panel-6-2$", invert = TRUE) %>%
      gtable::gtable_filter(pattern = "panel-6-5$", invert = TRUE) %>%
      gtable::gtable_filter(pattern = "panel-6-6$", invert = TRUE) %>%
        # Plot the modified gtable
      {grid::grid.newpage(); grid::grid.draw(myplot)} 
    
    
    png("../images/all sites by year.png", width = 10, height = 7, units = 'in', res = 300); plot(myplot2); dev.off()