【问题标题】:R graphics: output to several file formatsR图形:输出为多种文件格式
【发布时间】:2020-01-06 00:21:53
【问题描述】:

在许多脚本中,我首先在屏幕上开发了一个图形,然后需要将其保存为具有特定高度/宽度/分辨率的几种文件格式。使用png()pdf()svg(),...打开设备,然后dev.off() 关闭它,我不得不将所有设备打开调用放入我的脚本中并将它们注释掉并重新- 一次运行一个设备的代码。

我确实知道对于 ggplot 图形,ggsave() 使这更容易。 对于 base-Rlattice 图形,我可以做些什么来简化这一点?

一个例子:

png(filename="myplot.png", width=6, height=5, res=300, units="in")
# svg(filename="myplot.svg", width=6, height=5)
# pdf(filename="myplot.pdf", width=6, height=5)

op <- par()  # set graphics parameters
plot()       # do the plot
par(op)
dev.off() 

【问题讨论】:

    标签: r ggplot2 graphics lattice device-driver


    【解决方案1】:

    图形设备是 grDevices 包的一部分。关于使用多个开放设备的documentation 可能值得一读。据我了解,存储了一个开放设备的圆形阵列,但只有当前设备处于活动状态。出于这个原因,打开所有需要的设备,然后使用dev.list() 循环遍历它们可能是您最好的选择。

    # data for sample plot
    x <- 1:5
    y <- 5:1
    
    # open devices
    svg(filename="myplot.svg", width=6, height=5)
    png(filename="myplot.png", width=6, height=5, res=300, units="in")
    pdf()
    
    # devices assigned an index that can be used to call them
    dev.list()
    svg png pdf 
      2   3   4 
    
    # loop through devices, not sure how to do this without calling plot() each time
    # only dev.cur turned off and dev.next becomes dev.cur
    for(d in dev.list()){plot(x,y); dev.off()} 
    
    # check that graphics device has returned to default null device
    dev.cur()
    null device 
          1 
    dev.list()
    NULL
    
    file.exists("myplot.svg")
    [1] TRUE
    file.exists("myplot.png")
    [1] TRUE
    file.exists("Rplots.pdf") # default name since none specified in creating pdf device
    [1] TRUE
    

    您可以使用的文档中还有很多内容。

    【讨论】:

      【解决方案2】:

      您可以使用 cowplot 包将您的基础或格子图形转换为 ggplot2 对象,然后您可以通过ggsave() 保存这些对象。这并非完全万无一失,但适用于大多数情节。您还需要安装 gridGraphics 包才能使其正常工作。查看更多here.

      library(ggplot2)
      library(cowplot)
      #> 
      #> ********************************************************
      #> Note: As of version 1.0.0, cowplot does not change the
      #>   default ggplot2 theme anymore. To recover the previous
      #>   behavior, execute:
      #>   theme_set(theme_cowplot())
      #> ********************************************************
      
      # define a function that emits the desired plot
      p1 <- function() {
        par(
          mar = c(3, 3, 1, 1),
          mgp = c(2, 1, 0)
        )
        boxplot(mpg ~ cyl, xlab = "cyl", ylab = "mpg", data = mtcars)
      }
      
      # the plot using base graphics
      p1()
      

      
      # the plot converted into a ggplot2 object
      p2 <- ggdraw(p1)
      p2
      

      
      # save in different formats
      ggsave("plot.pdf", p2)
      #> Saving 7 x 5 in image
      ggsave("plot.png", p2)
      #> Saving 7 x 5 in image
      ggsave("plot.svg", p2)
      #> Saving 7 x 5 in image
      

      reprex package (v0.3.0) 于 2020-01-05 创建

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-12-06
        • 2017-01-03
        • 2011-11-12
        • 1970-01-01
        • 2023-03-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多