R:在列表中保存 ggplot2 图

标签 r ggplot2 histogram

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

我知道这个问题已经很老了,我在 renaming ggplot2 graphs in a for loop 上找到了答案。和 https://stat.ethz.ch/pipermail/r-help/2008-February/154438.html成为一个有用的起点。

我使用了 R 中可用的标准 Swiss Fertility 数据集来生成图。这是代码:-

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

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

谢谢!

最佳答案

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

  • 使用分面,而不是手动排列多个图
  • 用函数 melt 融化你的数据包装内reshape2
  • 这意味着您可以删除循环

  • 这是对您的代码的完全重写,看不到任何循环。
    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)
    

    结果图形:

    概率:

    enter image description here

    频率

    enter image description here

    关于R:在列表中保存 ggplot2 图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11357139/

    相关文章:

    matlab - 在Matlab中使用 "imhist"函数在同一个图形上绘制多个直方图

    java - 如何显示具有不同颜色的单个直方图条

    Python Matplotlib 直方图颜色

    regex - R 中的变音符号和正则表达式

    r - 如何在 RMarkdown html 中为 ggplot 图添加水平滚动条

    R ggplot2 geom_smooth - 单调平滑函数

    r - ggplot2如何支持循环绘图?

    r - R中的箱线图显示平均值

    r - 如何清除 rJava 使用的内存?

    r - ggplot geom_point 忽略未知美学 : text issue?