2013-12-11 50 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

相關問題