如何将R图形打印到多页PDF和多个PDF?

时间:2009-09-08 18:02:51

标签: graphics r plot

我知道

 pdf("myOut.pdf")

将打印到R中的PDF。如果我想

  1. 制作一个循环,在PDF文件的新页面上打印后续图表(追加到最后)?

  2. 创建一个循环,将后续图形打印到新的PDF文件(每个文件一个图形)?

3 个答案:

答案 0 :(得分:54)

你看过帮助(pdf)吗?

  

用法:

 pdf(file = ifelse(onefile, "Rplots.pdf", "Rplot%03d.pdf"),
     width, height, onefile, family, title, fonts, version,
     paper, encoding, bg, fg, pointsize, pagecentre, colormodel,
     useDingbats, useKerning)
     

参数:

file: a character string giving the name of the file. For use with
      'onefile=FALSE' give a C integer format such as
      '"Rplot%03d.pdf"' (the default in that case). (See
      'postscript' for further details.)

对于1),将onefile保留为默认值TRUE。几个图表都放在同一个文件中。

对于2),将onefile设置为FALSE并选择具有C整数格式的文件名,R将创建一组文件。

答案 1 :(得分:36)

不确定我理解。

追加到同一个文件(每页一个图):

pdf("myOut.pdf")
for (i in 1:10){
  plot(...)
}
dev.off()

每个循环的新文件:

for (i in 1:10){
  pdf(paste("myOut",i,".pdf",sep=""))
  plot(...)
  dev.off()
}

答案 2 :(得分:2)

pdf(file = "Location_where_you_want_the_file/name_of_file.pdf", title="if you want any")
plot() # Or other graphics you want to have printed in your pdf
dev.off()

您可以在pdf中绘制任意数量的内容,图表将添加到不同页面的pdf中。 dev.off()关闭与文件的连接,将创建pdf,你会看到像

这样的东西
> dev.off()
null device 1