Skip to content

Instantly share code, notes, and snippets.

@ryanburge
Created Jun 8, 2022
Embed
What would you like to do?
GenZ 2021
gg <- cces %>%
filter(year == 2021) %>%
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 Part.",
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()) +
geom_text(aes(label = ifelse(pct >.05, paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 6, family = "font", color = "black") +
labs(x = "", y = "", title = "Religious Breakdown by Generation", subtitle = "", caption = "@ryanburge\nData: CES 2021") +
save("gens_relig_2021.png", wd = 9, ht = 4)
gg <- cces %>%
filter(year == 2021) %>%
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()) +
geom_text(aes(label = ifelse(pct >.05, paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 6, family = "font", color = "black") +
labs(x = "", y = "", title = "Religious Service Attendance by Generation", subtitle = "", caption = "@ryanburge\nData: CES 2021") +
save("gens_att_2021.png", wd = 9, ht = 4)
gg <- cces %>%
mutate(gen = frcode(birthyr>= 1901 & birthyr <= 1924 ~ 'Greatest Generation',
birthyr>= 1925 & birthyr <= 1945 ~ 'Silent\n1925-1945',
birthyr>= 1946 & birthyr <= 1964 ~ 'Boomers\n1946-1964',
birthyr>= 1965 & birthyr <= 1976 ~ 'Gen X\n1965-1976',
birthyr>= 1977 & birthyr <= 1995 ~ 'Millennials\n1977-1995',
birthyr>= 1996 & birthyr <= 2019 ~ 'Gen Z\n1996-')) %>%
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 == 2021) %>%
ggplot(., aes(x = gen, y = mean, fill = factor(year))) +
geom_col(color = "black", position = "dodge") +
geom_text(aes(y = .02, label = ifelse(gen != "Gen Z\n1996-", paste0(lab*100, '%'), "")), position = position_dodge(width = .9), size = 4.5, family = "font")+
geom_text(aes(y = .02, label = ifelse(gen != "Gen Z\n1996-" & year == 2008, paste0(lab*100, '%'), "")), position = position_dodge(width = .9), size = 4.5, family = "font", color = "white")+
geom_text(aes(y = .02, label = ifelse(gen != "Gen Z\n1996-" & year == 2012, paste0(lab*100, '%'), "")), position = position_dodge(width = .9), size = 4.5, family = "font", color = "white")+
geom_text(aes(y = .035, label = ifelse(gen == "Gen Z\n1996-", paste0(lab*100, '%'), "")), position = position_dodge(width = .9), size = 7.5, family = "font")+
theme_rb(legend = TRUE) +
error_bar() +
scale_fill_manual(values=met.brewer("Renoir", 5)) +
y_pct() +
theme(axis.text.x = element_text(size = 20)) +
labs(x = "", y = "% Nones", title = "Share Who Have No Religious Affiliation", caption = "@ryanburge\nData: CES 2008-2021") +
save("none_years_gen.png", wd = 10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment