Skip to content

Instantly share code, notes, and snippets.

@ryanburge
Created July 11, 2023 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanburge/74f33aff0e73e7678533a52bb0c1f80d to your computer and use it in GitHub Desktop.
Save ryanburge/74f33aff0e73e7678533a52bb0c1f80d to your computer and use it in GitHub Desktop.
graph <- cces %>%
filter(year == 2008 | year == 2012 | year == 2016 | year == 2020 | year == 2022) %>%
filter(pew_bornagain == 1) %>%
cces_attend(pew_attendance) %>%
group_by(year) %>%
ct(att, wt = weight, show_na = FALSE)
graph %>%
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") +
scale_fill_manual(values = c(met.brewer("Johnson", 6))) +
theme_rb() +
theme(legend.position = "bottom") +
scale_y_continuous(labels = percent) +
theme(strip.text.y.left = element_text(angle=0, hjust = 1)) +
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 = 7, family = "font", color = "black") +
geom_text(aes(label = ifelse(pct >.05 & att == "Never", 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 Attendance Among Self-Identified Evangelicals", subtitle = "", caption = "@ryanburge\nData: Cooperative Election Study")
save("ev_att2022.png", wd = 9, ht = 4)
gg1 <- cces %>%
mutate(vimp = case_when(pew_importance == 1 ~ 1,
pew_importance == 2 | pew_importance == 3 | pew_importance == 4 ~ 0)) %>%
group_by(year) %>%
mean_ci(vimp, wt = weight, ci = .84) %>%
mutate(pid3 = "Entire Sample")
gg2 <- cces %>%
mutate(vimp = case_when(pew_importance == 1 ~ 1,
pew_importance == 2 | pew_importance == 3 | pew_importance == 4 ~ 0)) %>%
cces_pid3(pid7) %>%
group_by(year, pid3) %>%
mean_ci(vimp, wt = weight, ci = .84) %>%
filter(pid3 != "NA")
graph <- bind_rows(gg1, gg2) %>% filter(year >= 2008)
graph$pid3 <- factor(graph$pid3, levels = c("Entire Sample", "Democrat", "Independent", "Republican"))
graph %>%
ggplot(., aes(x = year, y = mean, color = pid3, group = pid3)) +
geom_point(stroke = .5, shape = 21, alpha = .45) +
geom_labelsmooth(aes(label = pid3), method = "loess", formula = y ~ x, family = "font", linewidth = 1, text_smoothing = 30, size = 6, linewidth = 1, boxlinewidth = 0.3, hjust = .85) +
scale_color_manual(values = c("black", "dodgerblue3", "darkorchid", "firebrick3")) +
y_pct() +
theme_rb() +
labs(x = "Year", y = "", title = "Share Saying Religion is Very Important", caption = "@ryanburge\nData: Cooperative Election Study, 2008-2022")
save("vimp_yrs_ces.png")
gg2 <- cces %>%
mutate(vimp = case_when(pew_importance == 1 ~ 1,
pew_importance == 2 | pew_importance == 3 | pew_importance == 4 ~ 0)) %>%
cces_pid3(pid7) %>%
group_by(year, pid3) %>%
mean_ci(vimp, wt = weight, ci = .84) %>%
filter(pid3 != "NA") %>%
select(pid3, year, imp = mean)
gg3 <- cces %>%
filter(year >= 2008) %>%
mutate(wk = case_when(pew_attendance == 1 | pew_attendance == 2 ~ 1,
pew_attendance <= 6 ~ 0)) %>%
cces_pid3(pid7) %>%
filter(pid3 != "NA") %>%
group_by(pid3, year) %>%
mean_ci(wk, wt = weight, ci = .84) %>%
select(pid3, year, wk = mean)
graph <- left_join(gg2, gg3) %>% na.omit() %>% filter(pid3 != "Independent")
library(ggrepel)
graph %>%
ggplot(., aes(x = imp, y = wk, color = pid3, group = pid3)) +
geom_point(stroke = 1, shape = 21) +
geom_smooth(se = FALSE, method = lm) +
geom_text_repel(data = graph, aes(x = imp, y= wk, label = year), family = "font", size = 5) +
theme_rb() +
scale_color_manual(values = c("dodgerblue3", "firebrick3")) +
scale_x_continuous(labels = percent, breaks = c(.25, .30, .35, .40, .45, .50, .55, .60)) +
y_pct() +
add_text(x = .50, y = .43, word = "Republicans", sz = 8) +
add_text(x = .33, y = .26, word = "Democrats", sz = 8) +
labs(x = "Share Saying Religion is Very Important", y = "Share Attending Services Weekly", title = "Religion Importance and Religious Attendance among Democrats and Republicans",
caption = "@ryanburge\nData: Cooperative Election Study, 2008-2022")
save("scatter_imp_att.png")
graph <- cces %>%
filter(year == 2008 | year == 2012 | year == 2016 | year == 2020 | year == 2022) %>%
filter(pew_attendance == 6) %>%
cces_pid3(pid7) %>%
mutate(imp = frcode(pew_importance == 4 ~ "Not at all important",
pew_importance == 3 ~ "Not too important",
pew_importance == 2 ~ "Somewhat important",
pew_importance == 1 ~ "Very important")) %>%
group_by(year, pid3) %>%
ct(imp, wt = weight, show_na = FALSE)
graph %>%
mutate(lab = round(pct, 2)) %>%
filter(pid3 != "Independent") %>%
ggplot(., aes(x = 1, y = pct, fill = fct_rev(imp))) +
geom_col(color = "black") +
coord_flip() +
facet_grid(year ~ pid3, switch = "y") +
scale_fill_manual(values = c(met.brewer("Homer1", 4))) +
theme_rb() +
theme(legend.position = "bottom") +
scale_y_continuous(labels = percent) +
theme(strip.text.y.left = element_text(angle=0, hjust = 1)) +
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") +
geom_text(aes(label = ifelse(pct >.05 & imp == "Very important", paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 6, family = "font", color = "white") +
labs(x = "", y = "", title = "Religious Importance Among Never Attenders", subtitle = "", caption = "@ryanburge\nData: Cooperative Election Study, 2008-2022")
save("imp_never__att2022.png", wd = 11, ht = 4)
graph <- cces %>%
filter(year >= 2008) %>%
cces_pid3(pid7) %>%
filter(pid3 == "Democrat" | pid3 == "Republican") %>%
mutate(ba = case_when(pew_bornagain == 1 ~ 1,
pew_bornagain == 2 ~ 0)) %>%
cces_attend(pew_attendance) %>%
group_by(year, pid3, att) %>%
mean_ci(ba, wt = weight, ci = .84) %>% filter(att != "NA")
graph %>%
ggplot(., aes(x = year, y = mean, color = pid3, group = pid3)) +
geom_point(stroke = .5, shape= 21, alpha = .45, show.legend = FALSE) +
geom_smooth(se = FALSE, method = lm) +
facet_wrap(~ att) +
scale_color_manual(values = c("dodgerblue3", "firebrick3")) +
theme_rb(legend = TRUE) +
y_pct() +
theme(strip.text = element_text(size = 20)) +
labs(x = "Year", y = "", title = "Share Self-Identifying as Born-Again/Evangelical by Religious Attendance", caption = "@ryanburge\nData: Cooperative Election Study, 2008-2022")
save("ba_att_pid2.png", ht = 8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment