0

I save several plots in a list as one file using ggsave. I recently did an update to ggplot2 and my function has stopped working. Please could someone help me.

My code that is not working is as follows (however this used to work before):

library(tidyverse)

plot_1 <- cars %>% 
  ggplot(aes(speed, dist)) + geom_line()
    
plot_2 <- cars %>% 
  ggplot(aes(dist, speed)) + geom_line()

   
ggplot2::ggsave(filename = "myplot.pdf",
                plot = map(list(plot_1, plot_2), print),
                device = "pdf")
cephalopod
  • 1,826
  • 22
  • 31
  • Try this https://stackoverflow.com/a/52902877/786542 – Tung Aug 10 '21 at 00:36
  • @Tung I have several files that save using the above code. Any idea why the code stopped working? I was hoping for a quick fix. My other alternative is to use `pdf` but this will take time, which I want to avoid. – cephalopod Aug 10 '21 at 00:51

1 Answers1

0

To print multiple plots in the same pdf this works for me.

ggplot2::ggsave(filename = "myplot.pdf",
                plot = gridExtra::marrangeGrob(list(plot_1, plot_2), nrow = 1, ncol = 1), 
                device = "pdf")
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213