【问题标题】:R: saving ggplot2 plots in a listR:将ggplot2图保存在列表中
【发布时间】:2012-07-06 14:43:37
【问题描述】:

我正在编写一个 R 代码,它允许用户从数据中选择列并为每个列绘制直方图。因此,我使用 'for' 循环使用 ggplot2 库生成所需数量的图并将它们保存在单个列表中。但我面临的问题是,在“for”循环的每次迭代中,列表中的所有对象都存储相同的图。因此,最终输出由直方图网格组成,标记不同但描绘相同(最后)列。

我知道这个问题已经很老了,我发现 renaming ggplot2 graphs in a for loophttps://stat.ethz.ch/pipermail/r-help/2008-February/154438.html 上的答案是一个有用的起点。

我使用 R 中可用的标准瑞士生育力数据集来生成图。这是代码:-

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()

如果我错过了这个论坛中问题的明显答案,我深表歉意,如果您能指导我解决这个问题,我将不胜感激。我希望我的解释很清楚,如果没有,请告诉我所需的说明。

谢谢!

【问题讨论】:

    标签: r ggplot2 histogram


    【解决方案1】:

    您最好使用aes_string,而不是使用aes 绘制美学:

     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')
     }
    

    至少对我有用。这避免了对数据进行子集化,并允许您通过引用名称引用要绘制的列。

    【讨论】:

      【解决方案2】:

      您可以通过以下方式大大简化您的代码:

      1. 使用构面,而不是手动排列多个图
      2. 使用包reshape2 中的函数melt 融合数据
      3. 这意味着您可以移除循环

      这是对代码的完全重写,看不到循环。

      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)
      

      生成的图形:

      概率:

      频率

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-02-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多