Skip to content

Instantly share code, notes, and snippets.

@ryanburge
Created August 3, 2024 19:05
Show Gist options
  • Select an option

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

Select an option

Save ryanburge/19424ce5656e7c692744b26b0f54c4eb to your computer and use it in GitHub Desktop.
library(rio)
library(janitor)
prri <- import("E://data/prri23.sav") %>% clean_names()
aaa1 <- prri %>%
mutate(cn = q42_c) %>%
mutate(cn = frcode(cn == 5 ~ "Have Not Heard of It",
cn == 4 ~ "Very Unfavorable",
cn == 3 ~ "Somewhat Unfavorable",
cn == 2 ~ "Somewhat Favorable",
cn == 1 ~ "Very Favorable")) %>%
ct(cn, wt = weight, show_na = FALSE) %>%
mutate(pid3 = "Entire Sample")
aaa2 <- prri %>%
mutate(pid3 = qpid100) %>%
mutate(pid3 = frcode(pid3 == 2 ~ "Democrat",
pid3 == 3 ~ "Independent",
pid3 == 1 ~ "Republican")) %>%
mutate(cn = q42_c) %>%
mutate(cn = frcode(cn == 5 ~ "Have Not Heard of It",
cn == 4 ~ "Very Unfavorable",
cn == 3 ~ "Somewhat Unfavorable",
cn == 2 ~ "Somewhat Favorable",
cn == 1 ~ "Very Favorable")) %>%
group_by(pid3) %>%
ct(cn, wt = weight, show_na = FALSE) %>% filter(pid3 != "NA")
graph <- bind_rows(aaa1, aaa2)
graph$pid3 <- factor(graph$pid3, levels = c("Entire Sample", "Democrat", "Independent", "Republican"))
graph %>%
mutate(lab = round(pct, 2)) %>%
ggplot(., aes(x = 1, y = pct, fill = fct_rev(cn))) +
geom_col(color = "black") +
coord_flip() +
facet_wrap(~ pid3, ncol =1, strip.position = "left") +
theme_rb() +
scale_fill_manual(values = c( "#033f63", "#28666e", "#7D3C98", "#5f2680", "azure4")) +
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") +
theme(plot.title = element_text(size = 16)) +
theme(strip.text.y.left = element_text(angle = 0, hjust = 1)) +
labs(x = "", y = "", title = "Do you have a favorable or an unfavorable view of Christian Nationalism", caption = "@ryanburge + @religiondata\nData: PRRI/Brookings, 2023")
save("cn_views_prri.png", wd = 9, ht = 4)
gg <- prri %>%
mutate(cn = q42_c) %>%
mutate(cn = case_when(cn == 5 ~ 1,
cn <= 4 ~ 0)) %>%
mutate(id = q21) %>%
mutate(id = frcode(id == 1 ~ "Extremely\nLiberal",
id == 2 ~ "Liberal",
id == 3 ~ "Slightly\nLiberal",
id == 4 ~ "Moderate",
id == 5 ~ "Slightly\nConservative",
id == 6 ~ "Conservative",
id == 7 ~ "Extremely\nConservative")) %>%
group_by(id) %>%
mean_ci(cn, wt = weight, ci = .84) %>%
filter(id != "NA")
color_gradient <- rev(brewer.pal(7, "RdBu"))
gg %>%
mutate(lab = round(mean, 2)) %>%
ggplot(., aes(x = id, y = mean, fill = id)) +
geom_col(color = "black") +
scale_fill_manual(values = color_gradient) +
y_pct() +
theme_rb() +
error_bar() +
lab_bar(top = FALSE, type = lab, pos = .035, sz = 9.5) +
labs(x = "", y = "", title = "Share Saying They Have Never Heard of Christian Nationalism", caption = "@ryanburge + @religiondata\nData: PRRI/Brookings, 2023")
save("cn_know_id7.png", wd = 6.5)
gg <- prri %>%
mutate(cn = q42_c) %>%
mutate(cn = case_when(cn == 5 ~ 1,
cn <= 4 ~ 0)) %>%
mutate(age = ppage) %>%
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(pid3 = qpid100) %>%
mutate(pid3 = frcode(pid3 == 2 ~ "Dem.",
pid3 == 3 ~ "Ind.",
pid3 == 1 ~ "Rep.")) %>%
group_by(age2, pid3) %>%
mean_ci(cn, wt = weight, ci = .84) %>%
filter(age2 != "NA") %>%
filter(pid3 != "NA")
gg %>%
mutate(lab = round(mean, 2)) %>%
ggplot(., aes(x = pid3, y = mean, fill = pid3)) +
geom_col(color = "black") +
facet_wrap(~ age2) +
error_bar() +
pid3_fill() +
theme_rb() +
y_pct() +
lab_bar(top = FALSE, type = lab, pos = .05, sz = 7.5) +
theme(strip.text = element_text(size = 20)) +
labs(x = "", y = "", title = "Share Saying They Have Never Heard of Christian Nationalism", caption = "@ryanburge + @religiondata\nData: PRRI/Brookings, 2023")
save("prri_cn_age2_pid3.png", wd = 6.25)
gg <- prri %>%
mutate(cn = q42_c) %>%
mutate(cn = case_when(cn == 5 ~ 1,
cn <= 4 ~ 0)) %>%
mutate(ed = ppeduc5) %>%
mutate(ed = frcode(ed == 1 | ed == 2 ~ "HS Or Less",
ed == 3 ~ "Some College",
ed == 4 ~ "4 Yr.",
ed == 5 ~ "Grad. Degree")) %>%
mutate(pid3 = qpid100) %>%
mutate(pid3 = frcode(pid3 == 2 ~ "Dem.",
pid3 == 3 ~ "Ind.",
pid3 == 1 ~ "Rep.")) %>%
group_by(ed, pid3) %>%
mean_ci(cn, wt = weight, ci = .84) %>%
filter(ed != "NA") %>%
filter(pid3 != "NA")
gg %>%
mutate(lab = round(mean, 2)) %>%
ggplot(., aes(x = pid3, y = mean, fill = pid3)) +
geom_col(color = "black") +
facet_wrap(~ ed) +
error_bar() +
pid3_fill() +
theme_rb() +
y_pct() +
lab_bar(top = FALSE, type = lab, pos = .05, sz = 9) +
theme(strip.text = element_text(size = 20)) +
labs(x = "", y = "", title = "Share Saying They Have Never Heard of Christian Nationalism", caption = "@ryanburge + @religiondata\nData: PRRI/Brookings, 2023")
save("prri_cn_ed_pid3.png", wd = 6.2)
one <- prri %>%
mutate(cn = q42_c) %>%
mutate(cn = case_when(cn == 5 ~ 1,
cn <= 4 ~ 0)) %>%
mutate(fav = q42_a) %>%
mutate(fav = frcode(fav == 4 ~ "Very Unfavorable",
fav == 3 ~ "Somewhat Unfavorable",
fav == 2 ~ "Somewhat Favorable",
fav == 1 ~ "Very Favorable")) %>%
group_by(fav) %>%
mean_ci(cn, wt = weight, ci = .84) %>%
mutate(type = "Biden") %>%
filter(fav != "NA")
two <- prri %>%
mutate(cn = q42_c) %>%
mutate(cn = case_when(cn == 5 ~ 1,
cn <= 4 ~ 0)) %>%
mutate(fav = q42_b) %>%
mutate(fav = frcode(fav == 4 ~ "Very Unfavorable",
fav == 3 ~ "Somewhat Unfavorable",
fav == 2 ~ "Somewhat Favorable",
fav == 1 ~ "Very Favorable")) %>%
group_by(fav) %>%
mean_ci(cn, wt = weight, ci = .84) %>%
mutate(type = "Trump") %>%
filter(fav != "NA")
both <- bind_rows(one, two)
both %>%
mutate(lab = round(mean, 2)) %>%
ggplot(., aes(x = type, y = mean, fill = type)) +
geom_col(color = "black") +
facet_wrap(~ fav, nrow = 1) +
scale_fill_manual(values = c("dodgerblue2", "firebrick2")) +
theme_rb() +
error_bar() +
y_pct() +
lab_bar(top = FALSE, type = lab, pos = .035, sz = 10) +
labs(x = "", y = "", title = "Share Saying They Have Never Heard of Christian Nationalism",
subtitle = "Based on Favorable/Unfavorable Views of Biden or Trump",
caption = "@ryanburge + @religiondata\nData: PRRI/Brookings, 2023")
save("cn_dk_favorables.png")
regg <- prri %>%
mutate(cn = q42_c) %>%
mutate(cn = case_when(cn == 5 ~ 0,
cn <= 4 ~ 1)) %>%
mutate(pid3 = qpid100) %>%
mutate(dem = case_when(pid3 == 2 ~ 1,
pid3 == 3 | pid3 == 1 ~ 0)) %>%
mutate(age = ppage) %>%
mutate(educ = ppeduc5) %>%
mutate(male = case_when(ppgender == 1 ~ 1,
ppgender == 2 ~ 0)) %>%
mutate(income = i_income) %>%
mutate(none = case_when(q22 == 13 ~ 1,
TRUE ~ 0)) %>%
mutate(white = case_when(ppethm == 1 ~ 1,
TRUE ~ 0)) %>%
mutate(att = 7 - attend_4_b) %>%
select(cn, dem, age, educ, male, income, none, white, att)
library(jtools)
coef_names <- c("White" = "white",
"Income" = "income",
"Education" = "educ",
"Age" = "age",
"Male" = "male",
"Democrat" = "dem",
"Church Attendance" = "att",
"No Religion" = "none")
out <- glm(cn ~ white + male + educ + income + att + dem + age + none, family = "binomial", data = regg)
gg <- plot_summs(out, scale = TRUE, robust = "HC3", coefs = coef_names, colors = "#5f2680")
gg +
theme_rb() +
# add_text(x = -.5, y = 3.5, word = "Less Agreement", sz = 9) +
add_text(x = .5, y = 7.5, word = "More Likely\nto Know of CN", sz = 9) +
labs(x = "", y = "", title = "What Factors Drive Up the Likelihood of Having Heard of Christian Nationalism?",
caption = "@ryanburge + @religiondata\nData: PRRI/Brookings, 2023")
save("reg_cn_know.png", ht = 6)
gg <- prri %>%
mutate(cn = q42_c) %>%
mutate(cn = case_when(cn == 5 ~ 1,
cn <= 4 ~ 0)) %>%
mutate(media = tv_2) %>%
mutate(media = frcode(media == 1 ~ "ABC/NBC/CBS",
media == 2 ~ "CNN",
media == 3 ~ "Fox News",
media == 4 ~ "MSNBC",
media == 5 ~ "Local News",
media == 6 ~ "PBS",
media == 8 ~ "Don't Watch",
media == 11 ~ "OAN/Newsmax")) %>%
group_by(media) %>%
mean_ci(cn, wt = weight, ci = .84) %>%
filter(media != "NA")
gg %>%
mutate(lab = round(mean, 2)) %>%
ggplot(., aes(x = reorder(media, mean), y = mean, fill = media)) +
geom_col(color = "black") +
coord_flip() +
scale_fill_manual(values = c("#28666e", "#5f2680", "#B5B682", "azure4", "#7D3C98", "#5f2680", "#033f63", "#28666e", "#e1e1e1", "#B5B682", "#FEDC97", "#7D3C98", "#5f2680")) +
y_pct() +
theme_rb() +
error_bar() +
geom_text(aes(y = .03, label = paste0(lab*100, '%')), position = position_dodge(width = .9), size = 9, family = "font", color = 'white') +
geom_text(aes(y = .03, label = ifelse(media == "Fox News", paste0(lab*100, '%'), "")), position = position_dodge(width = .9), size = 9, family = "font", color = "black") +
geom_text(aes(y = .03, label = ifelse(media == "MSNBC", paste0(lab*100, '%'), "")), position = position_dodge(width = .9), size = 9, family = "font", color = "black") +
labs(x = "", y = "", title = "Share Saying They Have Never Heard of Christian Nationalism",
subtitle = "Based on Media Source They Trust the Most",
caption = "@ryanburge + @religiondata\nData: PRRI/Brookings, 2023")
save("dk_cn_media.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment