Skip to content

Instantly share code, notes, and snippets.

@ryanburge
Created May 26, 2024 19:55
Show Gist options
  • Save ryanburge/bc654ff72524cb988ce642cd186a623e to your computer and use it in GitHub Desktop.
Save ryanburge/bc654ff72524cb988ce642cd186a623e to your computer and use it in GitHub Desktop.
cces_educ <- function(df, var){
df %>%
mutate(educ = frcode({{var}} == 1 | {{var}} == 2 ~ "HS or Less",
{{var}} == 3 | {{var}} == 4 ~ "Some\nColl.",
{{var}} == 5 | {{var}} == 6 ~ "4 Yr."))
}
graph <- cces %>%
mutate(gender = frcode(gender == 1 ~ "Men",
gender == 2 ~ "Women")) %>%
filter(year >= 2008) %>%
cces_educ(educ) %>%
mutate(never = case_when(pew_attendance == 1 | pew_attendance == 2 ~ 1,
TRUE ~ 0)) %>%
group_by(educ, year, gender) %>%
mean_ci(never, wt = weight, ci = .84) %>% filter(gender != "NA")
graph %>%
filter(year == 2008 | year == 2012 | year == 2016 | year == 2020 | year == 2022 | year == 2023) %>%
mutate(lab = round(mean, 2)) %>%
ggplot(., aes(x = educ, y = mean, group = gender, fill = gender)) +
geom_col(color = "black", position = "dodge") +
facet_wrap(~ year) +
theme_rb(legend = TRUE) +
# geom_smooth(se = FALSE, method = lm, color = "black", linetype = "twodash", linewidth = .5) +
scale_fill_calc() +
lab_bar(top = FALSE, type = lab, pos = .025, sz = 5.5) +
geom_text(aes(y = .025, label = ifelse(gender == "Men", paste0(lab*100, '%'), "")), position = position_dodge(width = .9), size = 5.5, family = "font", color = "white") +
y_pct() +
error_bar() +
theme(strip.text = element_text(size = 20)) +
labs(x = "Highest Level of Education", y = "", title = "Share Who Attend Religious Services At Least Once a Week", caption = "@ryanburge\nData: Cooperative Election Study, 2008-2023")
save("educ_nevers_ces23_new_gen.png", ht = 9)
cces_educ <- function(df, var){
df %>%
mutate(educ = frcode({{var}} == 1 | {{var}} == 2 ~ "HS or Less",
{{var}} == 3 | {{var}} == 4 ~ "Some College",
{{var}} == 5 | {{var}} == 6 ~ "4 Yr."))
}
graph <- cces %>%
mutate(gender = frcode(gender == 1 ~ "Men",
gender == 2 ~ "Women")) %>%
filter(year >= 2008) %>%
cces_educ(educ) %>%
cces_pid3(pid7) %>%
group_by(educ, year, gender) %>%
ct(pid3, wt = weight, show_na = FALSE) %>% filter(gender != "NA")
graph %>%
ggplot(., aes(x = year, y = pct, color = pid3, group = pid3)) +
geom_point(stroke = .75, shape = 21, show.legend = FALSE) +
geom_smooth(se = FALSE) +
facet_grid(gender ~ educ) +
theme_rb(legend = TRUE) +
pid3_color() +
y_pct() +
theme(strip.text = element_text(size = 20)) +
labs(x = "", y = "", title = "Partisanship Composition by Gender and Education, 2008-2023", caption = "@ryanburge\nData: Cooperative Election Study, 2008-2023")
save("pid3_gender_educ.png", ht = 9)
gg <- cces %>%
mutate(gender = frcode(gender == 1 ~ "Men",
gender == 2 ~ "Women")) %>%
filter(year >= 2008) %>%
cces_pid3(pid7) %>%
mutate(rel = frcode(religion == 1 | religion == 2 | religion == 4 ~ "Christian",
religion == 9 | religion == 10 | religion == 11 ~ "None",
TRUE ~ "All Others")) %>%
group_by(year, gender, pid3) %>%
ct(rel, wt = weight, show_na = FALSE) %>% filter(pid3 != "NA") %>% filter(gender != "NA") %>% filter(pid3 != "Independent") %>% filter(rel != "All Others")
gg %>%
filter(year >= 2010) %>%
ggplot(., aes(x = year, y = pct, color = rel, group = rel)) +
geom_point(stroke = .75, shape = 21, show.legend = FALSE) +
geom_smooth(se = FALSE) +
facet_grid(gender ~ pid3) +
theme_rb(legend = TRUE) +
scale_color_npg() +
y_pct() +
theme(strip.text = element_text(size = 20)) +
labs(x = "", y = "", title = "Religious Composition by Gender and Partisanship, 2010-2023", caption = "@ryanburge\nData: Cooperative Election Study, 2010-2023")
save("rel_gender_pid3.png", ht = 8)
gg <- cces %>%
mutate(religpew = religion) %>%
mutate(relig1 = frcode(religpew == 1 ~ "Protestant",
religpew == 2 ~ "Catholic",
religpew >= 3 & religpew <= 8 ~ "Other World Religions",
religpew == 9 | religpew == 10 ~ "Atheist/Agnostic",
religpew == 11 ~ "Nothing in Particular")) %>%
filter(educ == 5 | educ == 6) %>%
mutate(gender = frcode(gender == 1 ~ "Men",
gender == 2 ~ "Women")) %>%
filter(age <= 40) %>%
filter(year == 2008 | year == 2012 | year == 2016 | year == 2020 | year == 2022 | year == 2023) %>%
group_by(year, gender) %>%
ct(relig1, wt = weight, show_na = FALSE) %>%
mutate(year = as.factor(year)) %>% filter(gender != "NA")
gg %>%
mutate(lab = round(pct, 2)) %>%
ggplot(., aes(x = 1, y = pct, fill = fct_rev(relig1))) +
geom_col(color = "black") +
coord_flip() +
facet_grid(year ~ gender, switch = "y") +
theme_rb(legend = TRUE) +
theme(legend.position = "bottom") +
scale_y_continuous(labels = percent) +
scale_fill_manual(values = c(moma.colors("ustwo", 5))) +
theme(strip.text.y.left = element_text(angle = 0)) +
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 >.08, paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 5.5, family = "font", color = "black") +
geom_text(aes(label = ifelse(relig1 == "Protestant", paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 5.5, family = "font", color = "white") +
labs(x = "", y = "", title = "Religious Compositon of 18-40 Year Old College Graduates", caption = "@ryanburge\nData: Cooperative Election Study, 2008-2023")
save("rel_comp_gender_educ23.png", wd = 10, ht = 4.5)
regg <- cces %>%
filter(year >= 2020) %>%
mutate(att = case_when(pew_attendance == 1 | pew_attendance == 2 ~ 1, TRUE ~ 0)) %>%
mutate(nokids = case_when(havekids == 2 ~ 1,
havekids == 1 ~ 0)) %>%
mutate(single = case_when(marital_status == 5 ~ 1,
TRUE ~ 0)) %>%
mutate(gender = frcode(gender == 1 ~ "Men",
gender == 2 ~ "Women")) %>%
cces_educ(educ) %>%
mutate(white = case_when(race == 1 ~ 1,
TRUE ~ 0)) %>%
mutate(cons = case_when(ideo5 == 4 | ideo5 == 5 ~ 1,
ideo5 <= 3 ~ 0)) %>%
select(age, income, educ, gender, white, cons, single, nokids, att)
reg1 <- glm(att ~ educ*gender + age + income + white + single + nokids, data= regg, family = "binomial")
library(interactions)
graph <- cat_plot(reg1, pred = educ, modx = gender, interval = TRUE, int.width = .76, errorbar.width = .1, geom = "bar")
graph +
theme_rb() +
y_pct() +
theme(legend.position = "bottom") +
theme(plot.title = element_text(size = 15)) +
theme(plot.subtitle = element_text(size = 12)) +
labs(x = "", y = "", title = "Precited Likelihood of Weekly Religious Attendance",
subtitle = "Controls for age, income, race, marital status, parental status",
caption = "@ryanburge\nData: CCES 2020-2023")
save("cat_plot_gender_wk_att_no_cons.png", wd = 6, ht = 6)
regg <- cces %>%
filter(year >= 2020) %>%
mutate(att = case_when(pew_attendance == 1 | pew_attendance == 2 ~ 1, TRUE ~ 0)) %>%
mutate(nokids = case_when(havekids == 2 ~ 1,
havekids == 1 ~ 0)) %>%
mutate(single = case_when(marital_status == 5 ~ 1,
TRUE ~ 0)) %>%
mutate(gender = frcode(gender == 1 ~ "Men",
gender == 2 ~ "Women")) %>%
cces_educ(educ) %>%
mutate(white = case_when(race == 1 ~ 1,
TRUE ~ 0)) %>%
mutate(cons = case_when(ideo5 == 4 | ideo5 == 5 ~ 1,
ideo5 <= 3 ~ 0)) %>%
select(age, income, educ, gender, white, cons, single, nokids, att)
reg1 <- glm(att ~ educ*gender + age + income + white + single + nokids + cons, data= regg, family = "binomial")
library(interactions)
graph <- cat_plot(reg1, pred = educ, modx = gender, interval = TRUE, int.width = .76, errorbar.width = .1, geom = "bar")
graph +
theme_rb() +
y_pct() +
theme(legend.position = "bottom") +
theme(plot.title = element_text(size = 15)) +
theme(plot.subtitle = element_text(size = 12)) +
labs(x = "", y = "", title = "Precited Likelihood of Weekly Religious Attendance",
subtitle = "Controls for age, income, race, marital status, parental status, political ideology",
caption = "@ryanburge\nData: CCES 2020-2023")
save("cat_plot_gender_wk_attn.png", wd = 6, ht = 6)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment