Skip to content

Instantly share code, notes, and snippets.

@ryanburge
Last active December 31, 2023 16:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanburge/07044fa4e0e5cfeb32c87ad44d90a658 to your computer and use it in GitHub Desktop.
Save ryanburge/07044fa4e0e5cfeb32c87ad44d90a658 to your computer and use it in GitHub Desktop.
Stacked Bars Flipped
gg %>%
filter(trad2 != "NA") %>%
mutate(lab = round(pct, 2)) %>%
ggplot(., aes(x = 1, y = pct, fill = fct_rev(age2))) +
geom_col(color = "black") +
coord_flip() +
facet_wrap(~ trad2, ncol =1, strip.position = "left") +
theme_rb() +
scale_fill_manual(values = c(moma.colors("OKeeffe", 5))) +
theme(legend.position = "bottom") +
scale_y_continuous(labels = percent) +
theme(strip.text.y.left = element_text(angle=0)) +
guides(fill = guide_legend(reverse=T, nrow = 1)) +
theme(axis.title.y=element_blank(), axis.text.y=element_blank(), axis.ticks.y=element_blank()) +
theme(panel.grid.minor.y=element_blank(), panel.grid.major.y=element_blank()) +
geom_text(aes(label = ifelse(pct >.05, paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 4, family = "font", color = "black") +
geom_text(aes(label = ifelse(age2 == "18-35", paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 4, family = "font", color = "white") +
geom_text(aes(label = ifelse(age2 == "36-44", paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 4, family = "font", color = "white") +
theme(plot.title = element_text(size = 16)) +
theme(strip.text.y.left = element_text(angle = 0, hjust = 1)) +
labs(x = "", y = "", title = "The Age Distribution of Adults in the 25 Largest Protestant Denominations", caption = "@ryanburge\nData: Cooperative Election Study, 2022")
save("age2_25_prot_trad.png", wd = 9, ht = 8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment