计算ggplot中值的出现次数

时间:2016-10-20 09:26:58

标签: r ggplot2

我有以下数据:

df = data.frame(c("2012","2012","2012","2013"),
                c("tuesday","tuesday","friday","thursday"),
                c("AAA","BBB","AAA","AAA"))
colnames(df) = c("year","day","type")

我想显示每年和每天type值(AAA,BBB)的出现次数(绝对频率)。 目前我编写了以下代码,但它要求我向aes添加一个维度,例如aes(type, some_dimension, fill = as.factor(year))。那么,我该如何添加count(type)

之类的内容
ggplot(dat) +
  geom_bar(aes(type, fill = as.factor(year)), 
           position = "dodge", stat = "identity") + 
  facet_wrap(~day)

1 个答案:

答案 0 :(得分:2)

geom_barstat"identity"更改为"count",就像这里一样:

library("ggplot2")

ggplot(df) +
    geom_bar(aes(x = type, fill = as.factor(year)), 
             position = "dodge", stat = "count") + 
    facet_wrap(~day)