2013-12-11 51 views
2

我创建了一个(gg)图形对象列表,然后尝试查看单个图形中的所有图形。从问题:How can I arrange an arbitrary number of ggplots using grid.arrange?How do I arrange a variable list of plots using grid.arrange?,我希望下面的代码可以做到这一点(跳到最后几行代码,实际上是多图事)。在R/ggplot2中绘制多个图并保存结果

#!/usr/bin/Rscript 
library(ggplot2) 
library(reshape) 
library(gridExtra) 
args <- commandArgs(TRUE); 
# Very first argument is the directory containing modelling results 
setwd(args[1]) 
df = read.table('model_results.txt', header = TRUE) 
df_actual = read.table('measured_results.txt', header = TRUE) 
dfMerge = merge(df, df_actual, by = c("Clients", "MsgSize", "Connections")) 

# All graphs should be stored in the directory pointed by second argument 
setwd(args[2]) 

# Plot the results obtained from model for msgSize = 1, 1999 
# and connections = 10, 15, 20, 25, 30, 50 

msgSizes = c(1, 1999) 
connVals = c(5, 10, 15, 20, 25, 30, 50) 

cmp_df = data.frame(dfMerge$Clients, dfMerge$MsgSize, dfMerge$Connections, dfMerge$PThroughput, dfMerge$MThroughput) 
colnames(cmp_df) = c("Clients", "MsgSize", "Connections", "ModelThroughput", "MeasuredThroughput") 
cmp_df_melt = melt(cmp_df, id = c("Clients", "MsgSize", "Connections")) 
colnames(cmp_df_melt) = c("Clients", "MsgSize", "Connections", "Variable", "Value") 

plotList = list() 
for (i in msgSizes) { 
    msg_Subset = subset(cmp_df_melt, MsgSize == i) 

    for (j in connVals) { 
     plotData = subset(msg_Subset, Connections == j) 

     filename = paste("Modelling.",i, ".", j, ".png", sep = '') 
     list_item = ggplot(data = plotData, aes(Clients, y = Value, color = Variable)) + 
      geom_point() + 
      geom_line() + 
      xlab("Number of Clients") + 
      ylab("Throughput (in ops/second)") + 
      labs(title = paste("Message Size = ", i, ", Connections = ", j), color = "Legend") 

     plotList = c(plotList, list_item) 
     # ggsave(file = filename) 

    } 
} 

# Plot all graphs together 
pdf("Summary.pdf") 
list_len = length(plotList) 
nCol = floor(sqrt(list_len)) 
do.call(grid.arrange, c(plotList, list(ncol = nCol))) 
dev.off() 

相反,我打了以下错误:

Error in arrangeGrob(..., as.table = as.table, clip = clip, main = main, : 
    input must be grobs! 
Calls: do.call -> <Anonymous> -> grid.draw -> arrangeGrob 
Execution halted 

我在做什么错,特别是因为两个相连的问题提出了同样的事情?另外,应该做些什么改变才能将图形保存在文件中?

+0

不能真正考验你的代码,因为你没有提供的样本数据,但它看起来像在您引用'plotList'的例子是一个列表。 –

+0

疯狂的猜测,但也许更改为'do.call(grid.arrange,c(plotList,ncol = nCol))'会有帮助吗? – TheComeOnMan

+0

另外,试试'lapply(plotlist,class)'来确认每个元素是否实际上是一个阴谋。以防万一。 – TheComeOnMan

回答

4

正如其他人所说,由于您不提供样本数据,因此无法测试您的代码。但是,grid.arrange(...)需要包含grobsggplot对象的列表。您正在提供一个列表,其中包含一些ggplot对象和一个数字。你试过这个吗?

do.call(grid.arrange, plotlist) # untested 
5

这是您的问题的一个最小的,可重复的例子。它再现了错误消息,并且可以由任何感兴趣的用户通过将代码粘贴到新的R会话中轻松运行。该错误是由plot_list = c(plot_list, new_plot)的意外行为引起的。

library(ggplot2) 
library(gridExtra) 

dat = data.frame(x=1:10, y=1:10) 

plot_list = list() 
nplot = 3 

for (i in seq(nplot)) { 
    new_plot = ggplot(dat, aes(x=x, y=y)) + 
       geom_point() + 
       labs(title=paste("plot", i)) 
    plot_list = c(plot_list, new_plot) 
} 

png("plots.png", width=10, height=5, units="in", res=150) 
do.call(grid.arrange, c(plot_list, list(ncol=nplot))) 
dev.off() 
# Error in arrangeGrob(..., as.table = as.table, clip = clip, main = main, : 
# input must be grobs! 

错误是由包装new_plotlist()解决:

plot_list = c(plot_list, list(new_plot)) 

enter image description here

相关问题