Skip to content

Instantly share code, notes, and snippets.

@ryanburge
Created May 16, 2024 18:53
Show Gist options
  • Save ryanburge/178c0a0c7d07bb7f9100dab825c6b749 to your computer and use it in GitHub Desktop.
Save ryanburge/178c0a0c7d07bb7f9100dab825c6b749 to your computer and use it in GitHub Desktop.
gg <- 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")
gg %>%
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") +
labs(x = "", y = "", title = "The Rise of the Nones, 2008-2023", caption = "@ryanburge\nData: Cooperative Election Study, 2008-2023")
save("nones_ces23.png", wd = 9)
gg <- cces %>%
filter(year == 2023) %>%
mutate(relig = frcode(religion == 1 ~ "Protestant",
religion == 2 ~ "Catholic",
religion >= 3 & religion <= 8 ~ "Other World Religions",
religion == 9 | religion == 10 ~ "Atheist/Agnostic",
religion == 11 ~ "Nothing in Particular", TRUE ~ "All Others")) %>%
mutate(gen = frcode(birthyr>= 1901 & birthyr <= 1924 ~ 'Greatest Generation',
birthyr>= 1925 & birthyr <= 1945 ~ 'Silent Generation',
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 = 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(~ gen, ncol =1, strip.position = "left") +
scale_fill_paletteer_d("ggthemes::excel_Berlin") +
theme_rb() +
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()) +
theme(plot.title = element_text(size = 20)) +
geom_text(aes(label = ifelse(pct >.05, paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 8, family = "font", color = "black") +
labs(x = "", y = "", title = "Religious Composition of Each Generation", caption = "@ryanburge\nData: Cooperative Election Study, 2023")
save("gens_relig_2023.png", wd = 9, ht = 4)
gg <- cces %>%
filter(year == 2023) %>%
cces_attend(pew_attendance) %>%
mutate(gen = frcode(birthyr>= 1901 & birthyr <= 1924 ~ 'Greatest Generation',
birthyr>= 1925 & birthyr <= 1945 ~ 'Silent Generation',
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(att, wt = weight, show_na = FALSE)
gg %>%
mutate(lab = round(pct, 2)) %>%
ggplot(., aes(x = 1, y = pct, fill = fct_rev(att))) +
geom_col(color = "black") +
coord_flip() +
facet_wrap(~ gen, ncol =1, strip.position = "left") +
scale_fill_manual(values=met.brewer("Egypt", 6)) +
theme_rb() +
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()) +
theme(plot.title = element_text(size = 20)) +
geom_text(aes(label = ifelse(pct >.05, paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 7, family = "font", color = "black") +
geom_text(aes(label = ifelse(pct >.05 & att == "Weekly", paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 7, family = "font", color = "white") +
geom_text(aes(label = ifelse(pct >.05 & att == "Weekly+", paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 7, family = "font", color = "white") +
labs(x = "", y = "", title = "Religious Service Attendance by Generation", caption = "@ryanburge\nData: Cooperative Election Study, 2023")
save("gens_att_2023.png", wd = 9, ht = 4)
gg1 <- cces %>%
filter(year == 2023) %>%
mutate(gen = frcode(birthyr>= 1901 & birthyr <= 1924 ~ 'Greatest Generation',
birthyr>= 1925 & birthyr <= 1945 ~ 'Silent Generation',
birthyr>= 1946 & birthyr <= 1964 ~ 'Boomers',
birthyr>= 1965 & birthyr <= 1976 ~ 'Gen X',
birthyr>= 1977 & birthyr <= 1995 ~ 'Millennials',
birthyr>= 1996 & birthyr <= 2019 ~ 'Gen Z')) %>%
filter(gen == "Gen Z") %>%
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")) %>%
ct(trad, wt = weight, show_na = FALSE) %>%
mutate(type = "Gen Z")
gg2 <- cces %>%
filter(year == 2023) %>%
mutate(gen = frcode(birthyr>= 1901 & birthyr <= 1924 ~ 'Greatest Generation',
birthyr>= 1925 & birthyr <= 1945 ~ 'Silent Generation',
birthyr>= 1946 & birthyr <= 1964 ~ 'Boomers',
birthyr>= 1965 & birthyr <= 1976 ~ 'Gen X',
birthyr>= 1977 & birthyr <= 1995 ~ 'Millennials',
birthyr>= 1996 & birthyr <= 2019 ~ 'Gen Z')) %>%
filter(gen != "Gen Z") %>%
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")) %>%
ct(trad, wt = weight, show_na = FALSE) %>%
mutate(type = "Everyone Else")
both <- bind_rows(gg1, gg2)
both %>%
mutate(lab = round(pct, 2)) %>%
ggplot(., aes(x = reorder(trad, pct), y = pct, fill = type)) +
geom_col(color = "black", position = "dodge") +
coord_flip() +
geom_text(aes(y = pct + .0095, label = ifelse(pct < .10, paste0(lab*100, '%'), "")), position = position_dodge(width = .9), size =6, family = "font") +
geom_text(aes(y = pct + .0105, label = ifelse(pct > .10, paste0(lab*100, '%'), "")), position = position_dodge(width = .9), size = 6, family = "font") +
theme_rb(legend = TRUE) +
y_pct() +
scale_fill_calc() +
guides(fill = guide_legend(reverse = TRUE)) +
theme(legend.text = element_text(size = 20)) +
labs(x= "", y = "", title = "The Religious Breakdown of Generation Z vs Everyone Else", caption = "@ryanburge\nData: Cooperative Election Study, 2023")
save("gen_z_relig23_compare.png", ht = 9)
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) %>%
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.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.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.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 = 6.5, family = "font")+
theme_rb() +
error_bar() +
scale_fill_manual(values=met.brewer("Renoir", 6)) +
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-2023")
save("none_years_gen23.png", wd = 10, ht = 8)
gg <- cces %>%
filter(birthyr > 1940) %>%
filter(year == 2020 | year == 2021 | year == 2022 | year == 2023) %>%
mutate(none = case_when(religion == 9 | religion == 10 | religion == 11 ~ 1,
TRUE ~ 0)) %>%
group_by(birthyr, year) %>%
mean_ci(none, wt = weight) %>%
mutate(year = as.factor(year))
gg %>%
# filter(birthyr >= 1980) %>%
ggplot(., aes(x = birthyr, y = mean, color = year, group = year)) +
geom_point(stroke = .25, shape = 21, alpha = .6) +
geom_smooth(se = FALSE) +
theme_rb(legend = TRUE) +
scale_color_manual(values = c("#ff5964", "#00b259", "#35a7ff", "#ffcc00")) +
scale_y_continuous(labels = percent, limits = c(0, .51)) +
labs(x = "Birth Year", y = "", title = "Share Who Have No Religious Affiliation by Birth Year, Among Those Born in 1980 or Later", caption = "@ryanburge\nData: Cooperative Election Study, 2020-2023")
save("nones_birthyr23.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment