Skip to content

Instantly share code, notes, and snippets.

@ryanburge
Created April 3, 2025 02:07
Show Gist options
  • Save ryanburge/acf6e0479d3476f7fa02afd0b36b3559 to your computer and use it in GitHub Desktop.
Save ryanburge/acf6e0479d3476f7fa02afd0b36b3559 to your computer and use it in GitHub Desktop.
gg <- cces %>%
mutate(gen = frcode(birthyr>= 1901 & birthyr <= 1924 ~ 'Greatest Generation',
birthyr>= 1925 & birthyr <= 1945 ~ 'Silent, 1925-1945',
birthyr>= 1946 & birthyr <= 1964 ~ 'Boomers, 1946-1964',
birthyr>= 1965 & birthyr <= 1976 ~ 'Gen X, 1965-1976',
birthyr>= 1977 & birthyr <= 1995 ~ 'Millennials, 1977-1995',
birthyr>= 1996 & birthyr <= 2019 ~ 'Gen Z, 1996-')) %>%
mutate(nones = case_when(religion == 9 | religion == 10 | religion == 11 ~ 1, TRUE ~ 0)) %>%
group_by(year, gen) %>%
mean_ci(nones, wt = weight, ci = .84)
gg %>%
mutate(lab = round(mean, 2)) %>%
filter(gen != "Greatest Generation") %>%
filter(year == 2008 | year == 2012 | year == 2016 | year == 2020 | year == 2022| year == 2023 | year == 2024) %>%
ggplot(., aes(x = factor(year), y = mean, fill = factor(year))) +
geom_col(color = "black", position = "dodge") +
facet_wrap(~ gen) +
geom_text(aes(y = .03, label = ifelse(gen != "Gen Z, 1996-", paste0(lab*100, '%'), "")), position = position_dodge(width = .9), size = 5, family = "font")+
geom_text(aes(y = .03, label = ifelse(gen != "Gen Z, 1996-" & year == 2008, paste0(lab*100, '%'), "")), position = position_dodge(width = .9), size = 5, family = "font", color = "white")+
geom_text(aes(y = .03, label = ifelse(gen != "Gen Z, 1996-" & year == 2012, paste0(lab*100, '%'), "")), position = position_dodge(width = .9), size = 5, family = "font", color = "white")+
geom_text(aes(y = .035, label = ifelse(gen == "Gen Z, 1996-", paste0(lab*100, '%'), "")), position = position_dodge(width = .9), size = 5, family = "font")+
theme_rb() +
error_bar() +
scale_fill_manual(values=met.brewer("Renoir", 7)) +
y_pct() +
theme(strip.text = element_text(size = 20)) +
theme(plot.title = element_text(size = 26)) +
labs(x = "", y = "", title = "Share Who Have No Religious Affiliation by Generation", caption = "@ryanburge\nData: Cooperative Election Study, 2008-2024")
save("none_years_gen24.png", wd = 10, ht = 8)
gg1 <- cces %>%
mutate(religpew = religion) %>%
mutate(trad = frcode(religpew == 1 ~ "Protestant",
religpew == 2 ~ "Catholic",
religpew == 3 ~ "Mormon",
religpew == 4 ~ "Orthodox",
religpew == 5 ~ "Jewish",
religpew == 6 ~ "Muslim",
religpew == 7 ~ "Buddhist",
religpew == 8 ~ "Hindu",
religpew == 9 ~ "Atheist",
religpew == 10 ~ "Agnostic",
religpew == 11 ~ "Nothing in Particular",
religpew == 12 ~ "Something Else")) %>%
group_by(year) %>%
ct(trad, wt = weight, show_na = FALSE) %>% filter(trad == "Atheist" | trad == "Agnostic" | trad == "Nothing in Particular")
gg1 %>%
mutate(lab = round(pct, 2)) %>%
ggplot(., aes(x = year, y = pct, fill = trad)) +
geom_col(color = "black") +
theme_rb(legend = TRUE) +
scale_fill_gdocs() +
y_pct() +
geom_text(aes(label = paste0(lab*100, '%')), position = position_stack(vjust = 0.5), size = 6.5, family = "font") +
theme(plot.title = element_text(size = 24)) +
labs(x = "", y = "", title = "The Rise of the Nones, 2008-2024", caption = "@ryanburge\nData: Cooperative Election Study, 2008-2024")
save("nones_ces24.png", wd = 10)
gg1 <- cces %>%
filter(year == 2008 | year == 2012 | year == 2016 | year == 2020 | year == 2024) %>%
cces_attend(pew_attendance) %>%
group_by(year) %>%
ct(att, wt = weight, show_na = FALSE)
gg1 %>%
mutate(lab = round(pct, 2)) %>%
ggplot(., aes(x = 1, y = pct, fill = fct_rev(att))) +
geom_col(color = "black") +
coord_flip() +
facet_wrap(~ year, ncol =1, strip.position = "left") +
theme_rb() +
scale_fill_manual(values = c(moma.colors("OKeeffe", 6))) +
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 = 10, family = "font", color = "black") +
geom_text(aes(label = ifelse(att == "Never" | att == "Seldom", paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 10, 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 = 24)) +
theme(strip.text.y.left = element_text(angle = 0, hjust = 1)) +
labs(x = "", y = "", title = "Religious Attendance, 2008-2024", caption = "@ryanburge\nData: Cooperative Election Study, 2008-2024")
save("ces_relattend_2024.png", wd = 9, ht = 5)
gg1 <- cces %>%
filter(year == 2008 | year == 2012 | year == 2016 | year == 2020 | year == 2024) %>%
mutate(gen = frcode(birthyr>= 1946 & birthyr <= 1964 ~ 'Boomers',
birthyr>= 1965 & birthyr <= 1976 ~ 'Gen X',
birthyr>= 1977 & birthyr <= 1995 ~ 'Millennials',
birthyr>= 1996 & birthyr <= 2019 ~ 'Gen Z')) %>%
mutate(att = frcode(pew_attendance == 6 | pew_attendance == 5 ~ "Never/Seldom",
pew_attendance == 4 | pew_attendance == 3 ~ "Yearly/Monthly",
pew_attendance == 1 | pew_attendance == 2 ~ "Weekly")) %>%
group_by(year, gen) %>%
ct(att, wt = weight, show_na = FALSE) %>% na.omit()
gg1 %>%
mutate(year = as.factor(year)) %>%
mutate(lab = round(pct, 2)) %>%
ggplot(., aes(x = fct_rev(year), y = pct, fill = fct_rev(att))) +
geom_col(color = "black") +
coord_flip() +
facet_wrap(~ gen, ncol =1, strip.position = "top") +
theme_rb() +
scale_fill_manual(values = c(moma.colors("OKeeffe", 6)[c(1, 3, 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 = 8, family = "font", color = "black") +
geom_text(aes(label = ifelse(att == "Never/Seldom", paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 8, 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 = 24)) +
theme(strip.text.y.left = element_text(angle = 0, hjust = 1)) +
labs(x = "", y = "", title = "Religious Attendance by Generation, 2008-2024", caption = "@ryanburge\nData: Cooperative Election Study, 2008-2024")
save("ces_relattend_2024_gens.png", wd = 9, ht = 10)
gg1 <- cces24 %>%
mutate(relig = frcode(religpew == 1 ~ "Protestant",
religpew == 2 ~ "Catholic",
religpew >= 3 & religpew <= 8 ~ "Other World Religions",
religpew == 9 | religpew == 10 ~ "Atheist/Agnostic",
religpew == 11 ~ "Nothing in Part.",
religpew == 12 ~ "Something Else")) %>%
mutate(gen = frcode(birthyr>= 1925 & birthyr <= 1945 ~ 'Silent',
birthyr>= 1946 & birthyr <= 1964 ~ 'Boomers',
birthyr>= 1965 & birthyr <= 1976 ~ 'Gen X',
birthyr>= 1977 & birthyr <= 1995 ~ 'Millennials',
birthyr>= 1996 & birthyr <= 2019 ~ 'Gen Z')) %>%
group_by(gen) %>%
ct(relig, wt = commonweight, show_na = FALSE)
gg1 %>%
mutate(lab = round(pct, 2)) %>%
ggplot(., aes(x = 1, y = pct, fill = fct_rev(relig))) +
geom_col(color = "black") +
coord_flip() +
facet_wrap(~ gen, ncol =1, strip.position = "left") +
theme_rb() +
scale_fill_manual(values = c(
"Protestant" = "#0072B2", # Bold blue
"Catholic" = "#D55E00", # Bright orange
"Other World Religions" = "#009E73", # Teal green
"Atheist/Agnostic" = "#CC79A7", # Bright pink
"Nothing in Part." = "#F0E442", # Vivid yellow
"Something Else" = "#56B4E9" # Sky blue
)) +
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 = 10, family = "font", color = "black") +
geom_text(aes(label = ifelse(pct >.05 & relig == "Protestant", paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 10, 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 = 24)) +
theme(strip.text.y.left = element_text(angle = 0, hjust = 1)) +
labs(x = "", y = "", title = "Religious Composition of Each Generation in 2024", caption = "@ryanburge\nData: Cooperative Election Study, 2024")
save("ces_relcomp_gens2024.png", wd = 9, ht = 5)
gg1 <- cces %>%
filter(year >= 2014) %>%
mutate(gender = frcode(gender == 1 ~ "Men",
gender == 2 ~ "Women")) %>%
mutate(nones = case_when(religion == 9 | religion == 10 | religion == 11 ~ 1, TRUE ~ 0)) %>%
group_by(gender, birthyr, year) %>%
mean_ci(nones, wt = weight)
gg1 %>%
filter(year == 2014 | year == 2016 | year == 2018 | year == 2020 | year == 2022 | year == 2024) %>%
filter(birthyr >= 1940) %>%
filter(gender != "NA") %>%
ggplot(., aes(x = birthyr, y = mean, color = gender, group = gender)) +
geom_point(stroke = .1, shape = 21, alpha = .8) +
geom_smooth(se = FALSE) +
scale_color_calc() +
y_pct() +
facet_wrap(~ year) +
theme_rb(legend = TRUE) +
theme(strip.text = element_text(size = 20)) +
labs(x = "Birth Year", y = "", title = "Share Who Are Non-Religious by Gender and Birth Year", caption = "@ryanburge\nData: Cooperative Election Study, 2016-2024")
save("gender_ces24_nones.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment