在R / ggplot2中绘制多个图并保存结果

时间:2013-12-11 22:49:45

标签: r ggplot2

我正在创建一个(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?开始,我希望以下代码可以解决这个问题(跳过实际执行多情节事务的代码的最后几行< /强>)。

#!/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

我做错了什么,特别是因为两个相关的问题表明了同样的事情?另外,应该对图形实际保存在文件中进行哪些更改?

2 个答案:

答案 0 :(得分: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_plot

包装list()来解决错误
plot_list = c(plot_list, list(new_plot))

enter image description here

答案 1 :(得分:4)

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

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