Skip to content

Instantly share code, notes, and snippets.

@ryanburge
Created July 30, 2024 03:11
Show Gist options
  • Save ryanburge/8cd9e24033cdb426d77fdb27cdf0b9d1 to your computer and use it in GitHub Desktop.
Save ryanburge/8cd9e24033cdb426d77fdb27cdf0b9d1 to your computer and use it in GitHub Desktop.
gg1 <- cces %>%
filter(year >= 2022) %>%
filter(pid7 == 5 | pid7 == 6 | pid7 == 7) %>%
mutate(relig1 = 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(age2 = frcode(age >= 18 & age <= 35 ~ "18-35",
age >= 36 & age <= 44 ~ "36-44",
age >= 45 & age <= 54 ~ "45-54",
age >= 55 & age <= 64 ~ "55-64",
age >= 65 ~ "65+")) %>%
group_by(age2) %>%
ct(relig1, wt = weight, show_na = FALSE)
gg1 %>%
mutate(lab = round(pct, 2)) %>%
ggplot(., aes(x = 1, y = pct, fill = fct_rev(relig1))) +
geom_col(color = "black") +
coord_flip() +
facet_wrap(~ age2, ncol =1, strip.position = "left") +
theme_rb() +
scale_fill_manual(values = c(moma.colors("OKeeffe", 5))) +
theme(legend.position = "bottom") +
scale_y_continuous(labels = percent) +
theme(strip.text.y.left = element_text(angle=0, size = 20)) +
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 = "black") +
geom_text(aes(label = ifelse(relig1 == "Protestant", paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 8, family = "font", color = "white") +
geom_text(aes(label = ifelse(relig1 == "Catholic", paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 8, family = "font", color = "white") +
theme(plot.title = element_text(size = 20)) +
theme(strip.text.y.left = element_text(angle = 0, hjust = 1)) +
labs(x = "", y = "", title = "The Religious Composition of the Republican Party by Age", caption = "@ryanburge\nData: Cooperative Election Study, 2022-2023")
save("relig_gop_age2.png", wd = 9, ht = 5)
gg1 <- cces %>%
filter(year >= 2022) %>%
filter(pid7 == 5 | pid7 == 6 | pid7 == 7) %>%
mutate(pew_religimp = pew_importance) %>%
mutate(imp = frcode(pew_religimp == 4 ~ "Not at All",
pew_religimp == 3 ~ "Not too",
pew_religimp == 2 ~ "Somewhat",
pew_religimp == 1 ~ "Very")) %>%
group_by(age) %>%
ct(imp, wt = weight, show_na = FALSE)
gg1 %>%
filter(age <= 75) %>%
ggplot(., aes(x = age, y = pct, color = imp, group = imp)) +
geom_point(stroke = .3, shape = 21) +
geom_labelsmooth(aes(label = imp), method = "loess", formula = y ~ x, family = "font", linewidth = 1, text_smoothing = 30, size = 6, linewidth = 1, boxlinewidth = 0.3, hjust = .25) +
scale_color_manual(values = c(met.brewer("Egypt", 4))) +
theme_rb() +
scale_y_continuous(labels = percent, limits = c(0, .65)) +
add_text(x = 18, y = .17, word = "12%", sz = 7) +
add_text(x = 18, y = .10, word = "10%", sz = 7) +
add_text(x = 18, y = .32, word = "32%", sz = 7) +
add_text(x = 18, y = .46, word = "43%", sz = 7) +
add_text(x = 75, y = .03, word = "7%", sz = 7) +
add_text(x = 75, y = .12, word = "11%", sz = 7) +
add_text(x = 75, y = .28, word = "26%", sz = 7) +
add_text(x = 75, y = .61, word = "56%", sz = 7) +
labs(x = "Age", y = "", title = "How Important is Religion to You? - Among Republicans", caption = "@ryanburge\nData: Cooperative Election Study, 2022-2023")
save("relig_imp_gop_age.png")
gg1 <- cces %>%
filter(year == 2008 | year == 2022) %>%
filter(pid7 == 5 | pid7 == 6 | pid7 == 7) %>%
mutate(relig1 = frcode(religion == 1 ~ "Protestant",
religion == 2 ~ "Catholic",
religion >= 3 & religion <= 8 ~ "Other\nWorld\nReligions",
religion == 9 | religion == 10 ~ "Atheist/\nAgnostic",
religion == 11 ~ "Nothing\nin Particular")) %>%
mutate(age2 = frcode(age >= 18 & age <= 35 ~ "18-35",
age >= 36 & age <= 44 ~ "36-44",
age >= 45 & age <= 54 ~ "45-54",
age >= 55 & age <= 64 ~ "55-64",
age >= 65 ~ "65+")) %>%
group_by(age2, year) %>%
ct(relig1, wt = weight, show_na = FALSE)
gg1 %>%
mutate(lab = round(pct, 2)) %>%
filter(age2 == "18-35") %>%
ggplot(., aes(x = relig1, y = pct, fill = factor(year))) +
geom_col(color = "black", position = "dodge") +
theme_rb(legend = TRUE) +
scale_fill_calc() +
y_pct() +
geom_text(aes(y = pct + .02, label = paste0(lab*100, '%')), position = position_dodge(width = .9), size = 6.5, family = "font") +
labs(x = "", y = "", title = "The Religious Composition of Republicans, 18-35 Years Old", caption = "@ryanburge\nData: Cooperative Election Study, 2008-2022")
save("young_gop_relig_comp.png", wd = 6)
gg1 <- cces23 %>%
filter(pid7 == 5 | pid7 == 6 | pid7 == 7) %>%
mutate(age = 2023 - birthyr) %>%
mutate(age2 = frcode(age >= 18 & age <= 35 ~ "18-35",
age >= 36 & age <= 44 ~ "36-44",
age >= 45 & age <= 54 ~ "45-54",
age >= 55 & age <= 64 ~ "55-64",
age >= 65 ~ "65+")) %>%
mutate(rel = frcode(religpew <= 8 ~ "Religious",
religpew == 9 | religpew == 10 | religpew == 11 ~ "Not\nReligious")) %>%
mutate(ab = case_when(CC23_324a == 1 ~ 1,
CC23_324a == 2 ~ 0)) %>%
group_by(rel, age2) %>%
mean_ci(ab, wt = commonweight, ci = .84) %>%
na.omit()
library(ggpubr)
one <- gg1 %>%
filter(rel == "Religious") %>%
select(age2, rel = mean)
two <- gg1 %>%
ungroup() %>%
filter(rel == "Not\nReligious") %>%
select(age2, nrel = mean)
lab <- left_join(one, two) %>%
mutate(diff = rel - nrel) %>%
select(age2, diff) %>%
mutate(diff = round(diff, 2)) %>%
mutate(diff = diff*100) %>%
mutate(diff = abs(diff)) %>%
mutate(diff = paste0(diff, "%"))
ttt <- left_join(gg1, lab)
library(ggpubr)
ttt %>%
mutate(lab = round(mean, 2)) %>%
ggplot(., aes(x = rel, y = mean, fill = rel)) +
geom_col(color = "black") +
facet_wrap(~ age2) +
theme_rb() +
geom_bracket(data = ttt, aes(label = diff), xmin = 1, xmax = 2, y.position = .675, label = "test", family = "font", label.size = 7, size = 1) +
scale_y_continuous(labels = percent, limits = c(0, .75)) +
error_bar() +
scale_fill_manual(values = c("#008080", "#FFA500")) +
lab_bar(top = FALSE, type = lab, pos = .075, sz = 9) +
theme(strip.text = element_text(size = 20)) +
labs(x = "", y = "Share in Favor", title = "Always allow a woman the right to obtain an abortion\nas a matter of choice - Among Republicans", caption = "@ryanburge\nData: Cooperative Election Study, 2023")
save("ab_any_23_gop_age2.png", wd = 6, ht = 8)
gg1 <- cces23 %>%
filter(pid7 == 5 | pid7 == 6 | pid7 == 7) %>%
mutate(age = 2023 - birthyr) %>%
mutate(age2 = frcode(age >= 18 & age <= 35 ~ "18-35",
age >= 36 & age <= 44 ~ "36-44",
age >= 45 & age <= 54 ~ "45-54",
age >= 55 & age <= 64 ~ "55-64",
age >= 65 ~ "65+")) %>%
mutate(rel = frcode(religpew <= 8 ~ "Religious",
religpew == 9 | religpew == 10 | religpew == 11 ~ "Not\nReligious")) %>%
mutate(ab = case_when(CC23_343a == 1 ~ 1,
CC23_343a == 2 ~ 0)) %>%
group_by(rel, age2) %>%
socsci::mean_ci(ab, wt = commonweight, ci = .84) %>%
na.omit()
library(ggpubr)
one <- gg1 %>%
filter(rel == "Religious") %>%
select(age2, rel = mean)
two <- gg1 %>%
ungroup() %>%
filter(rel == "Not\nReligious") %>%
select(age2, nrel = mean)
lab <- left_join(one, two) %>%
mutate(diff = rel - nrel) %>%
select(age2, diff) %>%
mutate(diff = round(diff, 2)) %>%
mutate(diff = diff*100) %>%
mutate(diff = abs(diff)) %>%
mutate(diff = paste0(diff, "%"))
ttt <- left_join(gg1, lab)
ttt %>%
mutate(lab = round(mean, 2)) %>%
ggplot(., aes(x = rel, y = mean, fill = rel)) +
geom_col(color = "black") +
facet_wrap(~ age2) +
theme_rb() +
geom_bracket(data = ttt, aes(label = diff), xmin = 1, xmax = 2, y.position = .99, label = "test", family = "font", label.size = 7, size = 1) +
scale_y_continuous(labels = percent, limits = c(0, 1.05)) +
error_bar() +
scale_fill_manual(values = c("#00BCD4", "#E91E63")) +
lab_bar(top = FALSE, type = lab, pos = .075, sz = 9) +
theme(strip.text = element_text(size = 16)) +
theme(plot.title = element_text(size = 14)) +
labs(x = "", y = "Share in Favor", title = "Make it illegal for health care professionals to provide someone younger\nthan 18 with medical care for a gender transition. - Among Republicans",
caption = "@ryanburge\nData: Cooperative Election Study, 2023")
save("gender_change_23_gop_age2.png", wd = 6, ht = 8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment