Skip to content

Instantly share code, notes, and snippets.

@ryanburge
Created May 21, 2023 12:07
Show Gist options
  • Save ryanburge/4617b1dac0beda333488a22bc2f0e6f4 to your computer and use it in GitHub Desktop.
Save ryanburge/4617b1dac0beda333488a22bc2f0e6f4 to your computer and use it in GitHub Desktop.
graph <- cces %>%
filter(year >= 2020) %>%
mutate(rel = frcode(religion == 3 ~ "LDS",
religion == 9 ~ "Atheist")) %>%
mutate(kids = case_when(havekids == 1 ~ 1,
havekids == 2 ~ 0)) %>%
group_by(age, rel) %>%
mean_ci(kids, wt = weight) %>%
filter(rel != "NA")
graph %>%
filter(age <= 70) %>%
ggplot(., aes(x = age, y = mean, color = rel, group = rel)) +
geom_point(stroke = 1, shape = 21, alpha = .45) +
geom_labelsmooth(aes(label = rel), method = "loess", formula = y ~ x, family = "font", linewidth = 1, text_smoothing = 30, size = 4.5, linewidth = 1, boxlinewidth = 0.3, hjust = .35) +
scale_color_calc() +
theme_rb() +
scale_y_continuous(labels = percent, limits = c(0, .85)) +
labs(x = "", y = "", title = "Are you the parent or guardian of any children under the age of 18?", caption = "@ryanburge\nData: Cooperative Election Study, 2020-2022")
save("parent_ath_lds.png")
ggg <- cces %>%
filter(year >= 2020) %>%
filter(age >= 30 & age <= 45) %>%
cces_attend(pew_attendance) %>%
mutate(kids = case_when(havekids == 1 ~ 1,
havekids == 2 ~ 0)) %>%
cces_pid3(pid7) %>%
group_by(pid3, att) %>%
mean_ci(kids, wt = weight, ci = .84) %>%
filter(att != "NA") %>%
filter(pid3 != "NA")
ggg %>%
filter(pid3 != "Independent") %>%
mutate(lab = round(mean, 2)) %>%
ggplot(., aes(x = att, y = mean, fill = pid3)) +
geom_col(color = "black", position = "dodge") +
scale_fill_manual(values = c("dodgerblue3", "firebrick3")) +
theme_rb(legend = TRUE) +
error_bar() +
lab_bar(top = FALSE, type = lab, pos = .04, sz = 5.5) +
y_pct() +
theme(plot.title = element_text(size = 15)) +
labs(x = "Religious Attendance", y = "", title = "Share Who Have Children by Worship Attendance and Partisanship",
subtitle = "Among those 30-45 Years Old",
caption = "@ryanburge\nData: Cooperative Election Study, 2020-2022")
save("pid3_att_kids_ces.png", wd = 6)
ggg <- cces %>%
mutate(relig = frcode(religion == 1 ~ "Protestant",
religion == 2 ~ "Catholic",
religion >= 3 & religion <= 8 ~ "Other World Religions",
religion == 9 | religion == 10 ~ "Atheist/Agnostic",
religion == 11 ~ "Nothing in Particular")) %>%
mutate(yr = frcode(year == 2008 ~ "2008",
year == 2022 ~ "2022")) %>%
mutate(factor(year)) %>%
mutate(kids = case_when(havekids == 1 ~ 1,
havekids == 2 ~ 0)) %>%
group_by(age, yr, relig) %>%
mean_ci(kids, wt = weight)
ggg %>%
filter(relig != 'NA') %>%
filter(yr != 'NA') %>%
filter(age <= 70) %>%
ggplot(., aes(x = age, y = mean, color = yr, group = yr)) +
geom_smooth(se= FALSE) +
scale_color_tableau() +
theme_rb(legend = TRUE) +
facet_wrap(~ relig) +
scale_y_continuous(labels = percent, limits = c(0, .65)) +
labs(x = "Age", y = "", title = "Are you the parent or guardian of any children under the age of 18?", caption = "@ryanburge\nData: Cooperative Election Study, 2008+2022")
save("yr_compare_relig.png")
reg <- cces22 %>%
filter(faminc_new <= 16) %>%
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 == 9 | religpew == 10 ~ "Atheist/Agnostic")) %>%
mutate(male = car::recode(gender4, "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 = 2022 - birthyr) %>%
mutate(kids = case_when(child18 == 1 ~ 1,
child18 == 2 ~ 0)) %>%
filter(age >= 30 & age <= 45) %>%
select(trad, trad2, male, white, age, income, pid2, educ, kids)
regg <- glm(kids ~ income*trad2*pid2 + male + white + educ, family = "binomial", data = reg)
int <- interact_plot(regg, pred= income, modx = trad2, mod2 = pid2, int.width = .76, interval = TRUE, mod2.labels = c("Democrat", "Republican"))
int +
theme_rb(legend = TRUE) +
y_pct() +
theme(strip.text = element_text(size = 16)) +
scale_x_continuous(breaks = c(4, 8, 12, 16), labels = c("$30K-$40K", "$70K-$80K", "$150K-$200K", "$500K+")) +
labs(x = "Household Income", y = "", title = "Regression Predicting Likelihood of Being a Parent",
subtitle = "Controls for Gender, Race, and Education - Sample Restricted to Those 30-45 Years Old",
caption = "@ryanburge\nData: Cooperative Election Study 2022")
save("regress_parent_ces22.png")
ggg <- cces %>%
filter(age >= 30 & age <= 45) %>%
filter(year >= 2020) %>%
mutate(relig = frcode(religion == 1 ~ "Protestant",
religion == 2 ~ "Catholic",
religion >= 3 & religion <= 8 ~ "Other\nWorld Religions",
religion == 9 | religion == 10 ~ "Atheist/Agnostic",
religion == 11 ~ "Nothing\nin Particular")) %>%
mutate(pid2 = frcode(pid3 == 1 ~ "Democrat",
pid3 == 2 ~ "Republican")) %>%
mutate(numkids = replace_na(numkids, 0)) %>%
group_by(relig, pid2) %>%
mean_ci(numkids, wt = weight, ci = .84) %>% filter(pid2 != "NA") %>% filter(relig != "NA")
ggg %>%
mutate(lab = round(mean, 2)) %>%
ggplot(., aes(x = relig, y = mean, fill = pid2)) +
geom_col(color = "black", position = "dodge") +
scale_fill_manual(values = c("dodgerblue3", "firebrick3")) +
theme_rb(legend = TRUE) +
error_bar() +
geom_text(aes(y = .15, label = lab), position = position_dodge(width = .9), size = 6, family = "font") +
theme(plot.title = element_text(size = 15)) +
labs(x = "Religious Tradition", y = "", title = "Number of Children Per Adult by Partisanship and Religion",
subtitle = "Among those 30-45 Years Old - Having No Children Was Counted as Zero",
caption = "@ryanburge\nData: Cooperative Election Study, 2020-2022")
save("pid2_rel_numkids_ces.png", wd = 6)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment