gpt4 book ai didi

r - 计算ggplot中值的出现次数

转载 作者:行者123 更新时间:2023-12-04 13:40:51 30 4
gpt4 key购买 nike

我有以下数据:

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)

最佳答案

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)

关于r - 计算ggplot中值的出现次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40150490/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com