33

が呼び出されてからずっと後に、既存の pdf にプロットを追加したいdev.off()*。ヘルプ ファイルを読み、ここここ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()

* pdfデバイスが閉じられたにpdfファイルに追加したいので、これは上記のリンクされた質問の複製ではありません。

4

4 に答える 4

23

を使用recordPlotして各プロットを にlist保存し、最後に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()
于 2012-11-07T16:46:28.970 に答える
18

小規模で無料の、プラットフォームに依存しない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"
于 2012-11-07T16:49:50.310 に答える
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)

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:

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

于 2012-11-07T16:46:20.033 に答える
2

私は最近、この優れた作品を見つけました (自分のものだと主張しようとしているわけではありません)。

https://jonkimanalyze.wordpress.com/2014/07/24/r-compile-png-files-into-pdf/

OPが求めていたものとはまったく異なりますが、私が気に入っている理由は、pdf内のウィンドウのサイズ変更などに特にうまく反応しない非常に密な散布図やその他のプロットがあることが多いからです。ただし、複数ページの出力を生成する必要があります。したがって、プロットがデータ密度が高い場合は、それらを .png としてレンダリングし、上記の関数を使用して最後に再結合します。

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

}
于 2017-07-25T16:38:59.507 に答える