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.
我想在dev.off()被称为*之后很久再将一个阴谋添加到一个现有的pdf中。在阅读了pdf()帮助文件并在这里和这里阅读了Q & A之后,我很确定在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()
*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.
*这不是上面链接的问题的副本,因为我想在pdf设备关闭后附加到pdf文件中。
5 个解决方案
#1
19
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:
您可以使用recordPlot将每个plot存储在一个列表中,然后在replayPlot的末尾将它们全部写入一个pdf文件。这里有一个例子:
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()
#2
17
If you are willing to install the small, free, platform-independent pdftk utililty, you could use a system call from R to have it stitch all of your figures together:
如果您愿意安装小型的、免费的、独立于平台的pdftk实用程序,您可以使用来自R的系统调用,让它将您的所有数据缝合在一起:
## A couple of example pdf docs
pdf("Append to me.1.pdf")
plot(1:10,10:1)
dev.off()
pdf("Append to me.2.pdf")
plot(1:10,rep(5,10))
dev.off()
## Collect the names of the figures to be glued together
ff <- dir(pattern="Append to me")
## The name of the pdf doc that will contain all the figures
outFileName <- "AllFigs.pdf"
## Make a system call to pdftk
system2(command = "pdftk",
args = c(shQuote(ff), "cat output", shQuote(outFileName)))
## The command above is equiv. to typing the following at the system command line
## pdftk "Append to me.1.pdf" "Append to me.2.pdf" cat output "AllFigs.pdf"
#3
6
This is horribly hacky and probably belies my limited UNIX shell fu, but it works for me on a Fedora 17 box with the pdfjam package installed (not an R package, but from the YUM repos)
这非常陈腐,可能与我有限的UNIX shell fu不相符,但它适用于安装了pdfjam包(不是R包,而是YUM repos)的Fedora 17盒。
pdf("pdf1.pdf")
plot(1:10)
dev.off()
pdf("| pdfjoin --outfile \"pdf2.pdf\" && pdfjoin pdf1.pdf pdf2.pdf --outfile pdf1.pdf && rm pdf2.pdf")
plot(10:1)
dev.off()
The output in R is:
R中的输出为:
> pdf("| pdfjoin --outfile \"pdf2.pdf\" && pdfjoin pdf1.pdf pdf2.pdf --outfile pdf1.pdf && rm pdf2.pdf")## && pdfunite joined.pdf tmp.pdf joined.pdf && rm tmp.pdf")
> plot(10:1)
> dev.off()
----
pdfjam: This is pdfjam version 2.08.
pdfjam: Reading any site-wide or user-specific defaults...
(none found)
pdfjam: No PDF/JPG/PNG source specified: input is from stdin.
pdfjam: Effective call for this run of pdfjam:
/usr/bin/pdfjam --fitpaper 'true' --rotateoversize 'true' --suffix joined --outfile pdf2.pdf -- /dev/stdin -
pdfjam: Calling pdflatex...
pdfjam: Finished. Output was to 'pdf2.pdf'.
----
pdfjam: This is pdfjam version 2.08.
pdfjam: Reading any site-wide or user-specific defaults...
(none found)
pdfjam: Effective call for this run of pdfjam:
/usr/bin/pdfjam --fitpaper 'true' --rotateoversize 'true' --suffix joined --outfile pdf1.pdf -- pdf1.pdf - pdf2.pdf -
pdfjam: Calling pdflatex...
pdfjam: Finished. Output was to 'pdf1.pdf'.
null device
1
Basically, pdfjoin
will take input from stdin
if it is the only input file so I pipe the output from pdf()
to the pdfjoin
program and specify the output file using the --outfile
argument. Then using &&
is join the original pdf1.pdf
with the pdf2.pdf
just created, specifying that the output PDF is pdf1.pdf
, the name of the original PDF.
基本上,如果stdin是唯一的输入文件,pdfjoin将接受输入,因此我将输出从pdf()传输到pdfjoin程序,并使用-outfile参数指定输出文件。然后使用&并加入原始的pdf1。与pdf2 pdf。pdf刚刚创建,指定输出pdf为pdf1。pdf,原始pdf的名称。
#4
0
I found this excellent work recently (not attempting to claim it as my own)
我最近发现了这幅优秀的作品(并不是想声称它是我自己的)
https://jonkimanalyze.wordpress.com/2014/07/24/r-compile-png-files-into-pdf/
https://jonkimanalyze.wordpress.com/2014/07/24/r-compile-png-files-into-pdf/
It's not quite what the OP was asking for, but the reason I like it is that I often have quite dense scatterplots and other plots that don't respond particularly well to window resizing etc. within a pdf. However I need to produce multi page output. So if the plots are data-dense I render them as .pngs and then use the above function to recombine at the end.
这并不是OP所要求的,但我喜欢它的原因是,我经常有非常密集的散点图和其他图,它们对窗口大小的响应不是特别好。但是我需要生成多页输出。因此,如果这些图是数据密集的,我将它们作为。pngs,然后使用上面的函数在最后重新组合。
merge.png.pdf <- function(pdfFile, pngFiles, deletePngFiles=FALSE) {
pdf(pdfFile)
n <- length(pngFiles)
for( i in 1:n) {
pngFile <- pngFiles[i]
pngRaster <- readPNG(pngFile)
grid.raster(pngRaster, width=unit(0.8, "npc"), height= unit(0.8, "npc"))
if (i < n) plot.new()
}
dev.off()
if (deletePngFiles) {
unlink(pngFiles)
}
}
#5
-2
pdf("myFile.pdf",onefile=T)
plot(1:10,10:1) #First plot (page 1)
plot(1:10,rep(5,10)) #Want this one on page 2
dev.off()
#1
19
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:
您可以使用recordPlot将每个plot存储在一个列表中,然后在replayPlot的末尾将它们全部写入一个pdf文件。这里有一个例子:
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()
#2
17
If you are willing to install the small, free, platform-independent pdftk utililty, you could use a system call from R to have it stitch all of your figures together:
如果您愿意安装小型的、免费的、独立于平台的pdftk实用程序,您可以使用来自R的系统调用,让它将您的所有数据缝合在一起:
## A couple of example pdf docs
pdf("Append to me.1.pdf")
plot(1:10,10:1)
dev.off()
pdf("Append to me.2.pdf")
plot(1:10,rep(5,10))
dev.off()
## Collect the names of the figures to be glued together
ff <- dir(pattern="Append to me")
## The name of the pdf doc that will contain all the figures
outFileName <- "AllFigs.pdf"
## Make a system call to pdftk
system2(command = "pdftk",
args = c(shQuote(ff), "cat output", shQuote(outFileName)))
## The command above is equiv. to typing the following at the system command line
## pdftk "Append to me.1.pdf" "Append to me.2.pdf" cat output "AllFigs.pdf"
#3
6
This is horribly hacky and probably belies my limited UNIX shell fu, but it works for me on a Fedora 17 box with the pdfjam package installed (not an R package, but from the YUM repos)
这非常陈腐,可能与我有限的UNIX shell fu不相符,但它适用于安装了pdfjam包(不是R包,而是YUM repos)的Fedora 17盒。
pdf("pdf1.pdf")
plot(1:10)
dev.off()
pdf("| pdfjoin --outfile \"pdf2.pdf\" && pdfjoin pdf1.pdf pdf2.pdf --outfile pdf1.pdf && rm pdf2.pdf")
plot(10:1)
dev.off()
The output in R is:
R中的输出为:
> pdf("| pdfjoin --outfile \"pdf2.pdf\" && pdfjoin pdf1.pdf pdf2.pdf --outfile pdf1.pdf && rm pdf2.pdf")## && pdfunite joined.pdf tmp.pdf joined.pdf && rm tmp.pdf")
> plot(10:1)
> dev.off()
----
pdfjam: This is pdfjam version 2.08.
pdfjam: Reading any site-wide or user-specific defaults...
(none found)
pdfjam: No PDF/JPG/PNG source specified: input is from stdin.
pdfjam: Effective call for this run of pdfjam:
/usr/bin/pdfjam --fitpaper 'true' --rotateoversize 'true' --suffix joined --outfile pdf2.pdf -- /dev/stdin -
pdfjam: Calling pdflatex...
pdfjam: Finished. Output was to 'pdf2.pdf'.
----
pdfjam: This is pdfjam version 2.08.
pdfjam: Reading any site-wide or user-specific defaults...
(none found)
pdfjam: Effective call for this run of pdfjam:
/usr/bin/pdfjam --fitpaper 'true' --rotateoversize 'true' --suffix joined --outfile pdf1.pdf -- pdf1.pdf - pdf2.pdf -
pdfjam: Calling pdflatex...
pdfjam: Finished. Output was to 'pdf1.pdf'.
null device
1
Basically, pdfjoin
will take input from stdin
if it is the only input file so I pipe the output from pdf()
to the pdfjoin
program and specify the output file using the --outfile
argument. Then using &&
is join the original pdf1.pdf
with the pdf2.pdf
just created, specifying that the output PDF is pdf1.pdf
, the name of the original PDF.
基本上,如果stdin是唯一的输入文件,pdfjoin将接受输入,因此我将输出从pdf()传输到pdfjoin程序,并使用-outfile参数指定输出文件。然后使用&并加入原始的pdf1。与pdf2 pdf。pdf刚刚创建,指定输出pdf为pdf1。pdf,原始pdf的名称。
#4
0
I found this excellent work recently (not attempting to claim it as my own)
我最近发现了这幅优秀的作品(并不是想声称它是我自己的)
https://jonkimanalyze.wordpress.com/2014/07/24/r-compile-png-files-into-pdf/
https://jonkimanalyze.wordpress.com/2014/07/24/r-compile-png-files-into-pdf/
It's not quite what the OP was asking for, but the reason I like it is that I often have quite dense scatterplots and other plots that don't respond particularly well to window resizing etc. within a pdf. However I need to produce multi page output. So if the plots are data-dense I render them as .pngs and then use the above function to recombine at the end.
这并不是OP所要求的,但我喜欢它的原因是,我经常有非常密集的散点图和其他图,它们对窗口大小的响应不是特别好。但是我需要生成多页输出。因此,如果这些图是数据密集的,我将它们作为。pngs,然后使用上面的函数在最后重新组合。
merge.png.pdf <- function(pdfFile, pngFiles, deletePngFiles=FALSE) {
pdf(pdfFile)
n <- length(pngFiles)
for( i in 1:n) {
pngFile <- pngFiles[i]
pngRaster <- readPNG(pngFile)
grid.raster(pngRaster, width=unit(0.8, "npc"), height= unit(0.8, "npc"))
if (i < n) plot.new()
}
dev.off()
if (deletePngFiles) {
unlink(pngFiles)
}
}
#5
-2
pdf("myFile.pdf",onefile=T)
plot(1:10,10:1) #First plot (page 1)
plot(1:10,rep(5,10)) #Want this one on page 2
dev.off()