Skip to content

Instantly share code, notes, and snippets.

@ryanburge
Created April 12, 2023 19:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ryanburge/4422ea6adecb00bc9f60e8760e7fc2aa to your computer and use it in GitHub Desktop.
Save ryanburge/4422ea6adecb00bc9f60e8760e7fc2aa to your computer and use it in GitHub Desktop.
ggg1 <- cces08 %>%
mutate(rel = frcode(religpew == 9 ~ "Atheist",
religpew == 10 ~ "Agnostic",
religpew == 11 ~ "Nothing in Particular")) %>%
mutate(vote = CC410) %>%
mutate(vote = frcode(vote == 1 ~ "Republican",
vote == 2 ~ "Democrat",
vote == 3 | vote == 4 | vote == 5 | vote == 6 ~ "Third Party")) %>%
group_by(rel) %>%
ct(vote, wt = V201, show_na = FALSE) %>%
mutate(year = 2008)
ggg2 <- cces12 %>%
mutate(rel = frcode(religpew == 9 ~ "Atheist",
religpew == 10 ~ "Agnostic",
religpew == 11 ~ "Nothing in Particular")) %>%
mutate(vote = CC410a) %>%
mutate(vote = frcode(vote == 1 ~ "Democrat",
vote == 2 ~ "Republican",
vote == 4 ~ "Third Party")) %>%
group_by(rel) %>%
ct(vote, wt = weight_vv_post, show_na = FALSE) %>%
mutate(year = 2012)
ggg3 <- cces16 %>%
mutate(rel = frcode(religpew == 9 ~ "Atheist",
religpew == 10 ~ "Agnostic",
religpew == 11 ~ "Nothing in Particular")) %>%
mutate(vote = CC16_410a) %>%
mutate(vote = frcode(vote == 1 ~ "Republican",
vote == 2 ~ "Democrat",
vote == 3 | vote == 4 | vote == 5 | vote == 8 ~ "Third Party")) %>%
group_by(rel) %>%
ct(vote, wt = commonweight_vv_post, show_na = FALSE) %>%
mutate(year = 2016)
ggg4 <- cces20 %>%
mutate(rel = frcode(religpew == 9 ~ "Atheist",
religpew == 10 ~ "Agnostic",
religpew == 11 ~ "Nothing in Particular")) %>%
mutate(vote = CC20_410) %>%
mutate(vote = frcode(vote == 1 ~ "Democrat",
vote == 2 ~ "Republican",
vote == 4 ~ "Third Party")) %>%
group_by(rel) %>%
ct(vote, wt = commonpostweight, show_na = FALSE) %>%
mutate(year = 2020)
graph <- bind_rows(ggg1, ggg2, ggg3, ggg4)
graph %>%
filter(rel != "NA") %>%
mutate(year = as.factor(year)) %>%
mutate(lab = round(pct, 2)) %>%
ggplot(., aes(x = fct_rev(year), y = pct, fill = fct_rev(vote))) +
geom_col(color = "black") +
coord_flip() +
facet_wrap(~ rel, ncol =1, strip.position = "top") +
scale_fill_manual(values = c("darkorchid", "dodgerblue3", "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(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") +
labs(x = "", y = "", title = "Presidential Vote Choice Among the Non-Religious ", subtitle = "", caption = "@ryanburge\nData: Cooperative Election Study, 2008-2020")
save("nones_vote_all.png", wd = 6, ht = 6)
graph1 <- cces22 %>%
filter(pid7 <= 7) %>%
mutate(trad = frcode(evangelical == 1 & race == 1 ~ "White Evangelical",
evangelical == 1 & race != 1 ~ "Nonwhite Evangelical",
bprot == 1 ~ "Black Protestants",
mainline == 1 ~ "Mainline",
religpew == 2 & race == 1 ~ "White Catholic",
religpew == 2 & race != 1 ~ "Nonwhite 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")) %>%
group_by(trad) %>%
mean_ci(pid7, wt = commonweight) %>%
select(pid = mean, trad)
graph2 <- cces22 %>%
filter(ideo5 <= 7) %>%
mutate(trad = frcode(evangelical == 1 & race == 1 ~ "White Evangelical",
evangelical == 1 & race != 1 ~ "Nonwhite Evangelical",
bprot == 1 ~ "Black Protestants",
mainline == 1 ~ "Mainline",
religpew == 2 & race == 1 ~ "White Catholic",
religpew == 2 & race != 1 ~ "Nonwhite 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")) %>%
group_by(trad) %>%
mean_ci(ideo5, wt = commonweight) %>%
select(ideo = mean, trad)
gg1 <- left_join(graph1, graph2)
graph3 <- cces22 %>%
filter(pid7 <= 7) %>%
mean_ci(pid7, wt = commonweight) %>%
mutate(trad = "Entire Sample") %>%
select(pid = mean, trad)
graph4 <- cces22 %>%
filter(ideo5 <= 5) %>%
mean_ci(ideo5, wt = commonweight) %>%
mutate(trad = "Entire Sample") %>%
select(ideo = mean, trad)
gg2 <- left_join(graph3, graph4)
gg <- bind_rows(gg1, gg2) %>%
mutate(color = frcode(trad == "Atheist" | trad == "Agnostic" | trad == "Nothing in Particular" ~ "Yes", TRUE ~ "No"))
library(ggrepel)
gg <- gg %>%
filter(trad != "Unclassified") %>%
filter(trad != "Something Else")
gg %>%
ggplot(., aes(x = pid, y = ideo, color = color)) +
geom_point(shape = 21, stroke = 2) +
scale_x_continuous(limits = c(1,7), breaks = c(1,2,3,4,5,6,7), labels = c("Strong\nDem.", "Not Very\nStrong Dem.", "Lean\nDem.", "Independent", "Lean\nRep.", "Not Very\nStrong Rep.", "Strong\nRep.")) +
scale_y_continuous(limits = c(1,5), breaks = c(1,2,3,4,5), labels = c("Very\nLiberal", "Liberal", "Moderate", "Conservative", "Very\nConservative")) +
theme_rb() +
scale_color_manual(values = c( "darkblue", "orange")) +
theme(panel.grid.major = element_line(colour = "white")) +
geom_text_repel(data = gg, aes(x = pid, y = ideo, label = trad), family = "font") +
labs(x = "Partisanship", y = "Ideology", title = "Placing The Nones in Ideological and Partisan Space", caption = "@ryanburge\nData: Cooperative Election Study, 2008-2022")
save("nones_scatter.png")
cces <- read.fst("E://data/cces_abort21.fst")
yyy1 <- cces %>%
mutate(ab = case_when(ab_choice == 1 ~ 1,
ab_choice == 2 ~ 0)) %>%
filter(trad2 == "Atheist" | trad2 == "Agnostic" | trad2 == "Nothing in Particular") %>%
group_by(year, trad2) %>%
mean_ci(ab, wt = weight, ci = .84)
yyy2 <- cces22 %>%
mutate(ab = case_when(CC22_332a == 1 ~ 1,
CC22_332a == 2 ~ 0)) %>%
mutate(trad2 = frcode(religpew == 9 ~ "Atheist",
religpew == 10 ~ "Agnostic",
religpew == 11 ~ "Nothing in Particular")) %>%
group_by(trad2) %>%
mean_ci(ab, wt = commonweight, ci = .84) %>%
mutate(year = 2022)
graph1 <- bind_rows(yyy1, yyy2) %>% mutate(type = "Any Reason")
yyy1 <- cces %>%
mutate(ab = case_when(ab_rape == 1 ~ 1,
ab_rape == 2 ~ 0)) %>%
filter(trad2 == "Atheist" | trad2 == "Agnostic" | trad2 == "Nothing in Particular") %>%
group_by(year, trad2) %>%
mean_ci(ab, wt = weight, ci = .84)
yyy2 <- cces22 %>%
mutate(ab = case_when(CC22_332b == 1 ~ 1,
CC22_332b == 2 ~ 0)) %>%
mutate(trad2 = frcode(religpew == 9 ~ "Atheist",
religpew == 10 ~ "Agnostic",
religpew == 11 ~ "Nothing in Particular")) %>%
group_by(trad2) %>%
mean_ci(ab, wt = commonweight, ci = .84) %>%
mutate(year = 2022)
graph2 <- bind_rows(yyy1, yyy2) %>% mutate(type = "Only For Rape, Incest, Life of Mother")
yyy1 <- cces %>%
mutate(ab = case_when(ab_late == 1 ~ 1,
ab_late == 2 ~ 0)) %>%
filter(trad2 == "Atheist" | trad2 == "Agnostic" | trad2 == "Nothing in Particular") %>%
group_by(year, trad2) %>%
mean_ci(ab, wt = weight, ci = .84)
yyy2 <- cces22 %>%
mutate(ab = case_when(CC22_332c == 1 ~ 1,
CC22_332c == 2 ~ 0)) %>%
mutate(trad2 = frcode(religpew == 9 ~ "Atheist",
religpew == 10 ~ "Agnostic",
religpew == 11 ~ "Nothing in Particular")) %>%
group_by(trad2) %>%
mean_ci(ab, wt = commonweight, ci = .84) %>%
mutate(year = 2022)
graph3 <- bind_rows(yyy1, yyy2) %>% mutate(type = "Prohibit After 20 Weeks")
yyy1 <- cces %>%
mutate(ab = case_when(ab_ins == 1 ~ 1,
ab_ins == 2 ~ 0)) %>%
filter(trad2 == "Atheist" | trad2 == "Agnostic" | trad2 == "Nothing in Particular") %>%
group_by(year, trad2) %>%
mean_ci(ab, wt = weight, ci = .84)
yyy2 <- cces22 %>%
mutate(ab = case_when(CC22_332d == 1 ~ 1,
CC22_332d == 2 ~ 0)) %>%
mutate(trad2 = frcode(religpew == 9 ~ "Atheist",
religpew == 10 ~ "Agnostic",
religpew == 11 ~ "Nothing in Particular")) %>%
group_by(trad2) %>%
mean_ci(ab, wt = commonweight, ci = .84) %>%
mutate(year = 2022)
graph4 <- bind_rows(yyy1, yyy2) %>% mutate(type = "Employers Can Deny Coverage\n For Abortion in Insurance Plans")
yyy1 <- cces %>%
mutate(ab = case_when(ab_funds == 1 ~ 1,
ab_funds == 2 ~ 0)) %>%
filter(trad2 == "Atheist" | trad2 == "Agnostic" | trad2 == "Nothing in Particular") %>%
group_by(year, trad2) %>%
mean_ci(ab, wt = weight, ci = .84)
yyy2 <- cces22 %>%
mutate(ab = case_when(CC22_332e == 1 ~ 1,
CC22_332e == 2 ~ 0)) %>%
mutate(trad2 = frcode(religpew == 9 ~ "Atheist",
religpew == 10 ~ "Agnostic",
religpew == 11 ~ "Nothing in Particular")) %>%
group_by(trad2) %>%
mean_ci(ab, wt = commonweight, ci = .84) %>%
mutate(year = 2022)
graph5 <- bind_rows(yyy1, yyy2) %>% mutate(type = "Prohibit Federal Funds for Abortion")
yyy1 <- cces %>%
mutate(ab = case_when(ab_never == 1 ~ 1,
ab_never == 2 ~ 0)) %>%
filter(trad2 == "Atheist" | trad2 == "Agnostic" | trad2 == "Nothing in Particular") %>%
group_by(year, trad2) %>%
mean_ci(ab, wt = weight, ci = .84)
yyy2 <- cces22 %>%
mutate(ab = case_when(CC22_332f == 1 ~ 1,
CC22_332f == 2 ~ 0)) %>%
mutate(trad2 = frcode(religpew == 9 ~ "Atheist",
religpew == 10 ~ "Agnostic",
religpew == 11 ~ "Nothing in Particular")) %>%
group_by(trad2) %>%
mean_ci(ab, wt = commonweight, ci = .84) %>%
mutate(year = 2022)
graph6 <- bind_rows(yyy1, yyy2) %>% mutate(type = "Illegal in All Circumstances")
all <- bind_df("graph") %>% na.omit()
all %>%
mutate(lab = round(mean, 2)) %>%
ggplot(., aes(x = year, y = mean, color = trad2, group = trad2)) +
geom_point() +
geom_line() +
facet_wrap(~ type) +
theme_rb(legend = TRUE) +
scale_color_gdocs() +
y_pct() +
scale_x_continuous(breaks = c(2012, 2014, 2016, 2018, 2020, 2022)) +
labs(x= "Year", y = "Share In Favor", title = "Abortion Public Opinion Since 2014 Among the Nones", caption = "@ryanburge\nData: Cooperative Election Study, 2014-2022")
save("ab_nones_2022.png")
fun <- function(df, var1, var2, var3, var4, weight, yr) {
aaa1 <- df %>%
filter({{var1}} <= 7) %>%
mutate(trad2 = frcode(religpew == 9 ~ "Atheist",
religpew == 10 ~ "Agnostic",
religpew == 11 ~ "Nothing in Particular")) %>%
group_by(trad2) %>%
mean_ci({{var1}}, wt = {{weight}}) %>%
mutate(year = yr) %>%
mutate(group = "Yourself")
aaa2 <- df %>%
filter({{var2}} <= 7) %>%
mutate(trad2 = frcode(religpew == 9 ~ "Atheist",
religpew == 10 ~ "Agnostic",
religpew == 11 ~ "Nothing in Particular")) %>%
group_by(trad2) %>%
mean_ci({{var2}}, wt = {{weight}}) %>%
mutate(year = yr) %>%
mutate(group = "Democrats")
aaa3 <- df %>%
filter({{var3}} <= 7) %>%
mutate(trad2 = frcode(religpew == 9 ~ "Atheist",
religpew == 10 ~ "Agnostic",
religpew == 11 ~ "Nothing in Particular")) %>%
group_by(trad2) %>%
mean_ci({{var3}}, wt = {{weight}}) %>%
mutate(year = yr) %>%
mutate(group = "Republicans")
aaa4 <- df %>%
filter({{var3}} <= 7) %>%
mutate(trad2 = frcode(religpew == 9 ~ "Atheist",
religpew == 10 ~ "Agnostic",
religpew == 11 ~ "Nothing in Particular")) %>%
group_by(trad2) %>%
mean_ci({{var4}}, wt = {{weight}}) %>%
mutate(year = yr) %>%
mutate(group = "Trump")
bind_rows(aaa1, aaa2, aaa3, aaa4)
}
yyy1 <- cces16 %>% fun(CC16_340a, CC16_340g, CC16_340h, CC16_340e, commonweight, yr = 2016)
yyy2 <- cces17 %>% fun(CC17_350a, CC17_350d, CC17_350e, CC17_350c, weights_common, yr = 2017)
yyy3 <- cces18 %>% fun(CC18_334A, CC18_334D, CC18_334E, CC18_334C, commonweight, yr = 2018)
yyy4 <- cces19 %>% fun(CC19_334a, CC19_334d, CC19_334e, CC19_334c, commonweight, yr = 2019)
yyy5 <- cces20 %>% fun(CC20_340a, CC20_340e, CC20_340f, CC20_340d, commonweight, yr = 2020)
yyy6 <- cces21 %>% fun(CC21_330a, CC21_330e, CC21_330f, CC21_330b, commonweight, yr = 2021) %>% filter(group != "Trump")
yyy7 <- cces22 %>% fun(CC22_340a, CC22_340e, CC22_340f, CC22_340d, commonweight, yr = 2022)
graph <- bind_df("yyy") %>% filter(trad2 != "NA")
graph$group <- factor(graph$group, levels = c("Democrats", "Yourself", "Trump", "Republicans"))
graph %>%
mutate(year = as.factor(year)) %>%
ggplot(., aes(x = fct_rev(year), y = mean, color = group, group = group)) +
geom_line() +
geom_errorbar(aes(ymin = lower, ymax=upper), size = 1, width = 0) +
geom_point(shape = 21, stroke = 2, fill = "white") +
coord_flip() +
facet_wrap(~ trad2, ncol = 1) +
scale_y_continuous(breaks = c(1,2,3,4,5,6,7), labels = c("Very\nLib", "Lib.", "Somewhat\nLib.", "Middle of\nthe Road", "Somewhat\nCons.", "Cons.", "Very\nCons"), limits = c(1,7)) +
scale_color_manual(values = c("dodgerblue3", "black", "darkorchid", "firebrick3")) +
theme_rb(legend = TRUE) +
labs(x = "", y = "", title = "Place Yourself, Democrats, Trump, and\nRepublicans in Ideological Space", subtitle = "Among the Nones",
caption = "@ryanburge\nData: Cooperative Election Study, 2016-2022")
save("your_self_dems_republicans_trump_nones22.png", ht = 8, wd = 5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment