Skip to content

Instantly share code, notes, and snippets.

@ryanburge
Created May 3, 2024 15:30
Show Gist options
  • Save ryanburge/0af6e19ad59ab4c4e1e30f64b0e2dd98 to your computer and use it in GitHub Desktop.
Save ryanburge/0af6e19ad59ab4c4e1e30f64b0e2dd98 to your computer and use it in GitHub Desktop.
gg <- cces %>%
filter(year == 2008 | year == 2022) %>%
mutate(cath = case_when(religion == 2 ~ 1,
TRUE ~ 0)) %>%
group_by(birthyr, year) %>%
mean_ci(cath, wt = weight, ci = .84) %>%
na.omit() %>%
filter(n > 25) %>%
mutate(year = as.factor(year))
gg %>%
filter(birthyr >= 1930) %>%
ggplot(., aes(x = birthyr, y = mean, color = year, group = year)) +
geom_point(stroke = .5, shape = 21) +
scale_color_manual(values = c("#0D84A5", "#CA472F")) +
theme_rb() +
scale_y_continuous(labels = percent, limits = c(.1, .3)) +
geom_labelsmooth(aes(label = year), method = "loess", formula = y ~ x, family = "font", linewidth = 1, text_smoothing = 30, size = 7, linewidth = 1, boxlinewidth = 0.3) +
labs(x = "Birth Year", y = "", title = "Share Identifying as Catholic by Birth Year and Survey Year", caption = "@ryanburge\nData: Cooperative Election Study, 2008-2022")
save("cath_birth_year_ces.png")
gg1 <- gss %>%
filter(year <= 2018) %>%
mutate(birthyr = year - age) %>%
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(reltrad == 4) %>%
mutate(wk = case_when(attend == 6 | attend == 7 | attend == 8 ~ 1,
attend <= 5 ~ 0)) %>%
group_by(gen, year) %>%
mean_ci(wk, wt = wtssall)
gg2 <- gss %>%
filter(year > 2018) %>%
mutate(birthyr = year - age) %>%
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(reltrad == 4) %>%
mutate(wk = case_when(attend == 6 | attend == 7 | attend == 8 ~ 1,
attend <= 5 ~ 0)) %>%
group_by(gen, year) %>%
mean_ci(wk, wt = wtssnrps)
all <- bind_rows(gg1, gg2) %>% filter(n > 50) %>% filter(gen != "Gen Z") %>% filter(gen != "NA")
all %>%
ggplot(., aes(x = year, y = mean, color = gen, group = gen)) +
geom_point(stroke = .56, shape = 21) +
geom_labelsmooth(aes(label = gen), method = "loess", formula = y ~ x, family = "font", linewidth = 1, text_smoothing = 30, size = 6, linewidth = 1, boxlinewidth = 0.3) +
theme_rb() +
scale_color_gdocs() +
scale_y_continuous(labels = percent, limits = c(0, .7)) +
labs(x = "", y = "", title = "Share of Catholics Attending Mass Nearly Every Week or More", caption = "@ryanburge\nData: General Social Survey, 1972-2022")
save("cath_mass_attend_gens.png")
gg <- cces %>%
mutate(gen = frcode(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')) %>%
mutate(wk = case_when(pew_attendance == 1 | pew_attendance == 2 ~ 1,
pew_attendance <= 6 ~ 0)) %>%
group_by(year, gen) %>%
mean_ci(wk, wt = weight, ci = .84) %>% filter(n > 50)
gg %>%
ggplot(., aes(x = year, y = mean, color = gen, group = gen)) +
geom_point(stroke = .56, shape = 21) +
geom_labelsmooth(aes(label = gen), method = "loess", formula = y ~ x, family = "font", linewidth = 1, text_smoothing = 30, size = 6, linewidth = 1, boxlinewidth = 0.3) +
theme_rb() +
scale_y_continuous(labels = percent, limits = c(.2, .43)) +
scale_color_manual(values = c("#DC3912", "#FF9900", "#119618", "#980799", "azure4")) +
labs(x = "", y = "", title = "Share of Catholics Attending Mass Nearly Every Week or More", caption = "@ryanburge\nData: Cooperative Election Study, 2008-2022")
save("cath_mass_attend_gens_ces.png")
gg <- cces %>%
mutate(gen = frcode(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')) %>%
mutate(race2 = frcode(race == 1 ~ "White", TRUE ~ "Non-White")) %>%
mutate(wk = case_when(pew_attendance == 1 | pew_attendance == 2 ~ 1,
pew_attendance <= 6 ~ 0)) %>%
group_by(year, gen, race2) %>%
mean_ci(wk, wt = weight, ci = .84) %>% filter(n > 50) %>% filter(gen != 'NA')
gg %>%
ggplot(aes(x = year, y = mean, color = race2, group = race2)) +
geom_point(stroke = .56, shape = 21, show.legend = FALSE) +
geom_smooth(se = FALSE) +
facet_wrap(~ gen) +
theme_rb(legend = TRUE) +
scale_color_calc() +
theme(legend.position = c(.85, .16)) +
scale_y_continuous(labels = percent) +
labs(x = "", y = "", title = "Share of Catholics Attending Mass Nearly Every Week or More",
caption = "@ryanburge\nData: Cooperative Election Study, 2008-2022") +
guides(color = guide_legend(reverse = TRUE)) +
theme(legend.text = element_text(size = 20)) +
theme(strip.text = element_text(size = 20)) +
save("cath_mass_attend_gens_ces_race.png")
cces_attend2 <- function(df, var){
var <- enquo(var)
df %>%
mutate(att = car::recode(!! var, "6=1; 5=2; 4=3; 3=4; 2=5; 1=6; else = NA")) %>%
mutate(att = frcode(att == 1 ~ "Never",
att == 2 ~ "Seldom",
att == 3 ~ "Yearly",
att == 4 ~ "Monthly",
att == 5 | att == 6 ~ "Weekly"))
}
gg1 <- cces %>%
filter(year == 2008) %>%
filter(religion == 2) %>%
mutate(pew_religimp = pew_importance) %>%
mutate(imp = frcode(pew_religimp == 4 ~ "Not\nat All",
pew_religimp == 3 ~ "Not too",
pew_religimp == 2 ~ "Somewhat",
pew_religimp == 1 ~ "Very")) %>%
cces_attend2(pew_attendance) %>%
group_by(imp) %>%
ct(att, wt = weight, show_na = FALSE) %>%
na.omit() %>%
mutate(pct = n/6725) %>%
mutate(pct = round(pct, 2)) %>%
mutate(year = 2008)
gg2 <- cces %>%
filter(year == 2022) %>%
filter(religion == 2) %>%
mutate(pew_religimp = pew_importance) %>%
mutate(imp = frcode(pew_religimp == 4 ~ "Not\nat All",
pew_religimp == 3 ~ "Not too",
pew_religimp == 2 ~ "Somewhat",
pew_religimp == 1 ~ "Very")) %>%
cces_attend2(pew_attendance) %>%
group_by(imp) %>%
ct(att, wt = weight, show_na = FALSE) %>%
na.omit() %>%
mutate(pct = n/10219) %>%
mutate(pct = round(pct, 2)) %>%
mutate(year = 2022)
graph <- bind_rows(gg1, gg2)
graph <- graph %>%
mutate(bins = frcode(pct <= .01 ~ "a",
pct > .01 & pct <= .05 ~ "b",
pct > .05 & pct <= .10 ~ "c",
pct > .10 & pct <= .15 ~ "d",
pct > .15 ~ "e"))
graph %>%
ggplot(., aes(x= imp, y = att)) +
geom_tile(aes(fill = bins), color = "black") +
scale_fill_manual(values = c("#d6f2f7", "#42b1ce", "#237997", "#22627c", '#112c3b')) +
theme_rb() +
facet_wrap(~ year) +
theme(plot.subtitle = element_text(size = 24)) +
geom_text(aes(x= imp, y = att, label = paste0(pct*100, '%')), size = 11, family = "font") +
geom_text(aes(x= imp, y = att, label = ifelse(bins == 'e', paste0(pct*100, '%'), "")), size = 11, family = "font", color = "white") +
geom_text(aes(x= imp, y = att, label = ifelse(bins == 'd', paste0(pct*100, '%'), "")), size = 11, family = "font", color = "white") +
theme(strip.text = element_text(size = 20)) +
labs(x= "Religious Importance", y = "Religious Attendance", title = "Religious Importance and Religious Attendance among Catholics", caption = "@ryanburge\nData: Cooperative Election Study, 2008-2022")
save("heat_imp_att2022_caths.png", wd = 8, ht = 6)
gg <- cces %>%
filter(year == 2008 | year == 2022) %>%
cces_attend(pew_attendance) %>%
filter(religion == 2) %>%
group_by(year) %>%
ct(att, wt = weight, show_na = FALSE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment