Skip to content

Instantly share code, notes, and snippets.

@ryanburge
Created February 6, 2022 14:11
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/a2d3a34cc9ae9091e813f945a02870bf to your computer and use it in GitHub Desktop.
Save ryanburge/a2d3a34cc9ae9091e813f945a02870bf to your computer and use it in GitHub Desktop.
vsg <- read.fst("C://vsg_full.fst")
rel_fun <- function(df, old, new){
df %>%
mutate({{new}} := frcode({{old}} == 1 | {{old}} == 4 ~ "Protestant",
{{old}} == 2 ~ "Catholic",
{{old}} == 3 | {{old}} == 5 | {{old}} == 6 | {{old}} == 7 | {{old}} == 8 ~ "Other",
{{old}} == 9 | {{old}} == 10 ~ "Atheist/Agnostic",
{{old}} == 11 ~ "Nothing in Part."))
}
small <- vsg %>%
rel_fun(religion_2011, rel11) %>%
rel_fun(religion_2016, rel16) %>%
rel_fun(religion_2018, rel18) %>%
rel_fun(religion_2019Jan, rel19) %>%
rel_fun(religion_2020Sep, rel20) %>%
select(rel11, rel16, rel18, rel19, rel20) %>%
na.omit() %>%
mutate(caseid = 1:n()) %>%
pivot_longer(!caseid) %>%
mutate(name = frcode(name == "rel20" ~ "2020",
name == "rel19" ~ "2019",
name == "rel18" ~ "2018",
name == "rel16" ~ "2016",
name == "rel11" ~ "2011")) %>%
mutate(value = fct_rev(value))
ggplot(small,
aes(x = name, stratum = value, alluvium = caseid,
fill = value, label = value)) +
geom_flow(stat = "alluvium", lode.guidance = "leftright") +
geom_stratum() +
geom_label(fill = "white", stat = "stratum", size = 5, colour = "black", family = "font") +
theme_rb() +
scale_fill_paletteer_d("ggthemes::excel_Atlas") +
coord_flip() +
theme(legend.position="none") +
labs(x = "Year", y = "Number of Respondents", title = "Religious Tradition Shifts Over Times", caption = "@ryanburge\nData: VSG Panel (2011-2020)") +
save("vsg_rel_alluvial.png", wd = 12)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment