我有以下数据:
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)
答案 0 :(得分:2)
在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)