简体   繁体   English

ggplot2 中的多个直方图

[英]Multiple histograms in ggplot2

Here is a short part of my data:这是我数据的一小部分:

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

A data frame with scores of males and females on six 3-point variables.在六个 3 点变量上包含男性和女性得分的数据框。 Now I want to create a plot which shows the histograms of the scores of each variable of both males and females in a grid.现在我想创建一个 plot 以显示网格中男性和女性的每个变量的得分直方图。 For example, I can do:例如,我可以这样做:

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

which results in:这导致:

带有基本 R 图形的直方图

I could make this look better but I am more interested in learning how to use ggplot2.我可以让它看起来更好,但我对学习如何使用 ggplot2 更感兴趣。 So my question is, how do I create a pretty version of this using ggplot2?所以我的问题是,如何使用 ggplot2 创建一个漂亮的版本? One thing I got working is:我开始工作的一件事是:

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

But I guess there is a much easier way to do this?但我想有一种更简单的方法可以做到这一点?

You can try grid.arrange() from the gridExtra package;您可以从gridExtra package 尝试grid.arrange() ie, store your plots in a list (say qplt ), and use即,将您的图存储在列表中(例如qplt ),然后使用

do.call(grid.arrange, qplt)

Other ideas: use facetting within ggplot2 ( sex*variable ), by considering a data.frame (use melt ).其他想法:通过考虑 data.frame(使用melt ),在 ggplot2 ( sex*variable )中使用 facetting 。

As a sidenote, it would be better to use stacked barchart or Cleveland's dotplot for displaying items response frequencies, IMO.作为旁注,最好使用堆叠条形图或克利夫兰的点图来显示项目响应频率,IMO。 (I gave some ideas on CrossValidated .) (我在CrossValidated上给出了一些想法。)


For the sake of completeness, here are some implementation ideas:为了完整起见,这里有一些实现思路:

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

在此处输入图像描述

To follow up on chl's example - here's how to duplicate your base graphic with ggplot.跟进 chl 的示例 - 这是使用 ggplot 复制基本图形的方法。 I would heed his advice in looking to dotplots as well:在寻找点图时,我也会听从他的建议:

library(ggplot2)
dat.m <- melt(dat, "sex") 

ggplot(dat.m, aes(value)) + 
  geom_bar(binwidth = 0.5) + 
  facet_grid(variable ~ sex)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM