Show percentage by column on stacked histogram

I am trying to build a complex histogram showing the relative percentages of each group within a column.

Here is an illustration of my problem using the default mpg dataset:

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

And this is the result: enter image description here

My problem is that this result shows the percentage of each class to the total, and not the relative percentage within each manufacturer.

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

I found a similar question here: How to draw stacked columns in ggplot2 showing percentages for a group?

, ggplot2, , dplyr- , ggplot2.

+4
2

, , , "" . , . , .

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

, , , :

round(prop.table(table(mpg$class, mpg$manufacturer), margin = 2), 3) * 100

#             audi chevrolet dodge  ford honda hyundai  jeep land rover lincoln mercury nissan pontiac subaru toyota volkswagen
# 2seater      0.0      26.3   0.0   0.0   0.0     0.0   0.0        0.0     0.0     0.0    0.0     0.0    0.0    0.0        0.0
# compact     83.3       0.0   0.0   0.0   0.0     0.0   0.0        0.0     0.0     0.0   15.4     0.0   28.6   35.3       51.9
# midsize     16.7      26.3   0.0   0.0   0.0    50.0   0.0        0.0     0.0     0.0   53.8   100.0    0.0   20.6       25.9
# minivan      0.0       0.0  29.7   0.0   0.0     0.0   0.0        0.0     0.0     0.0    0.0     0.0    0.0    0.0        0.0
# pickup       0.0       0.0  51.4  28.0   0.0     0.0   0.0        0.0     0.0     0.0    0.0     0.0    0.0   20.6        0.0
# subcompact   0.0       0.0   0.0  36.0 100.0    50.0   0.0        0.0     0.0     0.0    0.0     0.0   28.6    0.0       22.2
# suv          0.0      47.4  18.9  36.0   0.0     0.0 100.0      100.0   100.0   100.0   30.8     0.0   42.9   23.5        0.0
+1

Source: https://habr.com/ru/post/1675119/


All Articles