【问题标题】:How to print R graphics to multiple pages of a PDF and multiple PDFs?如何将 R 图形打印到 PDF 的多个页面和多个 PDF?
【发布时间】:2010-11-26 14:16:39
【问题描述】:

我知道

 pdf("myOut.pdf")

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

  1. 创建一个循环以在 PDF 文件的新页面上打印后续图表(附加到末尾)?

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

【问题讨论】:

    标签: graphics r plot


    【解决方案1】:

    你看过帮助(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 将创建一组文件。

    【讨论】:

    • 如果您使用网格布局,如何移动到下一个 PDF 页面?例如,您准备了一些 ggplot,将它们放入多页 PDF 的 p1 上的视口中,但是如何让下一个视口进入第 2 ... 3 .... 页等?
    【解决方案2】:

    不确定我是否理解。

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

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

    【讨论】:

    • 您甚至不需要 paste() 覆盖文件名——R 也可以为您做到这一点;看我的回答。
    • 如何在每个页面中包含多个图?例如,一页中有 5 个图,下一页中有接下来的 5 个图。我曾尝试在命令plot 之前包含par(mfrow=c(5,1)),但我只得到每个图(在本例中为10 个图)出现在10 页中,但尺寸的大小在函数par 中定义,在此案例5行1列。提前致谢
    • @Ruben。您应该在 par(mfrow=c(5,1)) 之前调用 pdf() (即:不是相反的方向)。您还可以考虑将 pdf() 边距中的宽度和高度增加到很小。希望这会有所帮助。
    【解决方案3】:
    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
    

    【讨论】:

      猜你喜欢
      • 2017-12-26
      • 1970-01-01
      • 1970-01-01
      • 2012-08-27
      • 2018-11-15
      • 2017-09-17
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      相关资源
      最近更新 更多