Skip to content

Instantly share code, notes, and snippets.

@ryanburge
Created April 10, 2025 14:54
Show Gist options
  • Save ryanburge/5f5d318fa1e570541361ab7cf5d02ee5 to your computer and use it in GitHub Desktop.
Save ryanburge/5f5d318fa1e570541361ab7cf5d02ee5 to your computer and use it in GitHub Desktop.
ggg1 <- cces08 %>%
filter(V219 == 3) %>%
mutate(vote = CC410) %>%
mutate(vote = frcode(vote == 1 ~ "Republican",
vote == 2 ~ "Democrat",
vote == 3 | vote == 4 | vote == 5 | vote == 6 ~ "Third Party")) %>%
ct(vote, wt = V201, show_na = FALSE) %>%
mutate(year = 2008)
ggg2 <- cces12 %>%
filter(religpew == 3) %>%
mutate(vote = CC410a) %>%
mutate(vote = frcode(vote == 1 ~ "Democrat",
vote == 2 ~ "Republican",
vote == 4 ~ "Third Party")) %>%
ct(vote, wt = weight_vv_post, show_na = FALSE) %>%
mutate(year = 2012)
ggg3 <- cces16 %>%
filter(CL_matched == "Y") %>%
filter(religpew == 3) %>%
mutate(vote = CC16_410a) %>%
mutate(vote = frcode(vote == 1 ~ "Republican",
vote == 2 ~ "Democrat",
vote == 3 | vote == 4 | vote == 5 | vote == 8 ~ "Third Party")) %>%
ct(vote, wt = commonweight_vv_post, show_na = FALSE) %>%
mutate(year = 2016)
ggg4 <- cces20 %>%
filter(CL_voter_status <= 5) %>%
filter(religpew == 3) %>%
mutate(vote = CC20_410) %>%
mutate(vote = frcode(vote == 1 ~ "Democrat",
vote == 2 ~ "Republican",
vote == 4 ~ "Third Party")) %>%
ct(vote, wt = vvweight_post, show_na = FALSE) %>%
mutate(year = 2020)
ggg5 <- cces24 %>%
filter(religpew == 3) %>%
mutate(vote = CC24_410) %>%
mutate(vote = frcode(vote == 1 ~ "Democrat",
vote == 2 ~ "Republican",
vote >= 3 & vote <= 9 ~ "Third Party")) %>%
ct(vote, wt = commonpostweight, show_na = FALSE) %>%
mutate(year = 2024)
all <- bind_rows(ggg1, ggg2, ggg3, ggg4, ggg5) %>% mutate(year = as.factor(year))
all %>%
mutate(lab = round(pct, 2)) %>%
ggplot(., aes(x = fct_rev(year), y = pct, fill = fct_rev(vote))) +
geom_col(color = "black") +
coord_flip() +
theme_rb() +
scale_fill_manual(
values = c(
"Republican" = "#D73027", # Strong red
"Democrat" = "#4575B4", # Strong blue
"Third Party" = "#F0E442" # Bright gold/yellow
)
) +
theme(legend.position = "bottom") +
scale_y_continuous(labels = percent) +
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 >.05, paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 8, family = "font", color = "white") +
geom_text(aes(label = ifelse(pct >.05 & vote == "Third Party", paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 8, family = "font", color = "black") +
# 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 = 16)) +
theme(strip.text.y.left = element_text(angle = 0, hjust = 1)) +
labs(x = "", y = "", title = "Presidential Vote Choice Among Latter-day Saints, 2008-2024", caption = "@ryanburge\nData: Cooperative Election Study, 2008-2024")
save("pres_vote_lds_24.png", wd = 7, ht = 4)
gg1 <- cces %>%
cces_pid3(pid7) %>%
filter(religion == 3) %>%
group_by(year) %>%
ct(pid3, wt = weight, show_na = FALSE) %>%
mutate(year = as.factor(year))
gg1 %>%
filter(year == 2008 | year == 2012 | year == 2016 | year == 2020 | year == 2024) %>%
mutate(lab = round(pct, 2)) %>%
ggplot(., aes(x = fct_rev(year), y = pct, fill = fct_rev(pid3))) +
geom_col(color = "black") +
coord_flip() +
theme_rb() +
scale_fill_manual(
values = c(
"Republican" = "#D73027", # Strong red
"Democrat" = "#4575B4", # Strong blue
"Independent" = "darkorchid" # Bright gold/yellow
)
) +
theme(legend.position = "bottom") +
scale_y_continuous(labels = percent) +
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 >.05, paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 8, family = "font", color = "white") +
# geom_text(aes(label = ifelse(pct >.05 & vote == "Third Party", paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 8, family = "font", color = "black") +
# 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 = 16)) +
theme(strip.text.y.left = element_text(angle = 0, hjust = 1)) +
labs(x = "", y = "", title = "The Partisanship of Latter-day Saints, 2008-2024", caption = "@ryanburge\nData: Cooperative Election Study, 2008-2024")
save("pid3_lds_24.png", wd = 7, ht = 4)
aaa1 <- cces %>%
filter(year == 2016) %>%
filter(religion == 3) %>%
cces_pid3(pid7) %>%
mutate(id3 = frcode(ideo5 == 1 | ideo5 == 2 ~ "Liberal",
ideo5 == 3 ~ "Moderate",
ideo5 == 4 | ideo5 == 5 ~ "Conserative")) %>%
filter(pid3 != "NA") %>%
group_by(pid3, year) %>%
ct(id3, wt = weight, show_na = FALSE) %>%
ungroup() %>%
mutate(pct = n/862) %>%
mutate(year = 2016)
aaa2 <- cces %>%
filter(year == 2020) %>%
filter(religion == 3) %>%
cces_pid3(pid7) %>%
mutate(id3 = frcode(ideo5 == 1 | ideo5 == 2 ~ "Liberal",
ideo5 == 3 ~ "Moderate",
ideo5 == 4 | ideo5 == 5 ~ "Conserative")) %>%
filter(pid3 != "NA") %>%
group_by(pid3, year) %>%
ct(id3, wt = weight, show_na = FALSE) %>%
mutate(pct = n/716) %>%
mutate(year = 2020)
aaa3 <- cces %>%
filter(year == 2024) %>%
filter(religion == 3) %>%
cces_pid3(pid7) %>%
mutate(id3 = frcode(ideo5 == 1 | ideo5 == 2 ~ "Liberal",
ideo5 == 3 ~ "Moderate",
ideo5 == 4 | ideo5 == 5 ~ "Conserative")) %>%
filter(pid3 != "NA") %>%
group_by(pid3, year) %>%
ct(id3, wt = weight, show_na = FALSE) %>%
mutate(pct = n/623) %>%
mutate(year = 2024)
heat <- bind_rows(aaa1, aaa2, aaa3)
heat <- heat %>%
mutate(
bins = case_when(
pct < 0.10 ~ "a", # Low
pct < 0.25 ~ "b", # Medium
TRUE ~ "e" # High
)
)
custom_colors <- c(
"a" = "#f0e5cf", # light tan
"b" = "#c2b280", # muted khaki
"e" = "#7c6f4f" # deep olive brown
)
heat %>%
mutate(lab = round(pct, 2)) %>%
ggplot(aes(x = pid3, y = id3)) +
geom_tile(aes(fill = bins), color = "black") +
scale_fill_manual(values = custom_colors) +
geom_text(
aes(label = paste0(lab * 100, '%'), color = ifelse(bins == "e", "white", "black")),
size = 11, family = "font"
) +
scale_color_identity() +
facet_wrap(~ year) +
theme_rb() +
theme(
plot.subtitle = element_text(size = 14),
strip.text = element_text(size = 20)
) +
labs(
x = "Partisanship",
y = "Ideology",
title = "The Partisanship and Ideology of Latter-day Saints",
caption = "@ryanburge\nData: Cooperative Election Study, 2016–2024"
)
save("heat_lds_id3_pid3.png", wd = 10, ht = 5)
ggg3 <- cces16 %>%
filter(CL_matched == "Y") %>%
mutate(id3 = frcode(ideo5 == 1 | ideo5 == 2 ~ "Liberal",
ideo5 == 3 ~ "Moderate",
ideo5 == 4 | ideo5 == 5 ~ "Conserative")) %>%
filter(religpew == 3) %>%
mutate(vote = CC16_410a) %>%
mutate(vote = frcode(vote == 1 ~ "Republican",
vote == 2 ~ "Democrat",
vote == 3 | vote == 4 | vote == 5 | vote == 8 ~ "Third Party")) %>%
group_by(id3) %>%
ct(vote, wt = commonweight_vv_post, show_na = FALSE) %>%
mutate(year = 2016)
ggg4 <- cces20 %>%
filter(CL_voter_status <= 5) %>%
mutate(id3 = frcode(ideo5 == 1 | ideo5 == 2 ~ "Liberal",
ideo5 == 3 ~ "Moderate",
ideo5 == 4 | ideo5 == 5 ~ "Conserative")) %>%
filter(religpew == 3) %>%
mutate(vote = CC20_410) %>%
mutate(vote = frcode(vote == 1 ~ "Democrat",
vote == 2 ~ "Republican",
vote == 4 ~ "Third Party")) %>%
group_by(id3) %>%
ct(vote, wt = vvweight_post, show_na = FALSE) %>%
mutate(year = 2020)
ggg5 <- cces24 %>%
mutate(id3 = frcode(ideo5 == 1 | ideo5 == 2 ~ "Liberal",
ideo5 == 3 ~ "Moderate",
ideo5 == 4 | ideo5 == 5 ~ "Conserative")) %>%
filter(religpew == 3) %>%
mutate(vote = CC24_410) %>%
mutate(vote = frcode(vote == 1 ~ "Democrat",
vote == 2 ~ "Republican",
vote >= 3 & vote <= 9 ~ "Third Party")) %>%
group_by(id3) %>%
ct(vote, wt = commonpostweight, show_na = FALSE) %>%
mutate(year = 2024)
all <- bind_rows(ggg3, ggg4, ggg5) %>% filter(id3 == "Moderate") %>% mutate(year = as.factor(year))
all %>%
mutate(lab = round(pct, 2)) %>%
ggplot(., aes(x = fct_rev(year), y = pct, fill = fct_rev(vote))) +
geom_col(color = "black") +
coord_flip() +
theme_rb() +
scale_fill_manual(
values = c(
"Republican" = "#D73027", # Strong red
"Democrat" = "#4575B4", # Strong blue
"Third Party" = "#F0E442" # Bright gold/yellow
)
) +
theme(legend.position = "bottom") +
scale_y_continuous(labels = percent) +
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 >.05, paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 8, family = "font", color = "white") +
geom_text(aes(label = ifelse(pct >.05 & vote == "Third Party", paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 8, family = "font", color = "black") +
# 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 = 16)) +
theme(strip.text.y.left = element_text(angle = 0, hjust = 1)) +
labs(x = "", y = "", title = "Presidential Vote Choice Among Ideologically Moderate LDS, 2016-2024", caption = "@ryanburge\nData: Cooperative Election Study, 2016-2024")
save("pres_vote_lds_moderate_24.png", wd = 7, ht = 3)
gg1 <- cces %>%
filter(year >= 2020) %>%
filter(religion == 3) %>%
group_by(age) %>%
cces_pid3(pid7) %>%
ct(pid3, wt = weight, show_na = FALSE)
gg1 %>%
filter(age <= 75) %>%
ggplot(., aes(x = age, y = pct, color = pid3)) +
geom_point(stroke = 1, shape = 21, alpha = .20) +
geom_labelsmooth(aes(label = pid3, linetype = pid3), method = "loess", formula = y ~ x, family = "font", linewidth = 1, text_smoothing = 30, size = 6, linewidth = 1, boxlinewidth = 0.3) +
pid3_color() +
y_pct() +
theme_rb() +
labs(x ="Age", y = "", title = "Partisanship Composition of Latter-day Saints by Age", caption = "@ryanburge\nData: Cooperative Election Study, 2022-2024")
save("ces_lds_age_pid3.png")
library(socsci)
ggg3 <- cces16 %>%
mutate(age = 2016 - birthyr) %>%
mutate(age2 = frcode(age <= 35 ~ "18-35",
age >= 36 & age <= 50 ~ "36-50",
age >= 51 & age <= 64 ~ "51-64",
age >= 65 ~ "65+")) %>%
filter(CL_matched == "Y", religpew == 3) %>%
mutate(vote = case_when(CC16_410a == 1 ~ 1, # Republican
CC16_410a %in% c(2, 3, 4, 5, 8) ~ 0)) %>% # Democrat, Third Party
group_by(age2) %>%
mean_ci(vote, wt = commonweight_vv_post, ci = 0.84) %>%
mutate(year = 2016)
ggg4 <- cces20 %>%
filter(CL_voter_status <= 5, religpew == 3) %>%
mutate(age = 2020 - birthyr) %>%
mutate(age2 = frcode(age <= 35 ~ "18-35",
age >= 36 & age <= 50 ~ "36-50",
age >= 51 & age <= 64 ~ "51-64",
age >= 65 ~ "65+")) %>%
mutate(vote = case_when(CC20_410 == 2 ~ 1, # Republican
CC20_410 %in% c(1, 4) ~ 0)) %>% # Democrat, Third Party
group_by(age2) %>%
mean_ci(vote, ci = 0.84) %>%
mutate(year = 2020)
ggg5 <- cces24 %>%
filter(religpew == 3) %>%
mutate(age = 2024 - birthyr) %>%
mutate(age2 = frcode(age <= 35 ~ "18-35",
age >= 36 & age <= 50 ~ "36-50",
age >= 51 & age <= 64 ~ "51-64",
age >= 65 ~ "65+")) %>%
mutate(vote = case_when(CC24_410 == 2 ~ 1, # Republican
CC24_410 %in% c(1, 3:9) ~ 0)) %>% # Democrat, Third Party
group_by(age2) %>%
mean_ci(vote, wt = commonpostweight, ci = 0.84) %>%
mutate(year = 2024)
all <- bind_rows(ggg3, ggg4, ggg5) %>% mutate(year = as.factor(year))
all %>%
ggplot(., aes(x = year, y = mean, fill = mean)) +
geom_col(color = "black") +
facet_wrap(~ age2) +
scale_fill_gradient2(low = "dodgerblue3", mid = "azure1", high = "firebrick3", midpoint = .65) +
theme_rb() +
error_bar() +
y_pct() +
lab_bar(above = FALSE, pos = .065, sz = 8, type = mean) +
labs(x = "", y = "", title = "Vote Share for Trump among Latter-day Saints", caption = "@ryanburge\nData: Cooperative Election Study, 2016-2024")
save("lds_age2_votes.png", ht = 8, wd = 5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment