使用已汇总的数据在ggplot2上生成条形图

时间:2012-05-30 16:51:48

标签: r ggplot2

  

可能重复:
  Create a bar graph with pre-summarized data using ggplot2

关于ggplot2参考手册 http://had.co.nz/ggplot2/geom_bar.html

我们可以看到使用提供的钻石数据可以很容易地叠加条形图。

但是,我尝试使用的数据集与钻石数据不同,因为我已经对其进行了总结。

附件是我的意思的最小例子

   AgeGroup gender Level.of.Education  status count
1     18-25   male        High School married    10
2     18-25   male        High School  single    20
3     18-25   male            College married    25
4     18-25   male            College  single    10
5     18-25   male                PhD married    50
6     18-25   male                PhD  single     4
7     18-25   male                GED married    20
8     18-25   male                GED  single   100
9     18-25 female        High School married    20
10    18-25 female        High School  single    10
11    18-25 female            College married    30
12    18-25 female            College  single    60
13    18-25 female                PhD married    80
14    18-25 female                PhD  single    10
15    18-25 female                GED married     5
16    18-25 female                GED  single     2

完整的数据集也有时间表,所以我想要制作的是一个堆积的条形图,显示已婚人数和未婚人数加班,使用“教育水平”作为条形填充。

完整数据集太大,以至于非常需要像这样的聚合数据。

您的意见对我很有帮助。感谢。

1 个答案:

答案 0 :(得分:9)

您需要stat='identity'

ggplot(dat, aes(x=status, y=count, fill=Level.of.Education)) +
    geom_bar(stat='identity')

我还建议按性别分面:

ggplot(dat, aes(x=status, y=count, fill=Level.of.Education)) +
    geom_bar(stat='identity') +
    facet_wrap(~gender)