Skip to content

Instantly share code, notes, and snippets.

@ryanburge
Created July 1, 2022 16:44
Show Gist options
  • Save ryanburge/12f80ddc6dc40c92f29da1c12affb581 to your computer and use it in GitHub Desktop.
Save ryanburge/12f80ddc6dc40c92f29da1c12affb581 to your computer and use it in GitHub Desktop.
gg <- ns %>%
mutate(educ = frcode(education <= 3 ~ "Some HS",
education == 4 ~ "HS Grad",
education == 5 ~ "Trade School",
education == 6 ~ "Some College",
education == 7 ~ "2 Yr.",
education == 8 ~ "4 Yr.",
education == 9 ~ "Some Grad.",
education == 10 ~ "Masters",
education == 11 ~ "Doctorate")) %>%
mutate(relig = frcode(religion == 1 ~ "Protestant",
religion == 2 ~ "Catholic",
religion == 5 ~ "Christian",
religion == 10 | religion == 11 ~ "Atheist/Agnostic",
religion == 12 ~ "Nothing in Part.",
religion == 3 | religion == 4 | religion == 6 | religion == 7 | religion == 8 | religion == 9 ~ "Other World Religions")) %>%
group_by(educ) %>%
ct(relig, wt = weight, show_na = FALSE)
gg %>%
mutate(lab = round(pct, 2)) %>%
ggplot(., aes(x = 1, y = pct, fill = fct_rev(relig))) +
geom_col(color = "black") +
coord_flip() +
facet_wrap(~ educ, ncol =1, strip.position = "left") +
scale_fill_manual(values=met.brewer("Juarez", 6)) +
theme_rb() +
theme(legend.position = "bottom") +
scale_y_continuous(labels = percent) +
theme(strip.text.y.left = element_text(angle=0, hjust = 1)) +
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 = 6, family = "font", color = "black") +
geom_text(aes(label = ifelse(relig == "Protestant", paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 6, family = "font", color = "white") +
labs(x = "", y = "", title = "Religious Composition by Educational Attainment", subtitle = "", caption = "@ryanburge\nData: Nationscape 2019-2021")
save("ed_nationscape.png", wd = 9, ht = 6)
# https://gist.github.com/12f80ddc6dc40c92f29da1c12affb581
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment