在ggplot中使用geom_bar()显示频率而不是计数
•浏览 1
Display frequency instead of count with geom_bar() in ggplot
在此页面上,他们给出了以下示例
library(ggplot2)
library(reshape2)
ggplot(data=tips, aes(x=day)) + geom_bar(stat="bin")pp <- ggplot(data=tips, aes(x=day)) +
geom_bar(aes(y = (..count..)/sum(..count..)))library(scales)
pp + scale_y_continuous(labels = percent)ggplot(data=tips, aes(x=day)) +
geom_bar(aes(y = ..prop.., group = 1))
我希望在 y 轴上有一个频率,而不是计数。我怎样才能做到这一点?
这是可以在相关问题中找到的解决方案:
library(ggplot2)
library(reshape2)
ggplot(data=tips, aes(x=day)) + geom_bar(stat="bin")pp <- ggplot(data=tips, aes(x=day)) +
geom_bar(aes(y = (..count..)/sum(..count..)))library(scales)
pp + scale_y_continuous(labels = percent)ggplot(data=tips, aes(x=day)) +
geom_bar(aes(y = ..prop.., group = 1))
如果您想将频率标记为百分比,请添加以下内容(参见此处):
library(ggplot2)
library(reshape2)
ggplot(data=tips, aes(x=day)) + geom_bar(stat="bin")pp <- ggplot(data=tips, aes(x=day)) +
geom_bar(aes(y = (..count..)/sum(..count..)))library(scales)
pp + scale_y_continuous(labels = percent)ggplot(data=tips, aes(x=day)) +
geom_bar(aes(y = ..prop.., group = 1))
现在 ..prop.. 可用
library(ggplot2)
library(reshape2)
ggplot(data=tips, aes(x=day)) + geom_bar(stat="bin")pp <- ggplot(data=tips, aes(x=day)) +
geom_bar(aes(y = (..count..)/sum(..count..)))library(scales)
pp + scale_y_continuous(labels = percent)ggplot(data=tips, aes(x=day)) +
geom_bar(aes(y = ..prop.., group = 1))