[英]Plotting proportions within a scaling with geom_bar
I am having an issue with creating a bar graph of proportions. 我在创建比例条形图时遇到问题。
This is my current code: 这是我当前的代码:
ggplot(data = d,
aes(fill = version)) +
theme(legend.position = 'bottom') +
geom_bar(aes(x = cregion,
y = ..count../sum(..count..)),
position = 'dodge') +
scale_y_continuous(labels = percent) +
coord_flip()
Which produces this: 产生此:
This scales the bars so that the total sum is 100%, however. 这样会缩放条形图,以使总和为100%。 What I want is for the salmon bars to sum to 100%, and also for the aqua bars to sum to 100%.
我想要的是鲑鱼条的总和为100%,而水色条的总和为100%。 In other words, I am interested in the proportions within the scaling.
换句话说,我对缩放比例内的比例感兴趣。 Is there a way to do this with ggplot besides just going into my data and mucking around with my variables?
除了只进入我的数据并弄乱我的变量之外,还有没有办法用ggplot做到这一点?
As of March 2017, with ggplot2 2.2.1 I think the best solution is explained in Hadley Wickham's R for data science book: 截至2017年3月,使用ggplot2 2.2.1,我认为最好的解决方案在Hadley Wickham的R for data science book中得到了解释:
ggplot(data = d,aes(x=cregion)) +
stat_count(aes(y=..prop.., group=version, fill = version, position = "dodge"))+
theme(legend.position = "bottom")+
scale_y_continuous(labels = percent) +
coord_flip()
stat_count computes two variables: count is used by default, but you can choose to use prop which shows proportions. stat_count计算两个变量:默认使用count,但您可以选择使用显示比例的prop。
Find the original answer at Show % instead of counts in charts of categorical variables 在显示百分比而不是分类变量图表中的计数处找到原始答案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.