Reputation: 26
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:
I've searched extensively and I'm unable to understand how to correctly transform the data to pass into ggplot.
Upvotes: 0
Views: 323
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))
Upvotes: 1