【问题标题】:Multiple histograms in ggplot2ggplot2中的多个直方图
【发布时间】:2011-09-06 04:54:17
【问题描述】:

这是我的一小部分数据:

dat <-structure(list(sex = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("male", 
"female"), class = "factor"), A = c(1, 2, 0, 2, 1, 2, 2, 0, 2, 
0, 1, 2, 2, 0, 0, 2, 0, 0, 0, 2), B = c(0, 0, 0, 0, 0, 2, 0, 
0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0), C = c(1, 2, 1, 0, 0, 
2, 1, 1, 0, 1, 1, 0, 1, 2, 1, 2, 0, 2, 1, 2), D = c(2, 2, 0, 
2, 2, 2, 1, 0, 1, 1, 1, 0, 1, 2, 0, 0, 1, 1, 1, 0), E = c(0, 
0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 1, 2, 2), F = c(2, 
2, 1, 2, 1, 2, 2, 0, 1, 2, 0, 1, 2, 2, 0, 1, 2, 2, 2, 2)), .Names = c("sex", 
"A", "B", "C", "D", "E", "F"), variable.labels = structure(c("sex", 
"zenuwac", "panieke", "gespann", "rustelo", "angstig", "onzeker"
), .Names = c("sex", "anx01", "anx02", "anx03", "anx04", "anx05", 
"anx06")), codepage = 20127L, row.names = c(NA, 20L), class = "data.frame")

一个数据框,其中包含六个 3 点变量上的男性和女性得分。现在我想创建一个图,显示网格中男性和女性每个变量的得分直方图。例如,我可以这样做:

layout(matrix(1:12,6,2,byrow=TRUE))
par(mar=c(2,1,2,1))
for (i in 1:6) for (s in c("male","female")) hist(dat[dat$sex==s,i+1],main=paste("item",names(dat)[i+1],s))

导致:

我可以让它看起来更好,但我对学习如何使用 ggplot2 更感兴趣。所以我的问题是,如何使用 ggplot2 创建一个漂亮的版本?我开始做的一件事是:

library("ggplot2")
grid.newpage()
pushViewport(viewport(layout = grid.layout(6, 2)))   
for (s in 1:2)
{
    for (i in 1:6)
    {
        p <- qplot(dat[dat$sex==c("male","female")[s],i+1]+0.5, geom="histogram", binwidth=1)
        print(p, vp = viewport(layout.pos.row = i, layout.pos.col = s))
    }
}

但我想有更简单的方法可以做到这一点?

【问题讨论】:

    标签: r ggplot2 histogram


    【解决方案1】:

    您可以尝试gridExtra 包中的grid.arrange();即将你的图存储在一个列表中(比如qplt),然后使用

    do.call(grid.arrange, qplt)
    

    其他想法:在 ggplot2 (sex*variable) 中使用分面,通过考虑 data.frame(使用 melt)。

    作为旁注,最好使用堆叠条形图或克利夫兰的点图来显示项目响应频率,IMO。 (我在CrossValidated上给出了一些想法。)


    为了完整起见,这里有一些实现思路:

    # simple barchart
    ggplot(melt(dat), aes(x=as.factor(value), fill=as.factor(value))) + 
      geom_bar() + facet_grid (variable ~ sex) + xlab("") + coord_flip() + 
      scale_fill_discrete("Response")
    

    my.df <- ddply(melt(dat), c("sex","variable"), summarize, 
                   count=table(value))
    my.df$resp <- gl(3, 1, length=nrow(my.df), labels=0:2)
    
    # stacked barchart
    ggplot(my.df, aes(x=variable, y=count, fill=resp)) + 
      geom_bar() + facet_wrap(~sex) + coord_flip()
    

    # dotplot
    ggplot(my.df, aes(x=count, y=resp, colour=sex)) + geom_point() + 
      facet_wrap(~ variable)
    

    【讨论】:

      【解决方案2】:

      跟进 chl 的示例 - 这是使用 ggplot 复制基本图形的方法。在寻找点图时,我也会听从他的建议:

      library(ggplot2)
      dat.m <- melt(dat, "sex") 
      
      ggplot(dat.m, aes(value)) + 
        geom_bar(binwidth = 0.5) + 
        facet_grid(variable ~ sex)
      

      【讨论】:

      • (+1) 好的,我最终得到了类似ggplot(subset(melt(dat), as.numeric(variable)==i), aes(x=as.factor(value))) + geom_bar() + facet_grid (. ~ sex) + xlab("") 的东西(在variable 的for 循环内)。将value 转换为因子可以避免嘈杂的 x 单位。
      • @chl - 很好地将value 转换为因子。我不确定 OPs 的完整数据集是否会填补尴尬的空白,但无论如何都要袖手旁观。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-31
      • 1970-01-01
      • 2013-06-20
      • 1970-01-01
      • 2013-08-23
      • 1970-01-01
      • 2018-08-02
      相关资源
      最近更新 更多