Skip to content

Instantly share code, notes, and snippets.

@ryanburge
Created April 30, 2024 18:47
Show Gist options
  • Save ryanburge/3f68776ccb5a012b0435a93da98e174c to your computer and use it in GitHub Desktop.
Save ryanburge/3f68776ccb5a012b0435a93da98e174c to your computer and use it in GitHub Desktop.
gg <- cces %>%
filter(year == 2008 | year == 2012 | year == 2016 | year == 2020 | year == 2022) %>%
cces_pid3(pid7) %>%
mutate(aa = case_when(religion == 9 | religion == 10 ~ 1,
TRUE ~ 0)) %>%
group_by(year, pid3) %>%
mean_ci(aa, wt = weight, ci = .84) %>%
filter(pid3 == "Democrat" | pid3 == "Republican")
gg %>%
mutate(lab = round(mean, 2)) %>%
ggplot(., aes(x = factor(year), y = mean, fill = pid3)) +
geom_col(color = "black", position = "dodge") +
theme_rb(legend = TRUE) +
lab_bar(top = FALSE, type = lab, pos = .015, sz = 7) +
error_bar() +
scale_fill_manual(values = c("dodgerblue", "firebrick")) +
scale_y_continuous(labels = percent) +
labs(x = "Year", y = "", title = "Share Who Identify as Atheist/Agnostic by Partisanship", caption = "@ryanburge\nData: Cooperative Election Study, 2008-2022")
save("ath_agn_pid2.png", wd = 6)
gg1 <- cces %>%
filter(year == 2008 | year == 2012 | year == 2016 | year == 2020 | year == 2022) %>%
filter(pew_attendance == 5 | pew_attendance == 6) %>%
cces_pid3(pid7) %>%
mutate(religpew = religion) %>%
mutate(relig = frcode(religpew == 1 ~ "Protestant",
religpew == 2 ~ "Catholic",
religpew >= 3 & religpew <= 8 ~ "Other World Religions",
religpew == 9 | religpew == 10 ~ "Atheist/Agnostic",
religpew == 11 ~ "Nothing in Particular")) %>%
group_by(year, pid3) %>%
ct(relig, wt = weight, show_na = FALSE) %>%
filter(pid3 == "Democrat" | pid3 == "Republican")
gg1 %>%
mutate(lab = round(pct, 2)) %>%
ggplot(., aes(x = 1, y = pct, fill = fct_rev(relig))) +
geom_col(color = "black") +
coord_flip() +
facet_grid(year ~ pid3, switch = "y") +
scale_fill_manual(values = moma.colors("Panton", 5)) +
theme_rb(legend = TRUE) +
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 >.08, paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 6, family = "font", color = "black") +
geom_text(aes(label = ifelse(pct >.08 & relig == "Protestant", paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 6, family = "font", color = "white") +
geom_text(aes(label = ifelse(pct >.08 & relig == "Catholic", paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 6, family = "font", color = "white") +
theme(strip.text = element_text(size = 20)) +
labs(x = "", y = "", title = "Religious Composition of Those Who Attend Religious Services Seldom/Never", caption = "@ryanburge\nData: Cooperative Election Study, 2008-2022")
save("rel_com_low_attend_pid2.png", wd = 9, ht =4)
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"))
}
gg <- cces %>%
filter(year >= 2020) %>%
cces_pid3(pid7) %>%
cces_attend2(pew_attendance) %>%
mutate(imp = frcode(pew_importance == 4 ~ "Not\nAt All",
pew_importance == 3 ~ "Not Too",
pew_importance == 2 ~ "Somewhat",
pew_importance == 1 ~ "Very")) %>%
mutate(aa = case_when(religion == 9 | religion == 10 ~ 1,
TRUE ~ 0)) %>%
filter(pid3 == "Democrat" | pid3 == "Republican") %>%
group_by(pid3, att, imp) %>%
mean_ci(aa, wt = weight, ci = .84) %>%
filter(att != 'NA') %>%
filter(imp != 'NA')
gg <- gg %>%
mutate(bins = frcode(mean < .05 ~ "a",
mean >= .05 & mean < .30 ~ "b",
mean >= .30 ~ "c"))
gg %>%
mutate(lab = round(mean, 2)) %>%
ggplot(., aes(x= imp, y = att)) +
geom_tile(aes(fill = bins), color = "black") +
scale_fill_manual(values = c("#b6eaff", "#0a8dff", "#0f5099")) +
theme_rb() +
facet_wrap(~ pid3) +
# theme(plot.subtitle = element_text(size = 24)) +
geom_text(aes(x= imp, y = att, label = paste0(lab*100, '%')), size = 9, family = "font") +
geom_text(aes(x= imp, y = att, label = ifelse(bins == "c", paste0(lab*100, '%'), "")), size = 9, family = "font", color = "white") +
theme(strip.text = element_text(size = 20)) +
labs(x= "Religious Importance", y = "Religious Attendance", title = "Share Identifying as Atheist/Agnostic by Attendance/Importance",
caption = "@ryanburge\nData: Cooperative Election Study, 2020-2022")
save('heat_ath_agn_pid3.png', wd = 8, ht = 5)
fun <- function(df, var, name){
aa1 <- df %>%
filter(religpew == 9 | religpew == 10) %>%
cces_pid3(pid7) %>%
filter(pid3 == "Democrat" | pid3 == "Republican") %>%
group_by(pid3) %>%
mutate(ab = {{var}}) %>%
mutate(ab = case_when(ab == 1 ~ 1,
ab == 2 ~ 0)) %>%
mean_ci(ab, wt = commonweight, ci = .84) %>%
mutate(type = name) %>%
mutate(grp = "Ath./Agn.")
aa2 <- df %>%
cces_pid3(pid7) %>%
filter(pid3 == "Democrat" | pid3 == "Republican") %>%
group_by(pid3) %>%
mutate(ab = {{var}}) %>%
mutate(ab = case_when(ab == 1 ~ 1,
ab == 2 ~ 0)) %>%
mean_ci(ab, wt = commonweight, ci = .84) %>%
mutate(type = name) %>%
mutate(grp = "All")
bind_rows(aa1, aa2)
}
ttt1 <- cces22 %>% fun(CC22_332a, "Allow for Any Reason")
ttt2 <- cces22 %>% fun(CC22_332c, "Prohibit After 20 Wks.")
ttt3 <- cces22 %>% fun(CC22_332e, "Prohibit Federal Funding")
ttt4 <- cces22 %>% fun(CC22_332f, "Illegal in All Circumstances")
all <- bind_rows(ttt1, ttt2, ttt3, ttt4)
all <- all %>%
mutate(pid = frcode(pid3 == "Democrat" ~ "Dem.",
pid3 == "Republican" ~ "Rep."))
all <- all %>%
mutate(lab = paste(grp, pid, sep = '\n'))
all$lab <- factor(all$lab, levels = c("All\nDem.", "Ath./Agn.\nDem.", "Ath./Agn.\nRep.", "All\nRep."))
all %>%
mutate(lab2 = round(mean, 2)) %>%
ggplot(., aes(x = lab, y = mean, fill = lab)) +
geom_col(color = "black") +
facet_wrap(~ type) +
scale_fill_manual(values = c("#19478f", "#59baff", "#ff6464", "#881414")) +
theme_rb() +
error_bar() +
y_pct() +
lab_bar(top = FALSE, type = lab2, pos = .085, sz = 8) +
geom_text(aes(y = .085, label = ifelse(lab == "All\nDem.", paste0(lab2*100, '%'), "")), position = position_dodge(width = .9), size = 8, family = "font", color = "white") +
geom_text(aes(y = .085, label = ifelse(lab == "All\nRep.", paste0(lab2*100, '%'), "")), position = position_dodge(width = .9), size = 8, family = "font", color = "white") +
labs(x = "", y = "", title = "Views of Abortion by Partisanship and Religion", caption = "@ryanburge\nData: Cooperative Election Study, 2022")
save("abort_ath_agn_pid2_ces22.png", wd = 6)
graph1 <- cces08 %>%
cces_pid3(CC307a) %>%
filter(pid3 == "Republican") %>%
filter(V219 == 9 | V219 == 10) %>%
mutate(vote = CC410) %>%
mutate(vote = frcode(vote == 1 ~ "Republican",
vote == 2 ~ "Democrat",
vote == 3 | vote == 4 | vote == 5 | vote == 6 ~ "Third Party")) %>%
ct(vote, wt = V201, show_na = FALSE) %>%
mutate(year = "2008") %>%
mutate(type = "Atheist/Agnostic")
graph2 <- cces08 %>%
cces_pid3(CC307a) %>%
filter(pid3 == "Republican") %>%
mutate(vote = CC410) %>%
mutate(vote = frcode(vote == 1 ~ "Republican",
vote == 2 ~ "Democrat",
vote == 3 | vote == 4 | vote == 5 | vote == 6 ~ "Third Party")) %>%
ct(vote, wt = V201, show_na = FALSE) %>%
mutate(year = "2008") %>%
mutate(type = "All")
graph3 <- cces12 %>%
cces_pid3(pid7) %>%
filter(pid3 == "Republican") %>%
filter(religpew == 9 | religpew == 10) %>%
mutate(vote = CC410a) %>%
mutate(vote = frcode(vote == 1 ~ "Democrat",
vote == 2 ~ "Republican",
vote == 4 ~ "Third Party")) %>%
ct(vote, wt = weight_vv_post, show_na = FALSE) %>%
mutate(year = "2012") %>%
mutate(type = "Atheist/Agnostic")
graph4 <- cces12 %>%
cces_pid3(pid7) %>%
filter(pid3 == "Republican") %>%
mutate(vote = CC410a) %>%
mutate(vote = frcode(vote == 1 ~ "Democrat",
vote == 2 ~ "Republican",
vote == 4 ~ "Third Party")) %>%
ct(vote, wt = weight_vv_post, show_na = FALSE) %>%
mutate(year = "2012") %>%
mutate(type = "All")
graph5 <- cces16 %>%
cces_pid3(pid7) %>%
filter(pid3 == "Republican") %>%
filter(religpew == 9 | religpew == 10) %>%
mutate(vote = CC16_410a) %>%
filter(CL_matched == "Y") %>%
mutate(vote = CC16_410a) %>%
mutate(vote = frcode(vote == 1 ~ "Republican",
vote == 2 ~ "Democrat",
vote == 3 | vote == 4 | vote == 5 | vote == 8 ~ "Third Party")) %>%
ct(vote, wt = commonweight_vv_post, show_na = FALSE) %>%
mutate(year = "2016") %>%
mutate(type = "Atheist/Agnostic")
graph6 <- cces16 %>%
cces_pid3(pid7) %>%
filter(pid3 == "Republican") %>%
mutate(vote = CC16_410a) %>%
filter(CL_matched == "Y") %>%
mutate(vote = CC16_410a) %>%
mutate(vote = frcode(vote == 1 ~ "Republican",
vote == 2 ~ "Democrat",
vote == 3 | vote == 4 | vote == 5 | vote == 8 ~ "Third Party")) %>%
ct(vote, wt = commonweight_vv_post, show_na = FALSE) %>%
mutate(year = "2016") %>%
mutate(type = "All")
graph7 <- cces20 %>%
cces_pid3(pid7) %>%
filter(pid3 == "Republican") %>%
filter(religpew == 9 | religpew == 10) %>%
filter(CL_voter_status <= 5) %>%
mutate(vote = CC20_410) %>%
mutate(vote = frcode(vote == 1 ~ "Democrat",
vote == 2 ~ "Republican",
vote == 4 ~ "Third Party")) %>%
ct(vote, wt = vvweight_post, show_na = FALSE) %>%
mutate(year = "2020") %>%
mutate(type = "Atheist/Agnostic")
graph8 <- cces20 %>%
cces_pid3(pid7) %>%
filter(pid3 == "Republican") %>%
filter(CL_voter_status <= 5) %>%
mutate(vote = CC20_410) %>%
mutate(vote = frcode(vote == 1 ~ "Democrat",
vote == 2 ~ "Republican",
vote == 4 ~ "Third Party")) %>%
ct(vote, wt = vvweight_post, show_na = FALSE) %>%
mutate(year = "2020") %>%
mutate(type = "All")
all <- bind_rows(graph1, graph2, graph3, graph4, graph5, graph6, graph7, graph8)
all %>%
mutate(lab = round(pct, 2)) %>%
ggplot(., aes(x = fct_rev(type), y = pct, fill = fct_rev(vote))) +
geom_col(color = "black") +
coord_flip() +
facet_wrap(~ year, ncol =1, strip.position = "top") +
scale_fill_manual(values = c("azure2", "dodgerblue", "firebrick3")) +
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(strip.text = element_text(size = 20)) +
theme(plot.title = element_text(size = 16)) +
# 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 >.055, paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 6, family = "font", color = "black") +
labs(x = "", y = "",
title = "Vote in Presidential Elections among Self-Identified Republicans",
caption = "@ryanburge\nData: Cooperative Election Study, 2008-2020")
save("ath_agn_rep_vote.png", wd = 7, ht = 7)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment