微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

具有错误百分比的多个变量的图形堆栈栏

如何解决具有错误百分比的多个变量的图形堆栈栏

我正在尝试获取多个变量的图形堆栈栏,以显示每个变量中每个级别的百分比,例如:

Category  <- c(rep(c("A","B","C","D"),times = 4))
Country  <- c(rep(c("Country A","Country B","Country C","Country D","Country F","Country G"),times = 16))
Data      <- data.frame(Category,Country)

ggplot(Data,aes(x=factor(""),fill=factor(Category)))+
  geom_bar(position="fill")+
  geom_text(aes(label=scales::percent(..count../sum(..count..))),stat='count',position=position_fill(vjust=0.5))

百分比正常

enter image description here

现在我重塑了我的数据框,但遗憾的是我无法得到预期的结果,图表显示错误的百分比:

library(tidyr)
dflong= gather(Data,variable,freq)

ggplot(dflong,aes(x=factor(variable),fill=factor(freq)))+
  geom_bar(aes(x=factor(variable)),position="fill")+
  geom_text(aes(label=scales::percent(..count../sum(..count..))),position=position_fill(vjust=0.5))

错误的百分比

enter image description here

解决方法

您可以在绘图前预先计算百分比。

library(dplyr)
library(ggplot2)

dflong %>%
  count(variable,freq,name = 'percentage') %>%
  group_by(variable) %>%
  mutate(percentage = prop.table(percentage) * 100) %>%
  ggplot() + aes(variable,percentage,fill = freq,label = paste0(round(percentage,2),'%')) + 
  geom_col() + 
  geom_text(position=position_stack(vjust=0.5))

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。