在堆积的条形图上按列显示百分比 [英] Display percentage by column on a stacked bar graph

查看:214
本文介绍了在堆积的条形图上按列显示百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试绘制一个堆积的条形图,以显示一列中每个组的相对百分比.

I'm trying to plot a stacked bar chart showing the relative percentages of each group within a column.

使用默认的mpg数据集,这是我的问题的说明:

Here's an illustration of my problem, using the default mpg data set:

mpg %>%
  ggplot(aes(x=manufacturer, group=class)) +
  geom_bar(aes(fill=class), stat="count") +
  geom_text(aes(label=scales::percent(..prop..)),
    stat="count",
    position=position_stack(vjust=0.5))

这是输出:

我的问题是,此输出显示每个类别相对于总计的百分比,而不是每个制造商中的相对百分比.

My problem is that this output shows the percentage of each class against the grand total, not the relative percentage within each manufacturer.

例如,我希望第一列(audi)显示棕色(紧凑)为83.3%(15/18),绿色(中型)显示16.6%(3/18).

For example, I want the first column (audi) to show 83.3% (15/18) for brown (compact) and 16.6% (3/18) for green (midsize).

我在这里发现了一个类似的问题: 如何在ggplot2显示基于组的百分比?

I found a similar question here: How to draw stacked bars in ggplot2 that show percentages based on group?

但是我想知道在ggplot2中是否有一种更简单的方法,特别是因为我的实际数据集使用一堆dplyr管道对数据进行按摩之后才最终将其输送到ggplot2中.

But I wanted to know if there's an easier way to do this within ggplot2, especially since my actual dataset uses a bunch of dplyr pipes to massage the data before ultimately piping it into ggplot2.

推荐答案

如果我将您的问题与您提供的链接进行比较,则不同之处在于该链接算"出了自己.那就是我所做的.我不确定这是否适合您的真实数据.

If I compare your question to the link you gave than the difference is that the link "counted" them selves. That's what I did. I'am nor sure if this is than suitable for your real data.

library(ggplot2)
library(dplyr)

mpg %>%
  mutate(manufacturer = as.factor(manufacturer),
         class = as.factor(class)) %>%
  group_by(manufacturer, class) %>%
  summarise(count_class = n()) %>%
  group_by(manufacturer) %>%
  mutate(count_man = sum(count_class)) %>%
  mutate(percent = count_class / count_man * 100) %>%
  ggplot() +
  geom_bar(aes(x = manufacturer,
               y = count_man, 
               group = class,
               fill = class), 
           stat = "identity") +
  geom_text(aes(x = manufacturer,
                y = count_man,
                label = sprintf("%0.1f%%", percent)),
            position = position_stack(vjust = 0.5))

根据评论进行

我为y

library(ggplot2)
library(dplyr)

mpg %>%
  mutate(manufacturer = as.factor(manufacturer),
         class = as.factor(class)) %>%
  group_by(manufacturer, class) %>%
  summarise(count_class = n()) %>%
  group_by(manufacturer) %>%
  mutate(count_man = sum(count_class)) %>%
  mutate(percent = count_class / count_man * 100) %>%
  ungroup() %>%
  ggplot(aes(x = manufacturer,
             y = count_class,
             group = class)) +
  geom_bar(aes(fill = class), 
           stat = "identity") +
  geom_text(aes(label = sprintf("%0.1f%%", percent)),
            position = position_stack(vjust = 0.5))

这篇关于在堆积的条形图上按列显示百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆