Note that there are some explanatory texts on larger screens.

plurals
  1. POPlotting multiple graphs in R/ggplot2 and saving the result
    text
    copied!<p>I am creating a list of (gg)plot objects and then trying to view all plots in a single graph. From the questions: <a href="https://stackoverflow.com/questions/6681145/how-can-i-arrange-an-arbitrary-number-of-ggplots-using-grid-arrange">How can I arrange an arbitrary number of ggplots using grid.arrange?</a>, and <a href="https://stackoverflow.com/questions/10706753/how-do-i-arrange-a-variable-list-of-plots-using-grid-arrange">How do I arrange a variable list of plots using grid.arrange?</a>, I was hoping that the following code would do the trick (<strong>Skip to the last few lines for the code that actually does the multi-plot thing</strong>).</p> <pre><code>#!/usr/bin/Rscript library(ggplot2) library(reshape) library(gridExtra) args &lt;- 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() </code></pre> <p>Instead, I hit the following error:</p> <pre><code>Error in arrangeGrob(..., as.table = as.table, clip = clip, main = main, : input must be grobs! Calls: do.call -&gt; &lt;Anonymous&gt; -&gt; grid.draw -&gt; arrangeGrob Execution halted </code></pre> <p>What am I doing wrong, especially since the two linked questions suggest the same thing? Also, what changes should be made to actually save the graph in a file?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload