R: saving ggplot2 plots in a list

只谈情不闲聊 提交于 2019-12-18 03:57:19

问题


I am writing a R code that allows users to select columns from a data and plots histograms for each of them. Hence, I am using a 'for' loop to generate the required number of plots using the ggplot2 library and save them in a single list. But the problem I am facing is that, at every iteration of the 'for' loop, all objects in the list are storing the same plot. Thus, the final output consists of a grid of histograms, labeled differently but depicting the same(last) column.

I understand that this question is quite old and I found the answers on renaming ggplot2 graphs in a for loop and https://stat.ethz.ch/pipermail/r-help/2008-February/154438.html to be a useful starting point.

I have used the standard Swiss Fertility dataset available in R to generate the plots. Here is the code:-

data_ <- swiss
data_ <- na.omit(data_)

u <- c(2, 3, 4, 5, 6)
plotData <- data_[,u]
bw <- 5
plotType <- 'probability'

library(ggplot2)
library(gridExtra)

histogramList <- vector('list', length(u))

if(plotType=='probability')
{
 for(i in 1:length(u))
 {
   indexDataFrame <- data.frame(plotData[,i])
   probabilityHistogram <- ggplot(indexDataFrame, aes(x=indexDataFrame[,1]))
   histogramList[[i]] <-  probabilityHistogram + geom_histogram(aes(y=..density..),     binwidth=bw, colour='black', fill='skyblue') + geom_density() + scale_x_continuous(names(plotData)[i]) + opts(legend.position='none')
 }
} else
if(plotType=='frequency')
{
 for(i in 1:length(u))
 {
   indexDataFrame <- data.frame(plotData[,i])
   probabilityHistogram <- ggplot(indexDataFrame, aes(x=indexDataFrame[,1]))
   histogramList[[i]] <- probabilityHistogram + geom_histogram(aes(y=..count..), binwidth=bw, colour='black', fill='skyblue') + geom_density() + scale_x_continuous(names(plotData)[i]) + opts(legend.position='none')
 }
}

arg_list <- c(histogramList, list(nrow=3, ncol=2))
#jpeg('histogram', width=1024, height=968)
do.call(grid.arrange, arg_list)
#graphics.off()

I apologize if I have missed an obvious answer to the question in this forum and shall be grateful if you could direct me towards it. I hope my explanation is clear and if not, please let me know about the clarifications required.

Thanks!


回答1:


You can vastly simplify your code by:

  1. Using facets, rather than manually arranging multiple plots
  2. Melting your data with the function melt in package reshape2
  3. This means you can remove the loop

Here is a complete rewrite of your code, with no loop in sight.

data_ <- swiss
data_ <- na.omit(data_)

u <- c(2, 3, 4, 5, 6)
plotData <- data_[,u]
bw <- 5
plotType <- 'frequency'

library(ggplot2)
library(reshape2)

mdat <- melt(plotData)

if(plotType=='probability'){
  ph <- ggplot(mdat, aes(value)) +
    geom_histogram(aes(y=..density..), binwidth=bw, colour='black', fill='skyblue') + 
    geom_density() + 
    facet_wrap(~variable, scales="free")
} 

if(plotType=='frequency'){
  ph <- ggplot(mdat, aes(value)) +
    geom_histogram(aes(y=..count..), binwidth=bw, colour='black', fill='skyblue') + 
    geom_density() + 
    facet_wrap(~variable, scales="free")
}

print(ph)

The resulting graphics:

Probability:

Frequency




回答2:


Instead of mapping aesthetics using aes, you might be better off using aes_string:

 for(i in 1:length(u))
 {
   probabilityHistogram <- ggplot(plotData, aes_string(x=names(plotData)[i]))
   histogramList[[i]] <-  probabilityHistogram + geom_histogram(aes(y=..density..),     binwidth=bw, colour='black', fill='skyblue') + geom_density() + scale_x_continuous(names(plotData)[i]) + opts(legend.position='none')
 }

That worked for me, at least. This avoids having to subset your data and allows you to reference the column you want to plot by quoted name.



来源:https://stackoverflow.com/questions/11357139/r-saving-ggplot2-plots-in-a-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!