Save plots generated by repeat function

59 Views Asked by At

Currently I am looking for a solution to save 72 plots to an PDF file. Those 72 plots are created through a function and the below code of repeat.

The ID stands for a person within my dataset. This person has multiple rows of data attached to his ID. To go to the next person I use ID = ID + 1

With the below code I manage to create an PDF file but, this is a PDF file with 72 pages. I would like to have 4 plots on each row. Something in the idea of nrow = 4 like you use in grid.arrange. Preferably 4 on each row and 24 on each page.

pdf("plot1.pdf")
 repeat {
  ID = ID + 1
  print(ggplot(ID))
  if (ID == 72){
    break}}
dev.off()
1

There are 1 best solutions below

0
On

I am still not sure wether this is a legit solution but at least it is fixed now!

Apparently I was looking for a solution like this:

plot.list <- list()
for(i in 1:72){
  plot.list[[length(plot.list) + 1]] <- plot_utility(i)
}

grid1 <- grid.arrange(grobs = plot.list, ncol = 4)

ggsave("plot1.pdf", 
       plot = grid1, 
       device = "pdf", 
       scale = 2,
       width = 25, 
       height = 20, 
       units = c("cm")
)