Skip to content

Instantly share code, notes, and snippets.

@ryanburge
Created April 5, 2024 00:23
Show Gist options
  • Save ryanburge/e11540b2404d769ada123744616bef6c to your computer and use it in GitHub Desktop.
Save ryanburge/e11540b2404d769ada123744616bef6c to your computer and use it in GitHub Desktop.
ab_fun <- function(df, var, name){
var <- enquo(var)
ttt1 <- gss %>%
filter(year <= 2018) %>%
filter(reltrad == 4) %>%
mutate(any = case_when({{var}} == 1 ~ 1,
{{var}} == 2 ~ 0)) %>%
group_by(year) %>%
mean_ci(any, wt = wtssall) %>%
na.omit() %>%
mutate(type = name)
ttt2 <- gss %>%
filter(year == 2021 | year == 2022) %>%
filter(reltrad == 4) %>%
mutate(any = case_when({{var}} == 1 ~ 1,
{{var}} == 2 ~ 0)) %>%
group_by(year) %>%
mean_ci(any, wt = wtssnrps) %>%
na.omit() %>%
mutate(type = name)
bind_rows(ttt1, ttt2)
}
qqq1 <- gss %>% ab_fun(abnomore, "Wants No More Kids")
qqq2 <- gss %>% ab_fun(abdefect, "Serious Defect")
qqq3 <- gss %>% ab_fun(abhlth, "Mother's Health")
qqq4 <- gss %>% ab_fun(abpoor, "Can't Afford More")
qqq5 <- gss %>% ab_fun(abrape, "Result of Rape")
qqq6 <- gss %>% ab_fun(absingle, "Not Married")
graph <- bind_df("qqq")
graph %>%
ggplot(., aes(x = year, y = mean, color = type, group = type)) +
geom_point(stroke = .25, shape = 21, alpha = .5) +
geom_smooth(se = FALSE) +
facet_wrap(~ type)+
scale_color_manual(values = c("#033f63", "#28666e", "#150023", "#B5B682", "#FFA700", "#5f2680")) +
theme_rb() +
y_pct() +
theme(strip.text = element_text(size = 20)) +
labs(x = "Year", y = "", title = "Catholics Support for Legal Abortion in the Following Circumstances", caption = "@ryanburge + @religiondata\nData: General Social Survey, 1972-2022")
save("cath_abortion_views.png")
library(rio)
library(labelled)
fem <- import("E://data/fg_female.dta") %>% mutate(gender = "Female")
men <- import("E://data/fg_male.dta") %>% mutate(gender = "Male")
fg <- bind_rows(fem, men) %>% rename(weight = WGT2015_2017)
pill <- fg %>%
mutate(reltrad = frcode(reltrad == 1 ~ "Evangelical",
reltrad == 2 ~ "Mainline",
reltrad == 3 ~ "Black Prot.",
reltrad == 4 ~ "Catholic",
reltrad == 5 ~ "Other Religion",
reltrad == 6 ~ "No Religion")) %>%
mutate(pill = case_when(pill == 1 ~ 1,
pill == 5 ~ 0)) %>%
group_by(reltrad) %>%
mean_ci(pill, wt = weight, ci = .84) %>%
filter(reltrad != "NA") %>%
mutate(type = "Pill")
condom <- fg %>%
mutate(reltrad = frcode(reltrad == 1 ~ "Evangelical",
reltrad == 2 ~ "Mainline",
reltrad == 3 ~ "Black Prot.",
reltrad == 4 ~ "Catholic",
reltrad == 5 ~ "Other Religion",
reltrad == 6 ~ "No Religion")) %>%
mutate(pill = case_when(condom == 1 ~ 1,
condom == 5 ~ 0)) %>%
group_by(reltrad) %>%
mean_ci(pill, wt = weight, ci = .84) %>%
filter(reltrad != "NA") %>%
mutate(type = "Condoms")
depo <- fg %>%
mutate(reltrad = frcode(reltrad == 1 ~ "Evangelical",
reltrad == 2 ~ "Mainline",
reltrad == 3 ~ "Black Prot.",
reltrad == 4 ~ "Catholic",
reltrad == 5 ~ "Other Religion",
reltrad == 6 ~ "No Religion")) %>%
mutate(pill = case_when(depoprov == 1 ~ 1,
depoprov == 5 ~ 0)) %>%
group_by(reltrad) %>%
mean_ci(pill, wt = weight, ci = .84) %>%
filter(reltrad != "NA") %>%
mutate(type = "Depo Shot")
pull <- fg %>%
mutate(reltrad = frcode(reltrad == 1 ~ "Evangelical",
reltrad == 2 ~ "Mainline",
reltrad == 3 ~ "Black Prot.",
reltrad == 4 ~ "Catholic",
reltrad == 5 ~ "Other Religion",
reltrad == 6 ~ "No Religion")) %>%
mutate(pill = case_when(widrawal == 1 ~ 1,
widrawal == 5 ~ 0)) %>%
group_by(reltrad) %>%
mean_ci(pill, wt = weight, ci = .84) %>%
filter(reltrad != "NA") %>%
mutate(type = "Withdrawal")
rhythm <- fg %>%
mutate(reltrad = frcode(reltrad == 1 ~ "Evangelical",
reltrad == 2 ~ "Mainline",
reltrad == 3 ~ "Black Prot.",
reltrad == 4 ~ "Catholic",
reltrad == 5 ~ "Other Religion",
reltrad == 6 ~ "No Religion")) %>%
mutate(pill = case_when(rhythm == 1 ~ 1,
rhythm == 5 ~ 0)) %>%
group_by(reltrad) %>%
mean_ci(pill, wt = weight, ci = .84) %>%
filter(reltrad != "NA") %>%
mutate(type = "Rhythm")
patch <- fg %>%
mutate(reltrad = frcode(reltrad == 1 ~ "Evangelical",
reltrad == 2 ~ "Mainline",
reltrad == 3 ~ "Black Prot.",
reltrad == 4 ~ "Catholic",
reltrad == 5 ~ "Other Religion",
reltrad == 6 ~ "No Religion")) %>%
mutate(pill = case_when(patch == 1 ~ 1,
patch == 5 ~ 0)) %>%
group_by(reltrad) %>%
mean_ci(pill, wt = weight, ci = .84) %>%
filter(reltrad != "NA") %>%
mutate(type = "Patch")
ring <- fg %>%
mutate(reltrad = frcode(reltrad == 1 ~ "Evangelical",
reltrad == 2 ~ "Mainline",
reltrad == 3 ~ "Black Prot.",
reltrad == 4 ~ "Catholic",
reltrad == 5 ~ "Other Religion",
reltrad == 6 ~ "No Religion")) %>%
mutate(pill = case_when(ring == 1 ~ 1,
ring == 5 ~ 0)) %>%
group_by(reltrad) %>%
mean_ci(pill, wt = weight, ci = .84) %>%
filter(reltrad != "NA") %>%
mutate(type = "Ring")
planb <- fg %>%
mutate(reltrad = frcode(reltrad == 1 ~ "Evangelical",
reltrad == 2 ~ "Mainline",
reltrad == 3 ~ "Black Prot.",
reltrad == 4 ~ "Catholic",
reltrad == 5 ~ "Other Religion",
reltrad == 6 ~ "No Religion")) %>%
mutate(pill = case_when(mornpill == 1 ~ 1,
mornpill == 5 ~ 0)) %>%
group_by(reltrad) %>%
mean_ci(pill, wt = weight, ci = .84) %>%
filter(reltrad != "NA") %>%
mutate(type = "Plan B")
graph <- bind_rows(depo, pill, planb, pull, rhythm, condom)
graph$type <- factor(graph$type, levels = c("Condoms", "Pill", "Withdrawal", "Depo Shot", "Plan B", "Rhythm"))
graph %>%
mutate(lab = round(mean, 2)) %>%
ggplot(., aes(x = fct_rev(reltrad), y = mean, fill = reltrad)) +
geom_col(color = "black", alpha = .65) +
facet_wrap(~ type) +
coord_flip() +
y_pct() +
theme_rb() +
error_bar() +
scale_fill_manual(values = c("#033f63", "#28666e", "#e1e1e1", "#B5B682", "#FFA700", "#5f2680")) +
geom_text(aes(y = .1, label = ifelse(type == "Pill" | type == "Withdrawal" | type == "Plan B" | type == "Depo Shot" | type == "Condoms", paste0(lab*100, '%'), '')), position = position_dodge(width = .9), size = 6, family = "font") +
geom_text(aes(y = .1, label = ifelse(type == "Pill" & reltrad == "No Religion" | type == "Withdrawal" & reltrad == "No Religion" | type == "Plan B" & reltrad == "No Religion" | type == "Depo Shot" & reltrad == "No Religion" | type == "Condoms" & reltrad == "No Religion" , paste0(lab*100, '%'), '')), position = position_dodge(width = .9), size = 6, family = "font", color = "white") +
geom_text(aes(y = mean + .1, label = ifelse(type == "Rhythm" | type == "Ring", paste0(lab*100, '%'), '')), position = position_dodge(width = .9), size = 6, family = "font") +
labs(x = "", y = "", title = "Have You Ever Used the Following Forms of Contraception?", caption= "@ryanburge + @religiondata\nNational Survey of Family Growth, 2015-2017")
save("contra_type_reltrad_new.png")
aaa1 <- gss %>%
filter(year <= 2018) %>%
filter(reltrad == 4) %>%
mutate(cap = case_when(cappun == 1 ~ 1,
cappun == 2 ~ 0)) %>%
group_by(year) %>%
mean_ci(cap, wt = wtssall)
aaa2 <- gss %>%
filter(year > 2018) %>%
filter(reltrad == 4) %>%
mutate(cap = case_when(cappun == 1 ~ 1,
cappun == 2 ~ 0)) %>%
group_by(year) %>%
mean_ci(cap, wt = wtssnrps)
graph <- bind_rows(aaa1, aaa2)
graph %>%
filter(year >= 1974) %>%
ggplot(., aes(x = year, y = mean)) +
geom_line(color = "#5f2680") +
geom_point(stroke = 1, shape = 21, fill = "white", color = "#033f63") +
scale_y_continuous(labels = percent, limits = c(.40, .85)) +
theme_rb() +
add_text(x = 1974, y = .76, word = "74%", sz = 6) +
add_text(x = 1990, y = .83, word = "81%", sz = 6) +
add_text(x = 2006, y = .615, word = "63%", sz = 6) +
add_text(x = 2022, y = .63, word = "61%", sz = 6) +
labs(x = "Year", y = "", title = "Share of Catholics in Favor of the Death Penalty for Convicted Murderers", caption = "@ryanburge + @religiondata\nData: General Social Survey, 1974-2022")
save("catholic_cappun.png")
aaa1 <- gss %>%
filter(year <= 2018) %>%
filter(reltrad == 4) %>%
mutate(cap = case_when(suicide1 == 1 ~ 1,
suicide1 == 2 ~ 0)) %>%
group_by(year) %>%
mean_ci(cap, wt = wtssall)
aaa2 <- gss %>%
filter(year > 2018) %>%
filter(reltrad == 4) %>%
mutate(cap = case_when(suicide1 == 1 ~ 1,
suicide1 == 2 ~ 0)) %>%
group_by(year) %>%
mean_ci(cap, wt = wtssnrps)
graph <- bind_rows(aaa1, aaa2) %>% na.omit()
graph %>%
ggplot(., aes(x = year, y = mean)) +
geom_line(color = "#28666e") +
geom_point(stroke = 1, shape = 21, fill = "white", color = "#FFA700") +
scale_y_continuous(labels = percent, limits = c(0, .8)) +
theme_rb() +
add_text(x = 1977, y = .40, word = "36%", sz = 6) +
add_text(x = 1990, y = .64, word = "61%", sz = 6) +
add_text(x = 2012, y = .505, word = "53%", sz = 6) +
add_text(x = 2022, y = .725, word = "70%", sz = 6) +
labs(x = "Year", y = "", title = "Share of Catholics in Favor of Suicide in Case of Incurable Disease", caption = "@ryanburge + @religiondata\nData: General Social Survey, 1977-2022")
save("catholic_suicide.png")
aaa1 <- gss %>%
filter(year >=1977) %>%
filter(year <= 2018) %>%
filter(reltrad == 4) %>%
mutate(sui = case_when(suicide1 == 1 ~ 0,
suicide1 == 2 ~ 1)) %>%
mutate(cap = case_when(cappun == 1 ~ 0,
cappun == 2 ~ 1)) %>%
mutate(ab = case_when(abany == 1 ~ 0,
abany == 2 ~ 1)) %>%
mutate(all = sui + cap + ab) %>%
mutate(all1 = case_when(all == 3 ~ 1,
TRUE ~ 0)) %>%
group_by(year) %>%
mean_ci(all1, wt = wtssall) %>%
filter(mean > 0)
aaa2 <- gss %>%
filter(year > 2018) %>%
filter(reltrad == 4) %>%
mutate(sui = case_when(suicide1 == 1 ~ 0,
suicide1 == 2 ~ 1)) %>%
mutate(cap = case_when(cappun == 1 ~ 0,
cappun == 2 ~ 1)) %>%
mutate(ab = case_when(abany == 1 ~ 0,
abany == 2 ~ 1)) %>%
mutate(all = sui + cap + ab) %>%
mutate(all1 = case_when(all == 3 ~ 1,
TRUE ~ 0)) %>%
group_by(year) %>%
mean_ci(all1, wt = wtssnrps) %>%
filter(mean > 0)
graph <- bind_rows(aaa1, aaa2)
graph %>%
filter(year >= 1988) %>%
ggplot(., aes(x = year, y = mean)) +
geom_line(color = "#28666e") +
geom_point(stroke = 1, shape = 21, fill = "white", color = "#033f63") +
scale_y_continuous(labels = percent, limits = c(0, .1)) +
theme_rb() +
add_text(x = 1988, y = .032, word = "3.6%", sz = 6) +
add_text(x = 1998, y = .05, word = "4.6%", sz = 6) +
add_text(x = 2010, y = .066, word = "6.2%", sz = 6) +
add_text(x = 2022, y = .005, word = ".9%", sz = 6) +
labs(x = "Year", y = "", title = "Share of Catholics Who Oppose Capital Punishment, Abortion, and Euthanasia", caption = "@ryanburge + @religiondata\nData: General Social Survey, 1988-2022")
save("catholic_consistent_ethic.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment