D. Woods D. Woods - 1 year ago 110
R Question

How to append a plot to an existing pdf file

I want to append a plot to an existing pdf long after

has been called*. After reading the
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)
pdf("Append to me.%03d.pdf",onefile=T)
plot(1:10,rep(5,10)) #Want this one on page 2

*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.

Answer Source

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) {
    my.plots[[i]] <- recordPlot()

pdf('myplots.pdf', onefile=TRUE)
for (my.plot in my.plots) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download