我正在创建一个(gg)绘图对象列表,然后尝试在单个图中查看所有绘图.从问题:如何使用grid.arrange安排任意数量的ggplots?,以及如何使用grid.arrange排列图表的变量列表?,我希望下面的代码可以解决这个问题(跳过最后几行代码实际执行多绘图的事情).
#!/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 ->-> grid.draw -> arrangeGrob Execution halted
我做错了什么,特别是因为两个相关的问题表明了同样的事情?此外,应该进行哪些更改才能将图形实际保存在文件中?