2012-07-30 54 views
24

我真的很感谢您对以下问题的帮助。我知道几种将单个绘图保存到文件的方法。我的问题是:如何正确地将多字段保存到文件?R - 将多槽保存到文件

首先,我不是经验丰富的R用户。我使用ggplot2来创建我的图,另外我应该提到的是我使用RStudio GUI。使用R Cookbook的example,我可以在一个窗口中创建多个图。

我想保存这个所谓的多槽到一个文件(最好是jpeg),但某种方式不能做到这一点。

我创建的multiplot如下:

##define multiplot function 
    multiplot <- function(..., plotlist=NULL, cols) { 
     require(grid) 

     # Make a list from the ... arguments and plotlist 
     plots <- c(list(...), plotlist) 

     numPlots = length(plots) 

     # Make the panel 
     plotCols = cols       # Number of columns of plots 
     plotRows = ceiling(numPlots/plotCols) # Number of rows needed, calculated from # of cols 

     # Set up the page 
     grid.newpage() 
     pushViewport(viewport(layout = grid.layout(plotRows, plotCols))) 
     vplayout <- function(x, y) 
      viewport(layout.pos.row = x, layout.pos.col = y) 

     # Make each plot, in the correct location 
     for (i in 1:numPlots) { 
      curRow = ceiling(i/plotCols) 
      curCol = (i-1) %% plotCols + 1 
      print(plots[[i]], vp = vplayout(curRow, curCol)) 
     } 

    } 

## define subplots (short example here, I specified some more aesthetics in my script) 
plot1a <- qplot(variable1,variable2,data=Mydataframe1) 
plot1b <- qplot(variable1,variable3,data=Mydataframe1) 
plot1c <- qplot(variable1,variable2,data=Mydataframe2) 
plot1d <- qplot(variable1,variable3,data=Mydataframe2) 

## plot in one frame 
Myplot <- multiplot(plot1a,plot1b,plot1c,plot1d, cols=2) 

这给了期望的结果。当我尝试保存到文件时出现问题。我可以在RStudio中手动执行此操作(使用导出 - >将绘图另存为图像),但我希望在脚本中运行所有内容。我设法只保存subplot1d(这是last_plot()),而不是完整的多槽。

我试过到目前为止:

  1. 使用ggsave

    ggsave(filename = "D:/R/plots/Myplots.jpg") 
    

    这导致只有插曲1D被保存。

  2. 使用JPEG(),print()和dev.off()

    jpeg(filename = "Myplot.jpg", pointsize =12, quality = 200, bg = "white", res = NA, restoreConsole = TRUE) 
    print(Myplot) 
    dev.off() 
    

    这导致在全白图像(只是我假设的背景)。 print(Myplot)返回NULL。

不知道我在做什么错在这里。我缺乏理解R是我坚持试图找到解决方案的原因。任何人都可以解释我做错了什么,也许建议一种方法来解决我的问题?

+4

您也可以使用** gridExtra :: grid。排列(plot1a,plot1b,plot1c,plot1d,ncol = 2)** – dickoa 2012-07-30 12:32:11

+0

gridExtra软件包不包含grid.arrange,它包含了似乎提供相同功能的arrangeGrob。我似乎还记得一个名为grid.arrange的函数,也许作者更改了包中的函数标题。 – 2012-07-30 15:22:51

+0

'grid.arrange'应该在gridExtra中。可以肯定的是,'arrangeGrob' vs'multiplot'的好处之一是它与'ggsave'兼容,并且可以存储复合grob供以后使用。 – baptiste 2012-07-30 20:26:06

回答

18

它是因为Myplot是多功能函数的返回值,它什么都不返回(它的工作是打印图形)。您需要打开打开JPEG设备打开多槽:

jpeg(filename = "Myplot.jpg", pointsize =12, quality = 200, bg = "white", res = NA, restoreConsole = TRUE) 
multiplot(plot1a,plot1b,plot1c,plot1d, cols=2) 
dev.off() 

应该工作。

+0

真的很简单!非常感谢您解释为什么我的方法无效。 – 2012-07-30 12:43:58

17

使用example code(R食谱),它为我工作

png("chickweight.png") 
multiplot(p1, p2, p3, p4, cols=2) 
dev.off() 

enter image description here

+1

你是对的,但是你的代码与@M_Vermeulen略有不同,她首先绘制了4个图,期望'multiplot'返回一个网格对象。这可能是'grid.arrange'显示了这种行为? – 2012-07-30 12:44:31

+0

@PaulHiemstra:我不认为我在这里采取了不同的方法,请看一下示例页面。 @Spacedman解释了真正的问题。是的'grid.arrange'显示相同的行为 – dickoa 2012-07-30 14:16:05

+0

你的代码已经合并了@Spacedman的修复,多槽需要在调用'png'中。 – 2012-07-30 15:09:53

6

以及物品是否完整起见,ggsave不起作用,因为它仅在保存最后打印ggplot对象,你情况只是最后的情节。这是因为多槽创建绘图通过绘制ggplot对象到整个图形设备的不同子集的事实。另一种方法是通过将ggplot对象组合到一个大的ggplot对象中,然后打印该对象来创建绘图。这将与ggsave兼容。这种方法通过gridExtra包中的arrangeGrob实现。

+2

在技术上,'arrangeGrob'不会创建“一个大的ggplot对象”,它会返回一个“arrange”类的gTree,如果子列表中有ggplots,那么该类也会继承“ggplot”来愚弄ggsave 。 – baptiste 2012-07-30 20:29:11

+0

感谢您提供更详细的信息。 – 2012-07-31 04:48:17