![](/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.