Marcelo
Marcelo

Reputation: 26

R ggplot stacked horizontal percentage barplot with multiple categorical variables

I need a percentage stacked horizontal barplot where each bar is a <fct>variable of a dataframe.

Data example is:

    A       B       C 
    <fct>   <fct>   <fct>
1   Sim     Não     Não 
2   Sim     Não     Sim 
3   Sim     Não     Sim 
4   NA      Sim     Sim 
5   Não     Não     Não 

Example of the outcome would be:

Stacked Bar Graph

I've searched extensively and I'm unable to understand how to correctly transform the data to pass into ggplot.

Upvotes: 0

Views: 323

Answers (1)

Allan Cameron
Allan Cameron

Reputation: 174516

You could do

library(tidyverse)

df %>%
  pivot_longer(everything()) %>%
  group_by(name) %>%
  mutate(group_size = n()) %>%
  group_by(name, value) %>%
  summarize(group_percent = n() / mean(group_size)) %>%
  ggplot(aes(y = name, x = group_percent, fill = value)) +
  geom_col(width = 0.6, position = position_fill()) +
  geom_text(aes(label = scales::percent(group_percent)),
            position = position_fill(vjust = 0.5)) +
  scale_fill_manual(values = c("#1f77b4", "#ff7f0e"), na.value = 'green4') +
  theme_classic(base_size = 16) +
  scale_x_continuous('Percent', labels = scales::percent, expand = c(0, 0)) +
  coord_fixed(0.1) +
  theme(panel.border = element_rect(fill = NA))

enter image description here

Upvotes: 1

Related Questions