如何同时在多个设备上绘图?

发布于 2024-12-12 19:48:26 字数 366 浏览 0 评论 0原文

当我绘图时,我经常绘制到 eps 文件和 png 文件,如下所示:

postscript(file=paste(dir, output, "_ggplot.eps", sep=""), onefile=FALSE, horizontal=FALSE, width=4.8, height=4.0)
# Plotting code
dev.off()

png(paste(dir, output, "_ggplot.png", sep=""), width=450, height=300)
# Plotting code
dev.off()

问题是绘图代码重复两次。是否可以指定多个设备进行绘图?

When I am plotting, I often plot to an eps file and a png file like this:

postscript(file=paste(dir, output, "_ggplot.eps", sep=""), onefile=FALSE, horizontal=FALSE, width=4.8, height=4.0)
# Plotting code
dev.off()

png(paste(dir, output, "_ggplot.png", sep=""), width=450, height=300)
# Plotting code
dev.off()

The problem is that the plotting code is repeated twice. Is it possible to specify multiple devices for plotting?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

允世 2024-12-19 19:48:30

使用 R.devices 包,您可以执行以下操作:

library('R.devices')
library('ggplot2')

devEval(c("eps", "png"), name="myfig", tags="ggplot", sep="_", aspectRatio=1.2, {
  gg <- qplot(mpg, wt, data=mtcars, colour=cyl)
  print(gg)
})

这将生成“myfig_ggplot.eps”和“myfig_ggplot.png”。默认的 sep 是逗号,默认的输出目录是figures/。

Using the R.devices package, you can do:

library('R.devices')
library('ggplot2')

devEval(c("eps", "png"), name="myfig", tags="ggplot", sep="_", aspectRatio=1.2, {
  gg <- qplot(mpg, wt, data=mtcars, colour=cyl)
  print(gg)
})

This will generate 'myfig_ggplot.eps' and 'myfig_ggplot.png'. The default sep is a comma and the default output directory is figures/.

千柳 2024-12-19 19:48:30

ggplot(....)+(...)
ggsave("文件1.png")
ggsave("文件1.pdf")
ggsave("file1.jpg")

ggplot(....)+(...)
ggsave("文件2.png")
ggsave("文件2.pdf")
ggsave("文件2.jpg")

ggplot(....)+(...)
ggsave("file1.png")
ggsave("file1.pdf")
ggsave("file1.jpg")

ggplot(....)+(...)
ggsave("file2.png")
ggsave("file2.pdf")
ggsave("file2.jpg")

咽泪装欢 2024-12-19 19:48:29

从标准用法来看,泰勒是正确的。但是,为了让生活更轻松,您可以尝试另一种方法:将绘图代码包装为函数,以便您可以包装一系列输出。这至少可以简化用于生成输出的代码。

另一种可行的可能性是通过 foreach 分叉您的流程,并且每次迭代都会生成不同类型的输出,具体取决于与迭代关联的索引。我这样做是为了并行生成很多图(虽然也许我使用了 Hadoop,但我现在不记得了)。

Tyler is correct from the standard usage. However, to make life easier, you can try an alternate method: wrap your plotting code as a function, so that you can then wrap a sequence of outputs. That can at least simplify your code for producing output.

Another possibility that may work is to fork your process, via foreach, and each iteration produces a different type of output, depending on the index associated with the iteration. I have done this to produce a lot of plots in parallel (though maybe I used Hadoop, I can't remember at the moment).

醉态萌生 2024-12-19 19:48:29

您可以使用 for 循环:

devices <- c("pdf", "png")

for (i in seq_along(devices)) {

    if (devices[i] == "png") {
    ppi <- 600
    png(file = "Plots/regression.png",
        width = 8.4 * ppi, height = 6.5 * ppi, res = ppi,
        family = "Latin Modern Roman")
    }

    if (devices[i] == "pdf") {
        cairo_pdf(file = "Plots/regression.pdf", width = 8.4, height = 6.5,
                  family = "Latin Modern Roman")
    }

    # Insert plotting code

    graphics.off()

}

You could use a for-loop:

devices <- c("pdf", "png")

for (i in seq_along(devices)) {

    if (devices[i] == "png") {
    ppi <- 600
    png(file = "Plots/regression.png",
        width = 8.4 * ppi, height = 6.5 * ppi, res = ppi,
        family = "Latin Modern Roman")
    }

    if (devices[i] == "pdf") {
        cairo_pdf(file = "Plots/regression.pdf", width = 8.4, height = 6.5,
                  family = "Latin Modern Roman")
    }

    # Insert plotting code

    graphics.off()

}
遮了一弯 2024-12-19 19:48:28

您可以使用 dev.copy() 组合它们。例如,

  X11 ()
  plot (x,y)
  dev.copy (jpeg,filename="test.jpg");
  dev.off ();

查找 help(dev.copy) 了解更多详细信息。

Usage:

     dev.copy(device, ..., which = dev.next())
     dev.print(device = postscript, ...)
     dev.copy2eps(...)
     dev.copy2pdf(..., out.type = "pdf")
     dev.control(displaylist = c("inhibit", "enable"))

You can combine them using dev.copy(). For example,

  X11 ()
  plot (x,y)
  dev.copy (jpeg,filename="test.jpg");
  dev.off ();

Lookup help(dev.copy) for more details.

Usage:

     dev.copy(device, ..., which = dev.next())
     dev.print(device = postscript, ...)
     dev.copy2eps(...)
     dev.copy2pdf(..., out.type = "pdf")
     dev.control(displaylist = c("inhibit", "enable"))
美煞众生 2024-12-19 19:48:28

不,这是不可能的。至少根据 ?grDevices 的手册没有:

“详细信息:只有一个设备是“活动”设备:这是以下设备
所有图形操作都发生在其中。有一个“空设备”,它是
始终打开,但实际上是一个占位符:任何使用它的尝试都会
打开由 getOption("device")) 指定的新设备。"

No it's not possible. At least not according to the manual for ?grDevices:

"Details: Only one device is the ‘active’ device: this is the device in
which all graphics operations occur. There is a "null device" which is
always open but is really a placeholder: any attempt to use it will
open a new device specified by getOption("device"))."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文