【问题标题】:Graph stack bar for multiple variables with wrong percentages具有错误百分比的多个变量的图形堆栈栏
【发布时间】:2021-04-14 17:10:17
【问题描述】:

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

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))

百分比还可以

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

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..))), stat='count',position=position_fill(vjust=0.5))

错误的百分比

【问题讨论】:

    标签: r ggplot2 reshape geom-bar


    【解决方案1】:

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

    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))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-23
      • 2020-04-18
      • 1970-01-01
      • 2021-09-07
      • 2017-11-29
      • 2018-12-09
      • 2021-05-27
      • 1970-01-01
      相关资源
      最近更新 更多