I am running R version 3.1.1. in RStudio and am having difficulties with grid.arrange.
I am reading sales data for over 100 stores and plotting the sales over time. I am also trying to group the plots to display a set number at a time.
However, after many different attempts, the command
do.call("grid.arrange", c(plotlist, ncol=nCol))
results in all plots in the plot matrix being identical. Further, plotting individual items from the plotlist demonstrates that all plots are stored as identical elements in the plotlist as well.
Here is code that duplicates the issue:
require(ggplot2)
require(gridExtra)
wss <- data.frame(ind=1:10, dep=(1:10))
plotlist <- list()
for (i in 1:4) {
wss <- data.frame(ind=wss$ind, dep=(i*wss$dep))
plotname <- paste0("Store", i, sep="")
plotlist[[ plotname ]] <- ggplot(data=wss, aes(x=wss$ind, y=wss$dep)) + geom_line() + theme_bw() + ggtitle(paste0("Store #",i, sep="")) + ylab("Sales Revenue") + theme(axis.title.x=element_blank())
}
n <- length(plotlist)
nCol <- floor(sqrt(n))
do.call("grid.arrange", c(plotlist, ncol=nCol))