cutebunny
cutebunny

Reputation: 789

output ggplot results in one pdf but several pages in R

I have at least 10 plots by ggplot(we can call them plot1, plot2 ....). I can output them into separate pdf files. But I prefer to output them in only one pdf file but several pages. One page, one plot from ggplot.

I tried to list all plots and use ggsave but it can not work. Any idea or script can help? Thank you

Upvotes: 8

Views: 6271

Answers (2)

CoderGuy123
CoderGuy123

Reputation: 6649

Based on aosmith's answer, here's a simple wrapper function to save lists of ggplot2 plots to a single pdf.

GG_save_pdf = function(list, filename) {
  #start pdf
  pdf(filename)

  #loop
  for (p in list) {
    print(p)
  }

  #end pdf
  dev.off()

  invisible(NULL)
}

Upvotes: 6

aosmith
aosmith

Reputation: 36076

See the pdf function for this.

For three plots it would look like this (saving into your working directory with default naming). Run through dev.off line before you can open file.

pdf()
plot1
plot2
plot3
dev.off()

If your plots are already stored in a list named list1:

pdf()
list1
dev.off()

Upvotes: 15

Related Questions