以这个数据集为例:
demo <- tribble(
~cut, ~freq,
"Fair", 1610,
"Good", 4906,
"Very Good", 12082,
"Premium", 13791,
"Ideal", 21551
)
#There is no difference between this:
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, group = 1), stat = "count")
#and this:
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, y = stat(count), group = 1))但是,如果将"count“替换为"prop",则只有后面的命令有效,前者返回一个错误。为什么会这样呢?为什么它只适用于计数而不是道具?
任何帮助都将不胜感激!
发布于 2022-03-16 14:48:44
count和prop是geom_bar在使用stat="count"时计算出来的两个变量,可以通过after_stat()、stat()或..count../..prop..达到。
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, y = after_stat(count), group = 1), stat= "count")+
geom_text(mapping = aes(x = cut, y = stat(count),label=round(..prop..,2),vjust=-1, group = 1),stat="count")

另一种选择是使用stat="identity",在这种情况下,不计算prop和count,因为值保持不变,不计算。
没有stat="prop",因此您得到了错误消息。
https://stackoverflow.com/questions/71498001
复制相似问题