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

问题描述

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

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