计算 ggplot 中值出现的次数

Fio*_*anS 3 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")
Run Code Online (Sandbox Code Playgroud)

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)
Run Code Online (Sandbox Code Playgroud)

Mar*_*rta 5

geom_bar改为stat,如下所示"identity""count"

library("ggplot2")

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