0

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

Phil
  • 7,287
  • 3
  • 36
  • 66
Bramble95
  • 57
  • 8
  • Do you get an error message? Have you specified a correct pathway with `setwd()`? – Yacine Hajji Mar 10 '22 at 16:45
  • Hi thanks for your swift response. Yes the working directory is set. I got the following error message. Error in plot.window(...) : need finite 'xlim' values – Bramble95 Mar 10 '22 at 17:06
  • Does `plot(myplot2)` alone works when you chunk? – Yacine Hajji Mar 10 '22 at 17:24
  • No, when I run ```plot(myplot2)``` I get the same error messge ```Error in plot.window(...) : need finite 'xlim' values``` – Bramble95 Mar 10 '22 at 17:26
  • Just ran the code without attributing it to an object (ie began with ```myplot %>%```) and that still works fine, and draws the plot. However when I add the ```myplot %>%``` again (as in the code in my original question), I've noticed that in my list of values in the Environment window, it says myplot2 NULL. Not sure if that's relevant or not. – Bramble95 Mar 10 '22 at 17:29
  • 1
    I don't think you need `plot(myplot2)` but `grid::grid.newpage(); grid::grid.draw(myplot2)`. – teunbrand Mar 10 '22 at 21:51
  • This may help you https://stackoverflow.com/questions/21349368/error-in-plot-window-need-finite-xlim-values – Yacine Hajji Mar 11 '22 at 08:39

1 Answers1

0

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()
Bramble95
  • 57
  • 8