Skip to content

Instantly share code, notes, and snippets.

@ryanburge
Created June 18, 2024 03:27
Show Gist options
  • Save ryanburge/fa15be85e7e013c9950010d321986c1a to your computer and use it in GitHub Desktop.
Save ryanburge/fa15be85e7e013c9950010d321986c1a to your computer and use it in GitHub Desktop.
aaa1 <- cces15 %>%
cces_pid3(pid7) %>%
group_by(pid3) %>%
mutate(ab = CC15_322f) %>%
mutate(ab = case_when(ab == 1 ~ 1,
ab == 2 ~ 0)) %>%
mean_ci(ab, wt = weight, ci = .84) %>%
mutate(year = "2015")
aaa2 <- cces16 %>%
cces_pid3(pid7) %>%
group_by(pid3) %>%
mutate(ab = CC16_332f) %>%
mutate(ab = case_when(ab == 1 ~ 1,
ab == 2 ~ 0)) %>%
mean_ci(ab, wt = commonweight_vv, ci = .84) %>%
mutate(year = "2016")
aaa3 <- cces17 %>%
cces_pid3(pid7) %>%
group_by(pid3) %>%
mutate(ab = CC17_332f) %>%
mutate(ab = case_when(ab == 1 ~ 1,
ab == 2 ~ 0)) %>%
mean_ci(ab, wt = weights_common, ci = .84) %>%
mutate(year = "2017")
aaa4 <- cces18 %>%
cces_pid3(pid7) %>%
group_by(pid3) %>%
mutate(ab = CC18_321f) %>%
mutate(ab = case_when(ab == 1 ~ 1,
ab == 2 ~ 0)) %>%
mean_ci(ab, wt = commonweight, ci = .84) %>%
mutate(year = "2018")
aaa5 <- cces19 %>%
cces_pid3(pid7) %>%
group_by(pid3) %>%
mutate(ab = CC19_321e) %>%
mutate(ab = case_when(ab == 1 ~ 1,
ab == 2 ~ 0)) %>%
mean_ci(ab, wt = commonweight, ci = .84) %>%
mutate(year = "2019")
aaa6 <- cces20 %>%
cces_pid3(pid7) %>%
group_by(pid3) %>%
mutate(ab = CC20_332f) %>%
mutate(ab = case_when(ab == 1 ~ 1,
ab == 2 ~ 0)) %>%
mean_ci(ab, wt = commonweight, ci = .84) %>%
mutate(year = "2020")
aaa7 <- cces21 %>%
cces_pid3(pid7) %>%
group_by(pid3) %>%
mutate(ab = CC21_323e) %>%
mutate(ab = case_when(ab == 1 ~ 1,
ab == 2 ~ 0)) %>%
mean_ci(ab, wt = commonweight, ci = .84) %>%
mutate(year = "2021")
aaa8 <- cces22 %>%
cces_pid3(pid7) %>%
group_by(pid3) %>%
mutate(ab = CC22_332f) %>%
mutate(ab = case_when(ab == 1 ~ 1,
ab == 2 ~ 0)) %>%
mean_ci(ab, wt = commonweight, ci = .84) %>%
mutate(year = "2022")
aaa9 <- cces23 %>%
cces_pid3(pid7) %>%
group_by(pid3) %>%
mutate(ab = CC23_324c) %>%
mutate(ab = case_when(ab == 1 ~ 1,
ab == 2 ~ 0)) %>%
mean_ci(ab, wt = commonweight, ci = .84) %>%
mutate(year = "2023")
graph <- bind_df("aaa") %>% filter(pid3 != "NA")
graph %>%
mutate(year = as.numeric(year)) %>%
mutate(lab = round(mean, 2)) %>%
ggplot(., aes(x = year, y = mean, fill = pid3)) +
geom_col(color = "black", position= "dodge") +
error_bar() +
facet_wrap(~ pid3, nrow = 3) +
lab_bar(top = FALSE, type = lab, pos = .025, sz =6) +
geom_vline(xintercept = 2021.5, linetype = "dashed", color = "black") + # Add vertical line
theme_rb() +
y_pct() +
pid3_fill() +
theme(strip.text = element_text(size = 22)) +
labs(x = "Year", y = "", title = "Share of Each Party in Favor of Making Abortion\nIllegal in All Circumstances", caption = "@ryanburge\nData: Cooperative Election Study, 2015-2023")
save("pid3_ab_illegal_line.png", wd = 5, ht = 10)
aaa1 <- cces15 %>%
cces_trad(religpew) %>%
group_by(trad2) %>%
mutate(ab = CC15_322f) %>%
mutate(ab = case_when(ab == 1 ~ 1,
ab == 2 ~ 0)) %>%
mean_ci(ab, wt = weight, ci = .84) %>%
mutate(year = "2015")
aaa2 <- cces16 %>%
cces_trad(religpew) %>%
group_by(trad2) %>%
mutate(ab = CC16_332f) %>%
mutate(ab = case_when(ab == 1 ~ 1,
ab == 2 ~ 0)) %>%
mean_ci(ab, wt = commonweight_vv, ci = .84) %>%
mutate(year = "2016")
aaa3 <- cces17 %>%
cces_trad(religpew) %>%
group_by(trad2) %>%
mutate(ab = CC17_332f) %>%
mutate(ab = case_when(ab == 1 ~ 1,
ab == 2 ~ 0)) %>%
mean_ci(ab, wt = weights_common, ci = .84) %>%
mutate(year = "2017")
aaa4 <- cces18 %>%
cces_trad(religpew) %>%
group_by(trad2) %>%
mutate(ab = CC18_321f) %>%
mutate(ab = case_when(ab == 1 ~ 1,
ab == 2 ~ 0)) %>%
mean_ci(ab, wt = commonweight, ci = .84) %>%
mutate(year = "2018")
aaa5 <- cces19 %>%
cces_trad(religpew) %>%
group_by(trad2) %>%
mutate(ab = CC19_321e) %>%
mutate(ab = case_when(ab == 1 ~ 1,
ab == 2 ~ 0)) %>%
mean_ci(ab, wt = commonweight, ci = .84) %>%
mutate(year = "2019")
aaa6 <- cces20 %>%
cces_trad(religpew) %>%
group_by(trad2) %>%
mutate(ab = CC20_332f) %>%
mutate(ab = case_when(ab == 1 ~ 1,
ab == 2 ~ 0)) %>%
mean_ci(ab, wt = commonweight, ci = .84) %>%
mutate(year = "2020")
aaa7 <- cces21 %>%
cces_trad(religpew) %>%
group_by(trad2) %>%
mutate(ab = CC21_323e) %>%
mutate(ab = case_when(ab == 1 ~ 1,
ab == 2 ~ 0)) %>%
mean_ci(ab, wt = commonweight, ci = .84) %>%
mutate(year = "2021")
aaa8 <- cces22 %>%
cces_trad(religpew) %>%
group_by(trad2) %>%
mutate(ab = CC22_332f) %>%
mutate(ab = case_when(ab == 1 ~ 1,
ab == 2 ~ 0)) %>%
mean_ci(ab, wt = commonweight, ci = .84) %>%
mutate(year = "2022")
aaa9 <- cces23 %>%
cces_trad(religpew) %>%
group_by(trad2) %>%
mutate(ab = CC23_324c) %>%
mutate(ab = case_when(ab == 1 ~ 1,
ab == 2 ~ 0)) %>%
mean_ci(ab, wt = commonweight, ci = .84) %>%
mutate(year = "2023")
graph <- bind_df("aaa") %>% filter(trad2 != "NA")
graph %>%
mutate(year = as.numeric(as.character(year)), # Convert year to numeric
lab = round(mean, 2)) %>%
ggplot(., aes(x = year, y = mean, fill = trad2)) +
geom_col(color = "black", position= "dodge") +
error_bar() +
facet_wrap(~ trad2) +
theme_rb() +
y_pct() +
geom_vline(xintercept = 2021.5, linetype = "dashed", color = "black") + # Add vertical line
scale_x_continuous(breaks = seq(min(graph$year), max(graph$year), by = 2)) +
scale_fill_manual(values = c(moma.colors("Panton", 16))) +
labs(x = "Year", y = "", title = "Share of Each Religious Tradition in Favor of Making Abortion\nIllegal in All Circumstances", caption = "@ryanburge\nData: Cooperative Election Study, 2015-2023")
save("trad2_ab_illegal.png", wd = 8, ht = 10)
one <- cces20 %>%
mutate(age = 2020 - birthyr) %>%
mutate(ab = CC20_332f) %>%
mutate(ab = case_when(ab == 1 ~ 1,
ab == 2 ~ 0)) %>%
select(birthyr, ab, weight = commonweight)
two <- cces21 %>%
mutate(age = 2021 - birthyr) %>%
mutate(ab = CC21_323e) %>%
mutate(ab = case_when(ab == 1 ~ 1,
ab == 2 ~ 0)) %>%
select(birthyr, ab, weight = commonweight)
gg1 <- bind_rows(one, two) %>%
mutate(year = "Before")
one <- cces22 %>%
mutate(age = 2022 - birthyr) %>%
mutate(ab = CC22_332f) %>%
mutate(ab = case_when(ab == 1 ~ 1,
ab == 2 ~ 0)) %>%
select(birthyr, ab, weight = commonweight)
two <- cces23 %>%
mutate(ab = CC23_324c) %>%
mutate(ab = case_when(ab == 1 ~ 1,
ab == 2 ~ 0)) %>%
select(birthyr, ab, weight = commonweight)
gg2 <- bind_rows(one, two) %>%
mutate(year = "After")
both <- bind_rows(gg1, gg2)
graph <- both %>%
filter(birthyr >= 1940) %>%
mutate(cohorts = frcode(birthyr >= 1925 & birthyr <= 1929 ~ "1925-1929",
birthyr >= 1930 & birthyr <= 1934 ~ "1930-1934",
birthyr >= 1935 & birthyr <= 1939 ~ "1935-1939",
birthyr >= 1940 & birthyr <= 1944 ~ "1940-1944",
birthyr >= 1945 & birthyr <= 1949 ~ "1945-1949",
birthyr >= 1950 & birthyr <= 1954 ~ "1950-1954",
birthyr >= 1955 & birthyr <= 1959 ~ "1955-1959",
birthyr >= 1960 & birthyr <= 1964 ~ "1960-1964",
birthyr >= 1965 & birthyr <= 1969 ~ "1965-1969",
birthyr >= 1970 & birthyr <= 1974 ~ "1970-1974",
birthyr >= 1975 & birthyr <= 1979 ~ "1975-1979",
birthyr >= 1980 & birthyr <= 1984 ~ "1980-1984",
birthyr >= 1985 & birthyr <= 1989 ~ "1985-1989",
birthyr >= 1990 & birthyr <= 1994 ~ "1990-1994",
birthyr >= 1995 & birthyr <= 2000 ~ "1995-2000")) %>%
group_by(cohorts, year) %>%
mean_ci(ab, wt = weight) %>% filter(cohorts != "NA")
graph %>%
mutate(lab = round(mean, 2)) %>%
ggplot(., aes(x = fct_rev(year), y = mean, fill = year)) +
geom_col(color = "black") +
facet_wrap(~ cohorts) +
error_bar() +
scale_fill_calc() +
lab_bar(top = FALSE, type = lab, pos = .02, sz = 8) +
geom_text(aes(y = .02, label = ifelse(year == "After", paste0(lab*100, '%'), "")), position = position_dodge(width = .9), size = 8, family = "font", color = "white") +
theme_rb() +
scale_y_continuous(labels = percent) +
labs(x = "Before (2020/2021) or After (2022/2023) Dobbs", y = "", title = "Share of Each Birth Cohort in Favor of Making Abortion\nIllegal in All Circumstances", caption = "@ryanburge\nData: Cooperative Election Study, 2020-2023")
save("cohort_ban_abortion.png", ht = 10, wd = 7)
aaa1 <- cces15 %>%
cces_pid3(pid7) %>%
group_by(pid3) %>%
mutate(ab = CC15_322a) %>%
mutate(ab = case_when(ab == 1 ~ 1,
ab == 2 ~ 0)) %>%
mean_ci(ab, wt = weight, ci = .84) %>%
mutate(year = "2015")
aaa2 <- cces16 %>%
cces_pid3(pid7) %>%
group_by(pid3) %>%
mutate(ab = CC16_332a) %>%
mutate(ab = case_when(ab == 1 ~ 1,
ab == 2 ~ 0)) %>%
mean_ci(ab, wt = commonweight_vv, ci = .84) %>%
mutate(year = "2016")
aaa3 <- cces17 %>%
cces_pid3(pid7) %>%
group_by(pid3) %>%
mutate(ab = CC17_332a) %>%
mutate(ab = case_when(ab == 1 ~ 1,
ab == 2 ~ 0)) %>%
mean_ci(ab, wt = weights_common, ci = .84) %>%
mutate(year = "2017")
aaa4 <- cces18 %>%
cces_pid3(pid7) %>%
group_by(pid3) %>%
mutate(ab = CC18_321a) %>%
mutate(ab = case_when(ab == 1 ~ 1,
ab == 2 ~ 0)) %>%
mean_ci(ab, wt = commonweight, ci = .84) %>%
mutate(year = "2018")
aaa5 <- cces19 %>%
cces_pid3(pid7) %>%
group_by(pid3) %>%
mutate(ab = CC19_321a) %>%
mutate(ab = case_when(ab == 1 ~ 1,
ab == 2 ~ 0)) %>%
mean_ci(ab, wt = commonweight, ci = .84) %>%
mutate(year = "2019")
aaa6 <- cces20 %>%
cces_pid3(pid7) %>%
group_by(pid3) %>%
mutate(ab = CC20_332a) %>%
mutate(ab = case_when(ab == 1 ~ 1,
ab == 2 ~ 0)) %>%
mean_ci(ab, wt = commonweight, ci = .84) %>%
mutate(year = "2020")
aaa7 <- cces21 %>%
cces_pid3(pid7) %>%
group_by(pid3) %>%
mutate(ab = CC21_323a) %>%
mutate(ab = case_when(ab == 1 ~ 1,
ab == 2 ~ 0)) %>%
mean_ci(ab, wt = commonweight, ci = .84) %>%
mutate(year = "2021")
aaa8 <- cces22 %>%
cces_pid3(pid7) %>%
group_by(pid3) %>%
mutate(ab = CC22_332a) %>%
mutate(ab = case_when(ab == 1 ~ 1,
ab == 2 ~ 0)) %>%
mean_ci(ab, wt = commonweight, ci = .84) %>%
mutate(year = "2022")
aaa9 <- cces23 %>%
cces_pid3(pid7) %>%
group_by(pid3) %>%
mutate(ab = CC23_324a) %>%
mutate(ab = case_when(ab == 1 ~ 1,
ab == 2 ~ 0)) %>%
mean_ci(ab, wt = commonweight, ci = .84) %>%
mutate(year = "2023")
graph <- bind_df("aaa") %>% filter(pid3 != "NA")
graph %>%
mutate(year = as.numeric(year)) %>%
mutate(lab = round(mean, 2)) %>%
ggplot(., aes(x = year, y = mean, fill = pid3)) +
geom_col(color = "black", position= "dodge") +
error_bar() +
facet_wrap(~ pid3, nrow = 3) +
lab_bar(top = FALSE, type = lab, pos = .045, sz =6) +
geom_vline(xintercept = 2021.5, linetype = "dashed", color = "black") + # Add vertical line
theme_rb() +
y_pct() +
pid3_fill() +
theme(strip.text = element_text(size = 22)) +
labs(x = "Year", y = "", title = "Share of Each Birth Cohort in Favor Allowing a Woman to\nObtain an Abortion as a Matter of Choice for Any Reason", caption = "@ryanburge\nData: Cooperative Election Study, 2015-2023")
save("pid3_ab_legal_line.png", wd = 6, ht = 10)
aaa1 <- cces15 %>%
cces_trad(religpew) %>%
group_by(trad2) %>%
mutate(ab = CC15_322a) %>%
mutate(ab = case_when(ab == 1 ~ 1,
ab == 2 ~ 0)) %>%
mean_ci(ab, wt = weight, ci = .84) %>%
mutate(year = "2015")
aaa2 <- cces16 %>%
cces_trad(religpew) %>%
group_by(trad2) %>%
mutate(ab = CC16_332a) %>%
mutate(ab = case_when(ab == 1 ~ 1,
ab == 2 ~ 0)) %>%
mean_ci(ab, wt = commonweight_vv, ci = .84) %>%
mutate(year = "2016")
aaa3 <- cces17 %>%
cces_trad(religpew) %>%
group_by(trad2) %>%
mutate(ab = CC17_332a) %>%
mutate(ab = case_when(ab == 1 ~ 1,
ab == 2 ~ 0)) %>%
mean_ci(ab, wt = weights_common, ci = .84) %>%
mutate(year = "2017")
aaa4 <- cces18 %>%
cces_trad(religpew) %>%
group_by(trad2) %>%
mutate(ab = CC18_321a) %>%
mutate(ab = case_when(ab == 1 ~ 1,
ab == 2 ~ 0)) %>%
mean_ci(ab, wt = commonweight, ci = .84) %>%
mutate(year = "2018")
aaa5 <- cces19 %>%
cces_trad(religpew) %>%
group_by(trad2) %>%
mutate(ab = CC19_321a) %>%
mutate(ab = case_when(ab == 1 ~ 1,
ab == 2 ~ 0)) %>%
mean_ci(ab, wt = commonweight, ci = .84) %>%
mutate(year = "2019")
aaa6 <- cces20 %>%
cces_trad(religpew) %>%
group_by(trad2) %>%
mutate(ab = CC20_332a) %>%
mutate(ab = case_when(ab == 1 ~ 1,
ab == 2 ~ 0)) %>%
mean_ci(ab, wt = commonweight, ci = .84) %>%
mutate(year = "2020")
aaa7 <- cces21 %>%
cces_trad(religpew) %>%
group_by(trad2) %>%
mutate(ab = CC21_323a) %>%
mutate(ab = case_when(ab == 1 ~ 1,
ab == 2 ~ 0)) %>%
mean_ci(ab, wt = commonweight, ci = .84) %>%
mutate(year = "2021")
aaa8 <- cces22 %>%
cces_trad(religpew) %>%
group_by(trad2) %>%
mutate(ab = CC22_332a) %>%
mutate(ab = case_when(ab == 1 ~ 1,
ab == 2 ~ 0)) %>%
mean_ci(ab, wt = commonweight, ci = .84) %>%
mutate(year = "2022")
aaa9 <- cces23 %>%
cces_trad(religpew) %>%
group_by(trad2) %>%
mutate(ab = CC23_324a) %>%
mutate(ab = case_when(ab == 1 ~ 1,
ab == 2 ~ 0)) %>%
mean_ci(ab, wt = commonweight, ci = .84) %>%
mutate(year = "2023")
graph <- bind_df("aaa") %>% filter(trad2 != "NA")
graph %>%
mutate(year = as.numeric(as.character(year)), # Convert year to numeric
lab = round(mean, 2)) %>%
ggplot(., aes(x = year, y = mean, fill = trad2)) +
geom_col(color = "black", position= "dodge") +
error_bar() +
facet_wrap(~ trad2) +
theme_rb() +
y_pct() +
geom_vline(xintercept = 2021.5, linetype = "dashed", color = "black") + # Add vertical line
scale_x_continuous(breaks = seq(min(graph$year), max(graph$year), by = 2)) +
scale_fill_manual(values = c(moma.colors("ustwo", 16))) +
labs(x = "Year", y = "", title = "Share of Each Birth Cohort in Favor Allowing a Woman to\nObtain an Abortion as a Matter of Choice for Any Reason", caption = "@ryanburge\nData: Cooperative Election Study, 2015-2023")
save("trad2_ab_legal.png", wd = 8, ht = 10)
one <- cces20 %>%
mutate(age = 2020 - birthyr) %>%
mutate(ab = CC20_332a) %>%
mutate(ab = case_when(ab == 1 ~ 1,
ab == 2 ~ 0)) %>%
select(birthyr, ab, weight = commonweight)
two <- cces21 %>%
mutate(age = 2021 - birthyr) %>%
mutate(ab = CC21_323a) %>%
mutate(ab = case_when(ab == 1 ~ 1,
ab == 2 ~ 0)) %>%
select(birthyr, ab, weight = commonweight)
gg1 <- bind_rows(one, two) %>%
mutate(year = "Before")
one <- cces22 %>%
mutate(age = 2022 - birthyr) %>%
mutate(ab = CC22_332a) %>%
mutate(ab = case_when(ab == 1 ~ 1,
ab == 2 ~ 0)) %>%
select(birthyr, ab, weight = commonweight)
two <- cces23 %>%
mutate(ab = CC23_324a) %>%
mutate(ab = case_when(ab == 1 ~ 1,
ab == 2 ~ 0)) %>%
select(birthyr, ab, weight = commonweight)
gg2 <- bind_rows(one, two) %>%
mutate(year = "After")
both <- bind_rows(gg1, gg2)
graph <- both %>%
filter(birthyr >= 1940) %>%
mutate(cohorts = frcode(birthyr >= 1925 & birthyr <= 1929 ~ "1925-1929",
birthyr >= 1930 & birthyr <= 1934 ~ "1930-1934",
birthyr >= 1935 & birthyr <= 1939 ~ "1935-1939",
birthyr >= 1940 & birthyr <= 1944 ~ "1940-1944",
birthyr >= 1945 & birthyr <= 1949 ~ "1945-1949",
birthyr >= 1950 & birthyr <= 1954 ~ "1950-1954",
birthyr >= 1955 & birthyr <= 1959 ~ "1955-1959",
birthyr >= 1960 & birthyr <= 1964 ~ "1960-1964",
birthyr >= 1965 & birthyr <= 1969 ~ "1965-1969",
birthyr >= 1970 & birthyr <= 1974 ~ "1970-1974",
birthyr >= 1975 & birthyr <= 1979 ~ "1975-1979",
birthyr >= 1980 & birthyr <= 1984 ~ "1980-1984",
birthyr >= 1985 & birthyr <= 1989 ~ "1985-1989",
birthyr >= 1990 & birthyr <= 1994 ~ "1990-1994",
birthyr >= 1995 & birthyr <= 2000 ~ "1995-2000")) %>%
group_by(cohorts, year) %>%
mean_ci(ab, wt = weight) %>% filter(cohorts != "NA")
graph %>%
mutate(lab = round(mean, 2)) %>%
ggplot(., aes(x = fct_rev(year), y = mean, fill = year)) +
geom_col(color = "black") +
facet_wrap(~ cohorts) +
error_bar() +
scale_fill_tableau() +
lab_bar(top = FALSE, type = lab, pos = .065, sz = 8) +
geom_text(aes(y = .065, label = ifelse(year == "After", paste0(lab*100, '%'), "")), position = position_dodge(width = .9), size = 8, family = "font", color = "white") +
theme_rb() +
scale_y_continuous(labels = percent) +
labs(x = "Before (2020/2021) or After (2022/2023) Dobbs", y = "", title = "Share of Each Birth Cohort in Favor Allowing a Woman to\nObtain an Abortion as a Matter of Choice for Any Reason", caption = "@ryanburge\nData: Cooperative Election Study, 2020-2023")
save("cohort_allow_abortion.png", ht = 9, wd = 7)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment