Skip to content

Instantly share code, notes, and snippets.

@ryanburge
Created April 15, 2024 20:13
Show Gist options
  • Save ryanburge/005f769cd5eb56b2521971736b6458bc to your computer and use it in GitHub Desktop.
Save ryanburge/005f769cd5eb56b2521971736b6458bc to your computer and use it in GitHub Desktop.
gg <-cces %>%
filter(year >= 2020) %>%
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_attend(pew_attendance) %>%
group_by(imp) %>%
ct(att, wt = weight, show_na = FALSE) %>%
na.omit() %>%
mutate(pct = n/142261) %>%
mutate(pct = round(pct, 2))
gg %>%
ggplot(., aes(x= imp, y = att)) +
geom_tile(aes(fill = pct), color = "black") +
scale_fill_gradient2(low = "#1fa2ff", mid = "#12d8fa", high = "#a6ffcb", midpoint = .07) +
theme_rb() +
theme(plot.subtitle = element_text(size = 24)) +
geom_text(aes(x= imp, y = att, label = paste0(pct*100, '%')), size = 11, family = "font") +
labs(x= "Religious Importance", y = "Religious Attendance", title = "Religious Importance versus Religious Attendance", caption = "@ryanburge\nData: Cooperative Election Study, 2020-2022")
save("heat_imp_att2022.png", wd = 6, ht = 6)
gg <- cces %>%
mutate(pew_religimp = pew_importance) %>%
mutate(imp = case_when(pew_religimp == 1 ~ 1, TRUE ~ 0)) %>%
mutate(att = case_when(pew_attendance == 5 | pew_attendance == 6 ~ 1, TRUE ~ 0)) %>%
mutate(both = imp + att) %>%
mutate(both = case_when(both == 2 ~ 1, TRUE ~ 0)) %>%
group_by(year) %>%
mean_ci(both, wt = weight, ci = .84)
gg %>%
mutate(lab = round(mean, 2)) %>%
filter(year > 2007) %>%
ggplot(., aes(x = year, y = mean, fill = mean)) +
geom_col(color = "black") +
theme_rb() +
error_bar() +
scale_fill_gradient2(low = "#f1f8fa", mid = "#64a6b9", high = "#2c414d", midpoint = .07) +
geom_text(aes(y = .0075, label = paste0(lab*100, '%')), position = position_dodge(width = .9), size = 8, family = "font") +
geom_text(aes(y = .0075, label = ifelse(mean > .075, paste0(lab*100, '%'), "")), position = position_dodge(width = .9), size = 8, family = "font", color = "white") +
scale_x_continuous(breaks = c(2008, 2010, 2012, 2014, 2016, 2018, 2020, 2022)) +
scale_y_continuous(labels = percent) +
labs(x = "", y = "", title = "The Share Who Say Religion is Very Important But Don't Attend", caption = "@ryanburge\nData: Cooperative Election Study, 2008-2022")
save("low_att_high_importance.png")
gg <- cces %>%
cces_pid3(pid7) %>%
mutate(pew_religimp = pew_importance) %>%
mutate(imp = case_when(pew_religimp == 1 ~ 1, TRUE ~ 0)) %>%
mutate(att = case_when(pew_attendance == 5 | pew_attendance == 6 ~ 1, TRUE ~ 0)) %>%
mutate(both = imp + att) %>%
mutate(both = case_when(both == 2 ~ 1, TRUE ~ 0)) %>%
group_by(year, pid3) %>%
mean_ci(both, wt = weight, ci = .84) %>% filter(pid3 != "NA")
gg %>%
filter(year > 2007) %>%
ggplot(., aes(x = year, y = mean, color = pid3, group = pid3)) +
geom_point(stroke = 1, shape = 21, fill = "white") +
geom_labelsmooth(aes(label = pid3), method = "loess", formula = y ~ x, family = "font", linewidth = 1, text_smoothing = 30, size = 7, linewidth = 1, boxlinewidth = 0.3, hjust = .75) +
pid3_color() +
theme_rb() +
scale_y_continuous(labels = percent, limits = c(.03, .12)) +
labs(x = "", y = "", title = "The Share Who Say Religion is Very Important But Don't Attend", caption = "@ryanburge\nData: Cooperative Election Study, 2008-2022")
save("pid3_low_att_high_importance.png")
gg <- cces %>%
filter(year >= 2020) %>%
mutate(pew_religimp = pew_importance) %>%
mutate(imp = case_when(pew_religimp == 1 ~ 1, TRUE ~ 0)) %>%
mutate(att = case_when(pew_attendance == 5 | pew_attendance == 6 ~ 1, TRUE ~ 0)) %>%
mutate(both = imp + att) %>%
mutate(both = case_when(both == 2 ~ 1, TRUE ~ 0)) %>%
group_by(age) %>%
mean_ci(both, wt = weight, ci = .84)
gg %>%
filter(age <= 75) %>%
ggplot(., aes(x = age, y = mean)) +
geom_point(stroke = 1, shape = 21, alpha = .5) +
geom_smooth(se = FALSE, color = "firebrick1", linetype = "twodash") +
scale_y_continuous(labels = percent, limits = c(0, .13)) +
theme_rb() +
labs(x = "Age", y = "", title = "The Share Who Say Religion is Very Important But Don't Attend", caption = "@ryanburge\nData: Cooperative Election Study, 2020-2022")
save("low_att_high_importance_age.png")
gg1 <- cces %>%
filter(year >= 2020) %>%
mutate(pew_religimp = pew_importance) %>%
mutate(imp = case_when(pew_religimp == 1 ~ 1, TRUE ~ 0)) %>%
filter(imp == 1) %>%
mutate(att = case_when(pew_attendance == 5 | pew_attendance == 6 ~ 1, TRUE ~ 0)) %>%
group_by(age) %>%
mean_ci(att, wt = weight)
gg1 %>%
filter(age <= 75) %>%
ggplot(., aes(x = age, y = mean)) +
geom_point(stroke = 1, shape = 21, alpha = .5) +
geom_smooth(se = FALSE, color = "firebrick1", linetype = "twodash") +
scale_y_continuous(labels = percent, limits = c(0, .30)) +
theme_rb() +
labs(x = "Age", y = "", title = "Among Those Who Say Religion is Very Important, The Share Who Attend Seldom/Never", caption = "@ryanburge\nData: Cooperative Election Study, 2020-2022")
save("low_att_high_importance_age2.png")
regg <- cces %>%
filter(year >= 2020) %>%
mutate(pew_religimp = pew_importance) %>%
mutate(imp = case_when(pew_religimp == 1 ~ 1, TRUE ~ 0)) %>%
mutate(att = case_when(pew_attendance == 5 | pew_attendance == 6 ~ 1, TRUE ~ 0)) %>%
mutate(both = imp + att) %>%
mutate(both = case_when(both == 2 ~ 1, TRUE ~ 0)) %>%
mutate(nokids = case_when(havekids == 2 ~ 1,
havekids == 1 ~ 0)) %>%
mutate(single = case_when(marital_status == 5 ~ 1,
TRUE ~ 0)) %>%
mutate(male = case_when(gender == 1 ~ 1,
gender == 2 ~ 0)) %>%
mutate(white = case_when(race == 1 ~ 1,
TRUE ~ 0)) %>%
mutate(cons = case_when(ideo5 == 4 | ideo5 == 5 ~ 1,
ideo5 <= 3 ~ 0)) %>%
select(age, income, educ, male, white, cons, single, nokids, both)
one <- glm(both ~ male + white + age + income + educ + cons, family = "binomial", data = regg)
library(jtools)
coef_names <- c("Age" = "age",
"Male" = "male",
"White" = "white",
"Income" = "income",
"Education" = "educ",
"Conservative" = "cons")
out <- plot_summs(one, robust = "HC3", scale = TRUE, coefs = coef_names)
out +
theme_rb() +
add_text(x = .25, y = 4.75, word = "More Likely", sz = 8) +
add_text(x = -.25, y = 1.75, word = "Less Likely", sz = 8) +
labs(x = "", y = "", title = "What Factors Predict Someone to Say That Religion is Very Important But Don't Attend", caption = "@ryanburge\nData: Cooperative Election Study, 2020-2022")
save("reg_predict_high_imp_low_attend.png", ht = 4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment