Sequentially add plots to list

  Kiến thức lập trình

I’m creating a large number of ggplots, which I would like to add to a list, so that I can create a pdf from the entire list in one go.

myplots <- list()

#generate ggplot1
ggplot1 -> myplots[[1]]

#generate ggplot2
ggplot2 -> myplots[[2]]

#many more ggplots

pdf("myplots.pdf")
for (i in myplots) {
  print(i)
}
dev.off()

Is there a less repetitive way to code this? For instance, I’d like to add the plots at the end of the list, without having to specify the position numerically ([[2]] etc), as I might rearrange the plots in the future. Thanks!

LEAVE A COMMENT