Skip to content

Instantly share code, notes, and snippets.

@ryanburge
Created May 12, 2023 20:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanburge/ae063d15b23fe322cd59ab821851930d to your computer and use it in GitHub Desktop.
Save ryanburge/ae063d15b23fe322cd59ab821851930d to your computer and use it in GitHub Desktop.
fun <- function(var, name){
cces20 %>%
cces_trad(religpew) %>%
mutate(vv = {{var}}) %>%
mutate(vv = case_when(vv == 1 ~ 1,
vv == 2 ~ 0)) %>%
group_by(trad2) %>%
mean_ci(vv, wt = commonweight, ci = .84) %>%
mutate(type = name) %>%
filter(trad2 != "NA")
}
vars <- syms(c("CC20_430a_1","CC20_430a_2","CC20_430a_3","CC20_430a_4","CC20_430a_5","CC20_430a_6"))
labs <- c("Attend Political Meeting", "Put Up Political Sign", "Work for Candidate", "Attend Protest/March", "Contact Public Official", "Donate Money to Candidate")
gg <- map2_df(vars, labs, fun)
gg <- gg %>%
mutate(fill = frcode(trad2 == "Atheist" ~ 'a', TRUE ~ "b"))
gg %>%
mutate(lab = round(mean, 2)) %>%
ggplot(., aes(x= reorder(trad2, mean), y = mean, fill = fill)) +
geom_col(color = "black") +
facet_wrap(~ type, ncol = 2) +
coord_flip() +
lab_bar(top = FALSE, type = lab, pos = .03, sz = 4) +
geom_text(aes(y = .03, label = ifelse(fill == "a", paste0(lab*100, '%'), "")), position = position_dodge(width = .9), size = 4, family = "font", color = "white") +
theme_rb() +
y_pct() +
error_bar() +
scale_fill_manual(values = c("firebrick3", "azure3")) +
labs(title = "Have You Engaged in the Following Political Activites in the Previous Month?", caption = "@ryanburge\nData: Cooperative Election Study, 2020")
save("pol_act_2020ces.png", ht = 10)
fun <- function(var, name){
cces22 %>%
cces_trad(religpew) %>%
mutate(vv = {{var}}) %>%
mutate(vv = case_when(vv == 1 ~ 1,
vv == 2 ~ 0)) %>%
group_by(trad2) %>%
mean_ci(vv, wt = commonweight, ci = .84) %>%
mutate(type = name) %>%
filter(trad2 != "NA")
}
vars <- syms(c("CC22_430a_1","CC22_430a_2","CC22_430a_3","CC22_430a_4","CC22_430a_5","CC22_430a_6"))
labs <- c("Attend Political Meeting", "Put Up Political Sign", "Work for Candidate", "Attend Protest/March", "Contact Public Official", "Donate Money to Candidate")
gg <- map2_df(vars, labs, fun)
gg <- gg %>%
mutate(fill = frcode(trad2 == "Atheist" ~ 'a', TRUE ~ "b"))
gg %>%
mutate(lab = round(mean, 2)) %>%
ggplot(., aes(x= reorder(trad2, mean), y = mean, fill = fill)) +
geom_col(color = "black") +
facet_wrap(~ type, ncol = 2) +
coord_flip() +
lab_bar(top = FALSE, type = lab, pos = .03, sz = 4) +
geom_text(aes(y = .03, label = ifelse(fill == "a", paste0(lab*100, '%'), "")), position = position_dodge(width = .9), size = 4, family = "font", color = "white") +
theme_rb() +
y_pct() +
error_bar() +
scale_fill_manual(values = c("firebrick3", "azure3")) +
labs(title = "Have You Engaged in the Following Political Activites in the Previous Month?", caption = "@ryanburge\nData: Cooperative Election Study, 2022")
save("pol_act_2022ces.png", ht = 10)
yyy1 <- cces12 %>%
mutate(grp = frcode(evangelical == 1 & white == 1 ~ "White\nEvangelical",
religpew == 9 ~ "Atheist")) %>%
mutate(donate = CC417a_4) %>%
mutate(donate = case_when(donate == 1 ~ 1,
donate == 2 ~ 0)) %>%
group_by(grp) %>%
mean_ci(donate, wt = weight_vv, ci = .84) %>%
mutate(year = "2012") %>%
na.omit()
yyy2 <- cces14 %>%
mutate(grp = frcode(evangelical == 1 & white == 1 ~ "White\nEvangelical",
religpew == 9 ~ "Atheist")) %>%
mutate(donate = CC417a_4) %>%
mutate(donate = case_when(donate == 1 ~ 1,
donate == 2 ~ 0)) %>%
group_by(grp) %>%
mean_ci(donate, wt = weight, ci = .84) %>%
mutate(year = "2014") %>%
na.omit()
yyy3 <- cces16 %>%
mutate(grp = frcode(evangelical == 1 & white == 1 ~ "White\nEvangelical",
religpew == 9 ~ "Atheist")) %>%
mutate(donate = CC16_417a_4) %>%
mutate(donate = case_when(donate == 1 ~ 1,
donate == 2 ~ 0)) %>%
group_by(grp) %>%
mean_ci(donate, wt = commonweight, ci = .84) %>%
mutate(year = "2016") %>%
na.omit()
yyy4 <- cces18 %>%
mutate(grp = frcode(evangelical == 1 & white == 1 ~ "White\nEvangelical",
religpew == 9 ~ "Atheist")) %>%
mutate(donate = CC18_417a_6) %>%
mutate(donate = case_when(donate == 1 ~ 1,
donate == 2 ~ 0)) %>%
group_by(grp) %>%
mean_ci(donate, wt = commonweight, ci = .84) %>%
mutate(year = "2018") %>%
na.omit()
yyy5 <- cces20 %>%
mutate(grp = frcode(evangelical == 1 & white == 1 ~ "White\nEvangelical",
religpew == 9 ~ "Atheist")) %>%
mutate(donate = CC20_430a_6) %>%
mutate(donate = case_when(donate == 1 ~ 1,
donate == 2 ~ 0)) %>%
group_by(grp) %>%
mean_ci(donate, wt = commonweight, ci = .84) %>%
mutate(year = "2020") %>%
na.omit()
yyy6 <- cces22 %>%
mutate(grp = frcode(evangelical == 1 & white == 1 ~ "White\nEvangelical",
religpew == 9 ~ "Atheist")) %>%
mutate(donate = CC22_430a_6) %>%
mutate(donate = case_when(donate == 1 ~ 1,
donate == 2 ~ 0)) %>%
group_by(grp) %>%
mean_ci(donate, wt = commonweight, ci = .84) %>%
mutate(year = "2022") %>%
na.omit()
graph <- bind_df("yyy")
graph %>%
mutate(lab = round(mean, 2)) %>%
ggplot(., aes(x = grp, y = mean, fill = grp)) +
geom_col(color = "black") +
facet_wrap(~ year) +
geom_text(aes(y = mean + .05, label = paste0(lab*100, '%')), position = position_dodge(width = .9), size = 7, family = "font") +
scale_y_continuous(labels = percent, limits = c(0, .60)) +
theme_rb() +
theme(strip.text = element_text(size = 18)) +
scale_fill_manual(values = c("#f15a24", "#285e80")) +
labs(x = "", y = "", title = "During the Past Year, I Donated Money to\na Candidate/Campaign", caption = "@ryanburge\nData: Cooperative Election Study 2012-2022")
save("donate_money22_new.png", wd = 5)
graph <- cces20 %>%
mutate(grp = frcode(evangelical == 1 & white == 1 ~ "White Evangelical",
religpew == 9 ~ "Atheist")) %>%
filter(CC20_430a_6 != "NA") %>%
mutate(donate = CC20_430a_6) %>%
mutate(donate = frcode(donate == 1 ~ "Donated",
donate == 2 ~ "Did Not Donate")) %>%
filter(grp != "NA") %>%
group_by(grp) %>%
ct(donate, wt = commonweight, show_na = FALSE) %>%
mutate(pct = n/51551) %>%
mutate(labs = paste(grp, donate, sep = '-')) %>%
mutate(lab = round(pct, 3))
graph %>%
ggplot(., aes(x = labs, y = lab, fill = labs)) +
geom_col(color = "black") +
coord_flip() +
theme_rb() +
scale_fill_manual(values = c("#63C5DA", "#051094","#BC544B", "#60100B")) +
scale_y_continuous(labels = percent, limits = c(0, .15)) +
geom_text(aes(y = lab + .01, label = paste0(lab*100, '%')), position = position_dodge(width = .9), size = 8, family = "font") +
labs(x = "", y = "", title = "Share of the Full Sample That Donated/Not Donated to Candidate in 2020", caption = "@ryanburge\nData: Cooperative Election Study, 2020")
save("share_donating.png", ht = 3.5)
rfun <- function(var1, var2){
cces20 %>%
mutate(vv = {{var1}}) %>%
mutate({{var2}} := case_when(vv == 1 ~ 1,
vv == 2 ~ 0))
}
cces20 <- cces20 %>%
rfun(CC20_430a_1, act1) %>%
rfun(CC20_430a_2, act2) %>%
rfun(CC20_430a_3, act3) %>%
rfun(CC20_430a_4, act4) %>%
rfun(CC20_430a_5, act5) %>%
rfun(CC20_430a_6, act6) %>%
mutate(all = act1 + act2 + act3 + act4 + act5 + act6)
graph <- cces20 %>%
cces_trad(religpew) %>%
group_by(trad2) %>%
mean_ci(all, wt = commonweight, ci = .84)
graph %>%
ggplot(., aes(x = reorder(trad2, mean), y = mean, color = trad2)) +
geom_errorbar(aes(ymin=lower, ymax=upper), width=0, position=position_dodge(.9), linewidth = 1) +
geom_point(stroke = 1, shape = 21, fill = "white", size = 4) +
coord_flip() +
theme_rb() +
scale_color_manual(values = c(met.brewer("Johnson", 16))) +
labs(x = "", y = "", title = "Average Number of Political Acts in 2020 - Max is 6", caption = "@ryanburge\nData: Cooperative Election Study, 2020")
save("pol_acts_mean_20.png", ht = 5)
vars <- syms(c("CC20_430a_1","CC20_430a_2","CC20_430a_3","CC20_430a_4","CC20_430a_5","CC20_430a_6"))
cces_educ2 <- function(df, var){
df %>%
mutate(educ = frcode({{var}} == 1 | {{var}} == 2 ~ "HS or Less",
{{var}} == 3 ~ "Some College",
{{var}} == 4 ~ "2 Yr.",
{{var}} == 5 ~ "4 Yr.",
{{var}} == 6 ~ "Post-Grad"))
}
reg <- cces20 %>%
filter(faminc_new <= 16) %>%
mutate(act1 = car::recode(CC20_430a_1, "1=1; 2=0")) %>%
mutate(act2 = car::recode(CC20_430a_2, "1=1; 2=0")) %>%
mutate(act3 = car::recode(CC20_430a_3, "1=1; 2=0")) %>%
mutate(act4 = car::recode(CC20_430a_4, "1=1; 2=0")) %>%
mutate(act5 = car::recode(CC20_430a_5, "1=1; 2=0")) %>%
mutate(act6 = car::recode(CC20_430a_6, "1=1; 2=0")) %>%
mutate(acts = act1 + act2 + act3 + act4 + act5 + act6) %>%
mutate(trad = frcode(evangelical == 1 & race == 1 ~ "White Evangelical",
evangelical == 1 & race != 1 ~ "Non-White Evangelical",
mainline == 1 ~ "Mainline",
bprot == 1 ~ "Black Prot.",
religpew == 2 & race == 1 ~ "White Catholic",
religpew == 2 & race != 1 ~ "Non-White Catholic",
religpew == 3 ~ "Mormon",
religpew == 4 ~ "Orthodox",
religpew == 5 ~ "Jewish",
religpew == 6 ~ "Muslim",
religpew == 7 ~ "Buddhist",
religpew == 8 ~ "Hindu",
religpew == 9 ~ "Atheist",
religpew == 10 ~ "Agnostic",
religpew == 11 ~ "Nothing in Particular",
religpew == 12 ~ "Something Else",
TRUE ~ "Unclassified")) %>%
mutate(trad2 = frcode(religpew == 1 & pew_bornagain == 1 ~ "Evangelical Prot.",
religpew == 1 & pew_bornagain == 2 ~ "Non-Evangelical Prot.",
religpew == 2 ~ "Catholic",
religpew == 5 ~ "Jewish",
religpew == 9 ~ "Atheist")) %>%
mutate(male = car::recode(gender, "1=1; else =0")) %>%
mutate(white = car::recode(race, "1=1; else =0")) %>%
mutate(income = faminc_new) %>%
mutate(pid2 = frcode(pid3 == 1 ~ "Democrat",
pid3 == 2 ~ "Republican")) %>%
mutate(age = 2020- birthyr) %>%
cces_educ2(educ) %>%
select(acts, trad, trad2, male, white, age, income, pid2, educ, act1, act2, act3, act4, act5, act6)
reg1 <- lm(acts ~ educ*trad2 + male + white + income + age, data = reg)
# gg <- interact_plot(reg1, pred= educ, modx= trad2, modx.labels = c("Protestant", "Catholic", "Atheist", "Agnostic"), int.width = .76, interval = TRUE)
graph <- cat_plot(reg1, pred = educ, modx = trad2, interval = TRUE, int.width = .76, errorbar.width = .1, geom = "bar")
graph +
theme_rb(legend = TRUE) +
labs(x = "Highest Level of Education", y = "", title = "Number of Political Acts in 2020 Election", subtitle = "Regression model with controls for: gender, race, income, and age included",
caption = "@ryanburge\nData: Cooperative Election Study 2020")
save("pol_acts_regression.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment