Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple graphs pdf R

I would like to print multiple graphs in one pdf file. I know there has been a lot on this, but I would like to print different window/graph sizes for each page, i.e. first page a 8.5x11, second page 11x8.5 and so on. I tried this:

pdf(file="Combined_Graphs.pdf",onefile=TRUE,bg="white",width=8.5,height=11)
hist(rnorm(100))
pdf(file="Combined_Graphs.pdf",onefile=TRUE,width=11, height=8.5, bg="white")
hist(rnorm(100,10,2),col="blue")
dev.off()

I must be using onefile=TRUE wrong as it only generates the last graphic before closing. Is there a better way to size the graphic device without having to call the pdf function twice?

like image 214
austin Avatar asked Sep 26 '12 17:09

austin


People also ask

How do I save multiple plots as pdf in R?

To save multiple plots to the same page in the PDF file, we use the par() function to create a grid and then add plots to the grid. In this way, all the plots are saved on the same page of the pdf file. We use the mfrow argument to the par() function to create the desired grid.


1 Answers

What I would do is produce seperate PDF's and them combine them later. I use the PDF toolkit for this. Wrapping this in an R function using a system call through system even makes it scriptable from R. The call to pdftk will look something like:

pdftk *pdf cat output combined.pdf

or in R:

system("pdftk *pdf cat output combined.pdf")  

combine_pdfs = function(path, output_pdf) {
  system(sprintf("pdftk %s/*pdf cat output %s"), path, output_pdf)
}
like image 50
Paul Hiemstra Avatar answered Oct 05 '22 21:10

Paul Hiemstra