![](/img/trans.png)
[英]grouped bar plot with ggplot2 geom_bar is plotting proportions not counts
[英]ggplot2: plot correct proportions using geom_bar
我正在尝试使用geom_bar
和position = "dodge"
绘制钻石的比例。 这是我所做的。
library(ggplot2)
ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut))
下图告诉我每种cut
类型有多少颗钻石。
现在,让我们做一些花哨的事情。
ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, fill = clarity), position = "dodge")
下图提供了按每种cut
类型的clarity
将钻石分组的计数。
我想做的是得到与上述相同的躲避图,但显示比例而不是数量 。
例如,对于cut=ideal
且clarity = VS2
,该比例应为5071/21551 = 0.23
。
创建具有正确百分比(称为“百分比”)的列,然后使用
require(ggplot2)
require(scales)
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, y = percentage, fill = clarity), position = "dodge") +
scale_y_continuous(labels = scales::percent)
您还可以按照Maurits Evers的建议计算内联百分比。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.