Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append a plot to an existing pdf file

Tags:

plot

r

pdf

I want to append a plot to an existing pdf long after dev.off() has been called*. After reading the pdf() help file and after reading the Q & A here and here, I'm pretty sure it can't be done in R. But, maybe some of you smarter people have a solution that I wasn't able to find.

pdf("Append to me.%03d.pdf",onefile=T) plot(1:10,10:1) #First plot (page 1) dev.off() pdf("Append to me.%03d.pdf",onefile=T) plot(1:10,rep(5,10)) #Want this one on page 2 dev.off() 

*This not a duplicate of the questions linked above because I want to append to a pdf file after the pdf device has been closed.

like image 553
D. Woods Avatar asked Nov 07 '12 16:11

D. Woods


People also ask

What does append to existing PDF mean?

The "Append" button will create a new document containing the existing PDF file and will add the new document information to the end of the file. The "Replace" button will overwrite the existing PDF file, and the "Cancel" button will cancel the PDF creation without affecting the existing PDF file.

How do I add a page to a PDF in Python?

PyPDF2 in Python provide method addPage(page) which can be used to add pages in a PDF document in Python. This function adds a page to the PDF file. The page is usually acquired from a PdfFileReader instance. Parameters: page (PageObject) – The page to add to the document.

How do I save multiple plots 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

You could use recordPlot to store each plot in a list, then write them all to a pdf file at the end with replayPlot. Here's an example:

num.plots <- 5 my.plots <- vector(num.plots, mode='list')  for (i in 1:num.plots) {     plot(i)     my.plots[[i]] <- recordPlot() } graphics.off()  pdf('myplots.pdf', onefile=TRUE) for (my.plot in my.plots) {     replayPlot(my.plot) } graphics.off() 
like image 68
Matthew Plourde Avatar answered Sep 18 '22 16:09

Matthew Plourde