【问题标题】:Stack multiple columns of df in bar/col plot and create %在 bar/col 图中堆叠多列 df 并创建 %
【发布时间】:2021-02-24 16:35:55
【问题描述】:

this 问题的续集。我有以下df

df <- data.frame(genre = c("Thriller", "Horror", "Action"), europe = c(195, 210, 300), asia = c(130, 90, 150), america = c(325, 300, 150))

    genre europe asia america
1 Thriller    195  130     325
2   Horror    210   90     300
3   Action    300  150     150

我想知道剧情

【问题讨论】:

    标签: r dataframe ggplot2


    【解决方案1】:

    你长轴,计算比例,关键是使用粘贴动态制作标签:

    df %>% 
    pivot_longer(-genre) %>% 
    group_by(genre) %>% 
    mutate(p=100*value/sum(value)) %>% 
    ggplot(aes(x=genre,y=p,fill=name)) + 
    geom_col() + 
    geom_text(aes(label=paste(p,"%","(",value,")")),
    position = position_stack(vjust = .5))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-16
      • 2014-02-13
      • 2012-10-10
      • 2016-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多