如何将图添加到现有的pdf文件中 [英] How to append a plot to an existing pdf file

查看:145
本文介绍了如何将图添加到现有的pdf文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在调用dev.off()后很长时间将图添加到现有的pdf中.在阅读pdf()帮助文件之后,并在阅读了Q&之后.在此处此处,我是可以肯定,它无法在R中完成.但是,也许有些聪明的人有一个我找不到的解决方案.

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()

*这不是上面链接的问题的重复,因为我想在

关闭pdf设备后追加到 pdf文件.

解决方案

您可以使用recordPlot将每个图存储在list中,然后将它们全部写到pdf文件中,并以replayPlot结尾.这是一个示例:

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()

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.

解决方案

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()

这篇关于如何将图添加到现有的pdf文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆