Skip to content

Instantly share code, notes, and snippets.

@ryanburge
Created February 11, 2025 15:30
Show Gist options
  • Select an option

  • Save ryanburge/f735c3a230452a6b84697e6ede942893 to your computer and use it in GitHub Desktop.

Select an option

Save ryanburge/f735c3a230452a6b84697e6ede942893 to your computer and use it in GitHub Desktop.
library(rio)
cces <- import("E://data/jhu22.sav")
## How important are each of the following to your identity and how you think of yourself?
gg1 <- cces %>%
mutate(rel = JHU301) %>%
mutate(rel = frcode(rel == 1 ~ "Very Much",
rel == 2 ~ "Somewhat",
rel == 3 ~ "A little",
rel == 4 ~ "Not at all")) %>%
ct(rel, wt = teamweight, show_na = FALSE) %>%
mutate(type = "Your Race")
gg2 <- cces %>%
mutate(rel = JHU302) %>%
mutate(rel = frcode(rel == 1 ~ "Very Much",
rel == 2 ~ "Somewhat",
rel == 3 ~ "A little",
rel == 4 ~ "Not at all")) %>%
ct(rel, wt = teamweight, show_na = FALSE) %>%
mutate(type = "Your Gender")
gg3 <- cces %>%
mutate(rel = JHU303) %>%
mutate(rel = frcode(rel == 1 ~ "Very Much",
rel == 2 ~ "Somewhat",
rel == 3 ~ "A little",
rel == 4 ~ "Not at all")) %>%
ct(rel, wt = teamweight, show_na = FALSE) %>%
mutate(type = "Your Views about Religion")
both <- bind_rows(gg1, gg2, gg3)
both %>%
mutate(lab = round(pct, 2)) %>%
ggplot(., aes(x = 1, y = pct, fill = rel)) +
geom_col(color = "black") +
coord_flip() +
facet_wrap(~ type, ncol =1, strip.position = "left") +
theme_rb() +
scale_fill_brewer(palette = "Set2") +
theme(legend.position = "bottom") +
scale_y_continuous(labels = percent) +
theme(strip.text.y.left = element_text(angle=0)) +
guides(fill = guide_legend(reverse=TRUE, 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 = 8, family = "font", color = "black") +
# geom_text(aes(label = ifelse(age2 == "18-35", paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 4, family = "font", color = "white") +
# geom_text(aes(label = ifelse(age2 == "36-44", paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 4, family = "font", color = "white") +
theme(plot.title = element_text(size = 19, hjust = 1)) +
theme(strip.text.y.left = element_text(angle = 0, hjust = 1)) +
labs(x = "", y = "", title = "How important are each of the following to your identity and how you think of yourself?",
caption = "@ryanburge\nData: Cooperative Election Study - Johns Hopkins Module, 2022")
save("how_imp_religion_jhu.png", wd = 9, ht = 3)
gg1 <- cces %>%
mutate(rel = JHU301) %>%
mutate(rel = frcode(rel == 1 ~ "Very Much",
rel == 2 ~ "Somewhat",
rel == 3 ~ "A little",
rel == 4 ~ "Not at all")) %>%
cces_pid3(pid7) %>%
group_by(pid3) %>%
ct(rel, wt = teamweight, show_na = FALSE) %>%
mutate(type = "Your Race")
gg2 <- cces %>%
mutate(rel = JHU302) %>%
mutate(rel = frcode(rel == 1 ~ "Very Much",
rel == 2 ~ "Somewhat",
rel == 3 ~ "A little",
rel == 4 ~ "Not at all")) %>%
cces_pid3(pid7) %>%
group_by(pid3) %>%
ct(rel, wt = teamweight, show_na = FALSE) %>%
mutate(type = "Your Gender")
gg3 <- cces %>%
mutate(rel = JHU303) %>%
mutate(rel = frcode(rel == 1 ~ "Very Much",
rel == 2 ~ "Somewhat",
rel == 3 ~ "A little",
rel == 4 ~ "Not at all")) %>%
cces_pid3(pid7) %>%
group_by(pid3) %>%
ct(rel, wt = teamweight, show_na = FALSE) %>%
mutate(type = "Your Views about Religion")
both <- bind_rows(gg1, gg2, gg3)
both %>%
filter(pid3 != "NA") %>%
mutate(lab = round(pct, 2)) %>%
ggplot(., aes(x = fct_rev(pid3), y = pct, fill = rel)) +
geom_col(color = "black") +
coord_flip() +
facet_wrap(~ type, ncol =1, strip.position = "top") +
theme_rb() +
scale_fill_brewer(palette = "Set2") +
theme(legend.position = "bottom") +
scale_y_continuous(labels = percent) +
theme(strip.text.y.left = element_text(angle=0)) +
guides(fill = guide_legend(reverse=TRUE, 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 = 8, family = "font", color = "black") +
# geom_text(aes(label = ifelse(age2 == "18-35", paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 4, family = "font", color = "white") +
# geom_text(aes(label = ifelse(age2 == "36-44", paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 4, family = "font", color = "white") +
theme(plot.title = element_text(size = 19, hjust = 1)) +
theme(strip.text.y.left = element_text(angle = 0, hjust = 1)) +
labs(x = "", y = "", title = "How important are each of the following to your identity and how you think of yourself?",
caption = "@ryanburge\nData: Cooperative Election Study - Johns Hopkins Module, 2022")
save("how_imp_religion_jhu_pid3.png", wd = 9, ht = 7)
gg1 <- cces %>%
mutate(rel = JHU301) %>%
mutate(rel = frcode(rel == 1 ~ "Very Much",
rel == 2 ~ "Somewhat",
rel == 3 ~ "A little",
rel == 4 ~ "Not at all")) %>%
mutate(gen = frcode(birthyr>= 1946 & birthyr <= 1964 ~ 'Boomers',
birthyr>= 1965 & birthyr <= 1979 ~ 'Gen X',
birthyr>= 1980 & birthyr <= 1995 ~ 'Millennials',
birthyr>= 1996 & birthyr <= 2019 ~ 'Gen Z')) %>%
group_by(gen) %>%
ct(rel, wt = teamweight, show_na = FALSE) %>%
mutate(type = "Your Race")
gg2 <- cces %>%
mutate(rel = JHU302) %>%
mutate(rel = frcode(rel == 1 ~ "Very Much",
rel == 2 ~ "Somewhat",
rel == 3 ~ "A little",
rel == 4 ~ "Not at all")) %>%
mutate(gen = frcode(birthyr>= 1946 & birthyr <= 1964 ~ 'Boomers',
birthyr>= 1965 & birthyr <= 1979 ~ 'Gen X',
birthyr>= 1980 & birthyr <= 1995 ~ 'Millennials',
birthyr>= 1996 & birthyr <= 2019 ~ 'Gen Z')) %>%
group_by(gen) %>%
ct(rel, wt = teamweight, show_na = FALSE) %>%
mutate(type = "Your Gender")
gg3 <- cces %>%
mutate(rel = JHU303) %>%
mutate(rel = frcode(rel == 1 ~ "Very Much",
rel == 2 ~ "Somewhat",
rel == 3 ~ "A little",
rel == 4 ~ "Not at all")) %>%
mutate(gen = frcode(birthyr>= 1946 & birthyr <= 1964 ~ 'Boomers',
birthyr>= 1965 & birthyr <= 1979 ~ 'Gen X',
birthyr>= 1980 & birthyr <= 1995 ~ 'Millennials',
birthyr>= 1996 & birthyr <= 2019 ~ 'Gen Z')) %>%
group_by(gen) %>%
ct(rel, wt = teamweight, show_na = FALSE) %>%
mutate(type = "Your Views about Religion")
both <- bind_rows(gg1, gg2, gg3) %>% na.omit()
both %>%
mutate(lab = round(pct, 2)) %>%
ggplot(., aes(x = fct_rev(gen), y = pct, fill = rel)) +
geom_col(color = "black") +
coord_flip() +
facet_wrap(~ type, ncol =1, strip.position = "top") +
theme_rb() +
scale_fill_brewer(palette = "Set2") +
theme(legend.position = "bottom") +
scale_y_continuous(labels = percent) +
theme(strip.text.y.left = element_text(angle=0)) +
guides(fill = guide_legend(reverse=TRUE, 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 = 8, family = "font", color = "black") +
# geom_text(aes(label = ifelse(age2 == "18-35", paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 4, family = "font", color = "white") +
# geom_text(aes(label = ifelse(age2 == "36-44", paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 4, family = "font", color = "white") +
theme(plot.title = element_text(size = 19, hjust = 1)) +
theme(strip.text.y.left = element_text(angle = 0, hjust = 1)) +
labs(x = "", y = "", title = "How important are each of the following to your identity and how you think of yourself?",
caption = "@ryanburge\nData: Cooperative Election Study - Johns Hopkins Module, 2022")
save("how_imp_religion_jhu_gens.png", wd = 9, ht = 7)
gg1 <- cces %>%
mutate(relig1 = frcode(religpew == 1 & pew_bornagain == 1 ~ "Evangelical",
religpew == 1 & pew_bornagain == 2 ~ "Non-Evangelical",
religpew == 2 ~ "Catholic",
religpew >= 3 & religpew <= 8 ~ "Other World Religions",
religpew == 9 | religpew == 10 ~ "Atheist/Agnostic",
religpew == 11 ~ "Nothing in Particular")) %>%
mutate(rel = JHU303) %>%
mutate(rel = frcode(rel == 1 ~ "Very Much",
rel == 2 ~ "Somewhat",
rel == 3 ~ "A little",
rel == 4 ~ "Not at all")) %>%
group_by(relig1) %>%
ct(rel, wt = teamweight, show_na = FALSE)
gg1 %>%
filter(relig1 != "NA") %>%
mutate(lab = round(pct, 2)) %>%
ggplot(., aes(x = 1, y = pct, fill = rel)) +
geom_col(color = "black") +
coord_flip() +
facet_wrap(~ relig1, ncol =1, strip.position = "left") +
theme_rb() +
scale_fill_brewer(palette = "Set2") +
theme(legend.position = "bottom") +
scale_y_continuous(labels = percent) +
theme(strip.text.y.left = element_text(angle=0)) +
guides(fill = guide_legend(reverse=TRUE, 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 = 8, family = "font", color = "black") +
# geom_text(aes(label = ifelse(age2 == "18-35", paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 4, family = "font", color = "white") +
# geom_text(aes(label = ifelse(age2 == "36-44", paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 4, family = "font", color = "white") +
theme(plot.title = element_text(size = 18, hjust = 1)) +
theme(plot.subtitle = element_text(size = 20, hjust = -.425)) +
theme(strip.text.y.left = element_text(angle = 0, hjust = 1)) +
labs(x = "", y = "", title = "How important are your views about religion to your identity and how you think of yourself?",
caption = "@ryanburge\nData: Cooperative Election Study - Johns Hopkins Module, 2022")
save("how_imp_religion_jhu_relig.png", wd = 9, ht = 5)
gg1 <- cces %>%
mutate(rel = JHU301) %>%
mutate(rel = frcode(rel == 1 ~ "Very Much",
rel == 2 ~ "Somewhat",
rel == 3 ~ "A little",
rel == 4 ~ "Not at all")) %>%
ct(rel, wt = teamweight, show_na = FALSE) %>%
mutate(type = "Your Race")
gg2 <- cces %>%
mutate(rel = JHU302) %>%
mutate(rel = frcode(rel == 1 ~ "Very Much",
rel == 2 ~ "Somewhat",
rel == 3 ~ "A little",
rel == 4 ~ "Not at all")) %>%
ct(rel, wt = teamweight, show_na = FALSE) %>%
mutate(type = "Your Gender")
gg3 <- cces %>%
mutate(rel = JHU303) %>%
mutate(rel = frcode(rel == 1 ~ "Very Much",
rel == 2 ~ "Somewhat",
rel == 3 ~ "A little",
rel == 4 ~ "Not at all")) %>%
ct(rel, wt = teamweight, show_na = FALSE) %>%
mutate(type = "Your Views about Religion")
clean <- cces %>%
mutate(race = JHU301) %>%
mutate(gender = JHU302) %>%
mutate(religion = JHU303) %>%
select(caseid, race, gender, religion, teamweight)
cces_clean <- clean %>%
mutate(
race = frcode(race == 1 ~ "Very Much",
race == 2 ~ "Somewhat",
race == 3 ~ "A little",
race == 4 ~ "Not at all"),
gender = frcode(gender == 1 ~ "Very Much",
gender == 2 ~ "Somewhat",
gender == 3 ~ "A little",
gender == 4 ~ "Not at all"),
religion = frcode(religion == 1 ~ "Very Much",
religion == 2 ~ "Somewhat",
religion == 3 ~ "A little",
religion == 4 ~ "Not at all")
)
cces_scored <- cces_clean %>%
mutate(
race_score = case_when(race == "Very Much" ~ 4,
race == "Somewhat" ~ 3,
race == "A little" ~ 2,
race == "Not at all" ~ 1),
gender_score = case_when(gender == "Very Much" ~ 4,
gender == "Somewhat" ~ 3,
gender == "A little" ~ 2,
gender == "Not at all" ~ 1),
religion_score = case_when(religion == "Very Much" ~ 4,
religion == "Somewhat" ~ 3,
religion == "A little" ~ 2,
religion == "Not at all" ~ 1)
)
cces_scored <- cces_scored %>%
mutate(
max_score = pmax(race_score, gender_score, religion_score), # Find highest score in each row
most_important = case_when(
race_score == max_score & gender_score == max_score & religion_score == max_score ~ "All Tied",
race_score == max_score & gender_score == max_score ~ "Race + Gender",
race_score == max_score & religion_score == max_score ~ "Race + Religion",
gender_score == max_score & religion_score == max_score ~ "Gender + Religion",
race_score == max_score ~ "Race",
gender_score == max_score ~ "Gender",
religion_score == max_score ~ "Religion",
TRUE ~ NA_character_ # Should never happen, but a fallback just in case
)
)
small <- cces_scored %>%
select(caseid, most_important)
cces <- left_join(cces, small)
gg1 <- cces %>%
ct(most_important, wt = teamweight, show_na = FALSE) %>%
mutate(pid3 = "All")
cces_pid3 <- function(df, var){
df %>%
mutate(pid3 = frcode({{var}} == 1 | {{var}} == 2 | {{var}} == 3 ~ "Dem.",
{{var}} == 4 ~ "Ind.",
{{var}} == 5 | {{var}} == 6 | {{var}} == 7 ~ "Rep."))
}
gg2 <- cces %>%
cces_pid3(pid7) %>%
group_by(pid3) %>%
ct(most_important, wt = teamweight, show_na = FALSE)
both <- bind_rows(gg1, gg2) %>% na.omit()
both <- both %>%
mutate(pid3 = factor(pid3, levels = c("All", "Dem.", "Ind.", "Rep.")))
both <- both %>%
mutate(most_important = factor(most_important, levels = c(
"All Tied",
"Gender",
"Race",
"Religion",
"Race + Gender",
"Race + Religion",
"Gender + Religion"
)))
both %>%
ggplot(aes(x = pid3, y = pct, fill = pid3)) +
geom_col(color = "black") +
facet_wrap(~ most_important, nrow = 2) +
theme_rb() +
lab_bar(type = pct, above = TRUE, pos = .02, sz = 6) +
scale_y_continuous(labels = percent) +
scale_fill_manual(values = c("All" = "azure2", # Neutral dark gray
"Dem." = "#2b6cb0", # Democratic blue
"Ind." = "#6b46c1", # Purple for independents
"Rep." = "#c53030")) + # Republican red
labs(x = "", y = "", title = "Which Identify is More Important?",
caption = "@ryanburge\nData: Cooperative Election Study - Johns Hopkins Module, 2022")
save("most_important_pid3_jhu.png")
regg <- cces %>%
mutate(rel = case_when(most_important == "Religion" ~ 1,
TRUE ~ 0)) %>%
mutate(att = 7 - pew_churatd) %>%
mutate(cons = case_when(ideo5 == 4 | ideo5 == 5 ~ 1,
ideo5 <= 3 ~ 0)) %>%
mutate(white = case_when(race == 1 ~ 1,
TRUE ~ 0)) %>%
mutate(male = case_when(gender4 == 1 ~ 1,
gender4 == 2 ~ 0)) %>%
mutate(age = 2022 - birthyr) %>%
select(rel, att, cons, white, male, educ, income = faminc_new, age) %>%
na.omit() %>%
as_tibble() %>%
filter(income <= 16) %>%
mutate(across(everything(), ~ (. - min(.)) / (max(.) - min(.))))
out <- glm(rel ~ ., data = regg, family = "binomial")
library(jtools)
summary(out)
coef_names <- c("Education" = "educ",
"Age" = "age",
"Male" = "male",
"Income" = "income",
"White" = "white",
"Religious Attendance" = "att",
"Politically Conservative" = "cons")
gg <- plot_summs(out, scale = TRUE, robust = "HC3", coefs = coef_names)
gg +
theme_rb() +
scale_color_manual(values = c("firebrick4")) +
labs(x = "Odds Ratio", y = "", title = "What Factors Lead to Saying Religion is the Most Important",
caption = "@ryanburge\nData: Cooperative Election Study - Johns Hopkins Module, 2022")
save("religion_most_important.png", ht = 3.5)
gg2 <- cces %>%
mutate(gender = frcode(gender4 == 1 ~ "Men",
gender4 == 2 ~ "Women")) %>%
mutate(rel = JHU302) %>%
mutate(rel = frcode(rel == 1 ~ "Very Much",
rel == 2 ~ "Somewhat",
rel == 3 ~ "A little",
rel == 4 ~ "Not at all")) %>%
group_by(gender) %>%
ct(rel, wt = teamweight, show_na = FALSE) %>%
mutate(type = "Your Gender")
gg1 <- cces %>%
mutate(race = frcode(race == 1 ~ "White",
TRUE ~ "Non-White")) %>%
mutate(rel = JHU301) %>%
mutate(rel = frcode(rel == 1 ~ "Very Much",
rel == 2 ~ "Somewhat",
rel == 3 ~ "A little",
rel == 4 ~ "Not at all")) %>%
mutate(gen = frcode(birthyr>= 1946 & birthyr <= 1964 ~ 'Boomers',
birthyr>= 1965 & birthyr <= 1979 ~ 'Gen X',
birthyr>= 1980 & birthyr <= 1995 ~ 'Millennials',
birthyr>= 1996 & birthyr <= 2019 ~ 'Gen Z')) %>%
group_by(race) %>%
ct(rel, wt = teamweight, show_na = FALSE) %>%
mutate(type = "Your Race")
gg3 <- cces %>%
mutate(rell = frcode(religpew <= 8 ~ "Religious",
religpew == 9 | religpew == 10 | religpew == 11 ~ "Non-Religious")) %>%
mutate(rel = JHU303) %>%
mutate(rel = frcode(rel == 1 ~ "Very Much",
rel == 2 ~ "Somewhat",
rel == 3 ~ "A little",
rel == 4 ~ "Not at all")) %>%
group_by(rell) %>%
ct(rel, wt = teamweight, show_na = FALSE) %>%
mutate(type = "Your Views about Religion")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment