R 如何保存在循环中创建的图表对象

R 如何保存在循环中创建的图表对象,r,graphics,plot,charts,save,R,Graphics,Plot,Charts,Save,我在几个循环中创建了一个图表,我想自动将图表写入外部循环末尾的文件中。以下是一个玩具示例: filename <- "mychart" for(i in 1:5) { x <- 1:5 fun1 <- sample(1:10, 5, replace = TRUE) xlim <- c(1, 5) ylim <- c(0, 10) plot(x, fun1, xlim = xlim, ylim = ylim, type = "l") for(j

我在几个循环中创建了一个图表,我想自动将图表写入外部循环末尾的文件中。以下是一个玩具示例:

filename <- "mychart"
for(i in 1:5) {
  x <- 1:5
  fun1 <- sample(1:10, 5, replace = TRUE)
  xlim <- c(1, 5)
  ylim <- c(0, 10)
  plot(x, fun1, xlim = xlim, ylim = ylim, type = "l")
  for(j in 1:3)  {
    fun2 <- 2:6 + j
    lines(x, fun2, type = "l", col = "red")
  }
  out.filename <- paste(filename, i, sep = "")
  ## want to save this plot out to disk here!
}

filename我想这就是你想要的:

plotit <- function(i) {
   x = 1:5
   fun1 = sample(1:10, 5, replace=TRUE)
   plot(x, fun1, xlim=c(1,5), ylim=c(0,10), type="l")
   for(j in 1:3)  {
       fun2 = 2:6 + j
       lines(x, fun2, type = "l", col = "red")
   }   
   savePlot(paste0("mychart", i, ".png"), type="png")
}

保存基本图形打印的典型方法是使用单独的设备功能,如
pdf()
png()
,等等。您可以使用适当的文件名打开打印设备,创建打印,然后使用
dev.off()
关闭设备。绘图是否在for循环中创建并不重要。请参见
?png
中的大量设备(以及底部的示例)

对于您的代码,它将如下所示:

filename <- "mychart"
for(i in 1:5) {

    out.filename <- paste(filename, i, ".png", sep = "")
    ## Open the device before you start plotting
    png(file = out.filename)
    # you can set the height and width (and other parameters) or use defaults

    x <- 1:5
    fun1 <- sample(1:10, 5, replace = TRUE)
    xlim <- c(1, 5)
    ylim <- c(0, 10)
    plot(x, fun1, xlim = xlim, ylim = ylim, type = "l")
    for(j in 1:3)  {
        fun2 <- 2:6 + j
        lines(x, fun2, type = "l", col = "red")
    }
    ## Close the device when you are done plotting.
    dev.off() 
}

filename也许我前面的答案很有用,我想几乎是重复的。链接。是的,savePlot是我需要的。下面给出的例子。谢谢。@smci这是基础图形,没有打印对象。谢谢。这会引发一个错误,因为我认为需要加载X11库才能使用savePlot。错误显示“保存图(paste0(“mychart”,i,“.png”),type=“png”):只能从“X11(type=“*cairo”)”设备复制”,我使用的是RStudio,这可能是一个因素。好的。我越深入这一点……我使用的是Mac OS和RStudio。当我尝试使用X11()我需要安装软件包XQuartz。但是,XQuartz不适用于R3.2.1。因此,可能savePlot对我不可用。??好的。要使上述答案在Mac OS上工作(Maverick)RStudio我必须安装XQuartz,并将其作为我的主要X11窗口服务器。这为我的桌面增加了一层复杂性,只是为了解决这一问题,但它确实起作用。是的,安装XQuartz的RStudio过程有点痛苦,也许你可以建议改进他们的错误消息和在线文档。对于建议使用窗口,我深表歉意s-specific回答,但没有说明它是,我很高兴你把它整理好了。但是,Gregor,这不允许人们在RStudio控制台窗口中看到绘图,对吗?最好能够在绘图时看到这些绘图,而不必等待和查看文件,因为我使用的真正循环会运行数小时。不,这是错误的oesn没有。(我想我没有把问题通读一遍:\)。只要
dev.off()一出现,你就可以查看每个情节
line正在运行。你真的花了几个小时来观察绘图的制作然后保存吗?似乎保存所有绘图并进行审阅是一个更好的工作流程。不,同意,但我需要查看前一个或两个绘图,以确保在整个运行过程中投入数小时的机器时间之前一切正常。可能有些事情我需要合作通过中断基于第一个绘图的运行进行更正。但是,您的解决方案在不涉及X11的情况下更干净。我可能只需要使用它,正如您所说,在程序仍在运行时查看第一个绘图文件。
filename <- "mychart"
for(i in 1:5) {

    out.filename <- paste(filename, i, ".png", sep = "")
    ## Open the device before you start plotting
    png(file = out.filename)
    # you can set the height and width (and other parameters) or use defaults

    x <- 1:5
    fun1 <- sample(1:10, 5, replace = TRUE)
    xlim <- c(1, 5)
    ylim <- c(0, 10)
    plot(x, fun1, xlim = xlim, ylim = ylim, type = "l")
    for(j in 1:3)  {
        fun2 <- 2:6 + j
        lines(x, fun2, type = "l", col = "red")
    }
    ## Close the device when you are done plotting.
    dev.off() 
}