R 使用几何图形栏在缩放范围内打印比例

R 使用几何图形栏在缩放范围内打印比例,r,plot,ggplot2,geom-bar,R,Plot,Ggplot2,Geom Bar,我在创建比例条形图时遇到问题 这是我当前的代码: 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 = per

我在创建比例条形图时遇到问题

这是我当前的代码:

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()
这就产生了:


但是,这会缩放条形图,使总和为100%。我想要的是鲑鱼条加起来是100%,而水族条加起来是100%。换句话说,我对缩放范围内的比例感兴趣。除了进入我的数据和处理我的变量之外,还有什么办法可以用ggplot做到这一点吗?

截至2017年3月,使用ggplot2.2.1,我认为最好的解决方案在Hadley Wickham的R for data science一书中进行了解释:

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()
统计计数计算两个变量:默认情况下使用计数,但您可以选择使用显示比例的道具

在以下位置找到原始答案: