Skip to content

Instantly share code, notes, and snippets.

@ryanburge
Created December 11, 2024 04:04
Show Gist options
  • Save ryanburge/4279a9aa7c006a6e674d4328748b9dd4 to your computer and use it in GitHub Desktop.
Save ryanburge/4279a9aa7c006a6e674d4328748b9dd4 to your computer and use it in GitHub Desktop.
library(rio)
library(janitor)
ch <- import("E://data/chapman21.sav") %>% clean_names()
# List of questions and their respective labels
questions <- list(
q20a = "Aliens",
q20b = "9/11 Attacks",
q20c = "South Dakota Crash",
q20d = "Global Warming",
q20e = "JFK Assassination",
q20f = "Moon Landing",
q20g = "Illuminati/New World Order",
q20h = "Mass Shootings",
q20i = "Q'Anon"
)
# Function to calculate mean and confidence intervals
calculate_ci <- function(data, question, label) {
data %>%
mutate(qq = case_when(
!!sym(question) == 1 | !!sym(question) == 2 ~ 1, # Agreement
!!sym(question) == 3 | !!sym(question) == 4 ~ 0 # Disagreement
)) %>%
mean_ci(qq, wt = weight, ci = .84) %>%
mutate(type = label)
}
# Apply the function to each question and combine results
results <- bind_rows(
lapply(names(questions), function(q) {
calculate_ci(ch, q, questions[[q]])
})
)
results %>%
mutate(lab = round(mean, 2)) %>%
ggplot(., aes(x = reorder(type, mean), y = mean, fill = type)) +
geom_col(color = "black") +
coord_flip() +
theme_rb() +
error_bar() +
y_pct() +
scale_fill_manual(values =met.brewer("Klimt", 9)) +
lab_bar(top = FALSE, type = lab, pos = .05, sz = 10) +
geom_text(aes(y = .05, label = ifelse(type == "Moon Landing", paste0(lab*100, '%'), "")), position = position_dodge(width = .9), size = 10, family = "font", color = "white") +
geom_text(aes(y = .05, label = ifelse(type == "Q'Anon", paste0(lab*100, '%'), "")), position = position_dodge(width = .9), size = 10, family = "font", color = "white") +
geom_text(aes(y = .05, label = ifelse(type == "South Dakota Crash", paste0(lab*100, '%'), "")), position = position_dodge(width = .9), size = 10, family = "font", color = "white") +
geom_text(aes(y = .05, label = ifelse(type == "Mass Shootings", paste0(lab*100, '%'), "")), position = position_dodge(width = .9), size = 10, family = "font", color = "white") +
labs(x = "", y = "", title = "Share Agreeing the Government is Concealing What It Knows About...",
caption = "@ryanburge + @religiondata\nData: Chapman Survey of American Fears, 2021")
save("chapman_conspiracies_all.png")
# Function to calculate mean and confidence intervals
calculate_ci <- function(data, question, label) {
data %>%
mutate(qq = case_when(
!!sym(question) == 1 | !!sym(question) == 2 ~ 1, # Agreement
!!sym(question) == 3 | !!sym(question) == 4 ~ 0 # Disagreement
)) %>%
mutate(rel = frcode(q1 == 1 ~ "Not\nat All",
q1 == 2 ~ "Not\nToo",
q1 == 3 ~ "Somewhat",
q1 == 4 ~ "Very\nReligious")) %>%
group_by(rel) %>%
mean_ci(qq, wt = weight, ci = .84) %>%
mutate(type = label)
}
# Apply the function to each question and combine results
results <- bind_rows(
lapply(names(questions), function(q) {
calculate_ci(ch, q, questions[[q]])
})
)
palette <- c(
"Not\nat All" = "#1f78b4", # Blue
"Not\nToo" = "#33a02c", # Green
"Somewhat" = "#e31a1c", # Red
"Very\nReligious" = "#ff7f00" # Orange
)
results %>%
mutate(lab = round(mean, 2)) %>%
ggplot(., aes(x = rel, y = mean, fill = rel)) +
geom_col(color = 'black') +
facet_wrap(~ type) +
scale_fill_manual(values = palette) +
error_bar() +
theme_rb() +
y_pct() +
lab_bar(top = FALSE, type = lab, pos = .045, sz = 7) +
labs(x = "How Religious Are You?", y = "", title = "Share Agreeing the Government is Concealing What It Knows About...",
caption = "@ryanburge + @religiondata\nData: Chapman Survey of American Fears, 2021")
save("rel_fears_conspiracies_by_category.png", ht = 10, wd = 8.25)
ch <- ch %>%
mutate(across(starts_with("q20"), ~ dplyr::recode(.,
`1` = 1, # Strongly agree
`2` = 1, # Agree
`3` = 0, # Disagree
`4` = 0, # Strongly disagree
.default = NA_real_))) %>%
rowwise() %>%
mutate(con_index = sum(c_across(starts_with("q20")), na.rm = TRUE)) %>%
ungroup()
graph <- ch %>%
mutate(rel = frcode(q1 == 1 ~ "Not\nat All",
q1 == 2 ~ "Not\nToo",
q1 == 3 ~ "Somewhat",
q1 == 4 ~ "Very\nReligious")) %>%
group_by(rel) %>%
mean_ci(con_index, wt = weight, ci = .84)
palette <- c(
"Not\nat All" = "#1f78b4", # Blue
"Not\nToo" = "#33a02c", # Green
"Somewhat" = "#e31a1c", # Red
"Very\nReligious" = "#ff7f00" # Orange
)
graph %>%
mutate(lab = round(mean, 2)) %>%
ggplot(., aes(x = rel, y = mean, fill = rel)) +
geom_col(color = "black") +
theme_rb() +
error_bar() +
scale_fill_manual(values = palette) + # Apply the custom color palette
geom_text(aes(y = .5, label = lab), position = position_dodge(width = .9), size = 12, family = "font") +
labs(x = "How Religious Are You?", y = "Number of Conspiracies Embraced", title = "Is There a Relationship Between Religiosity\nand Conspiratorial Thinking?",
caption = "@ryanburge + @religiondata\nData: Chapman Survey of American Fears, 2021")
save("rel_conspiracy_chapman.png", wd = 5)
graph <- ch %>%
mutate(att = q2) %>%
mutate(att = frcode(att == 1 ~ "Never",
att == 2 ~ "Special\nOccasions",
att == 3 | att == 4 | att == 5 ~ "Yearly",
att == 6 | att == 7 ~ "Monthly",
att == 8 | att == 9 ~ "Weekly")) %>%
group_by(att) %>%
mean_ci(con_index, wt = weight, ci = .84)
palette <- c(
"Never" = "#a6cee3", # Light Blue
"Special\nOccasions" = "#b2df8a", # Light Green
"Yearly" = "#fb9a99", # Light Red
"Monthly" = "#fdbf6f", # Light Orange
"Weekly" = "#cab2d6" # Light Purple
)
graph %>%
mutate(lab = round(mean, 2)) %>%
ggplot(., aes(x = att, y = mean, fill = att)) +
geom_col(color = "black") +
theme_rb() +
error_bar() +
scale_fill_manual(values = palette) + # Apply the custom color palette
geom_text(aes(y = .5, label = lab), position = position_dodge(width = .9), size = 11, family = "font") +
labs(x = "How Often Do You Attend Religious Services?", y = "Number of Conspiracies Embraced", title = "Is There a Relationship Between Religious Attendance\nand Conspiratorial Thinking?",
caption = "@ryanburge + @religiondata\nData: Chapman Survey of American Fears, 2021")
save("att_conspiracy_chapman.png", wd = 5.5)
ch <- ch %>%
mutate(across(starts_with("q20"), ~ dplyr::recode(.,
`1` = 1, # Strongly agree
`2` = 1, # Agree
`3` = 0, # Disagree
`4` = 0, # Strongly disagree
.default = NA_real_))) %>%
rowwise() %>%
mutate(con_index = sum(c_across(starts_with("q20")), na.rm = TRUE)) %>%
ungroup()
reg <- ch %>%
mutate(married = case_when(i_marital == 3 ~ 1,
TRUE ~ 0)) %>%
mutate(white = case_when(i_race == 1 ~ 1,
TRUE ~ 0)) %>%
mutate(male = case_when(i_sex == 1 ~ 1,
i_sex == 2 ~ 0)) %>%
mutate(cons = case_when(q4 == 1 | q4 == 2 | q4 == 3 ~ 1,
q4 <= 7 ~ 0)) %>%
mutate(literal = case_when(q3 == 1 ~ 1,
q3 == 2 | q3 == 3 | q3 == 4 | q3 == 5 ~ 0)) %>%
select(con_index, attend = q2, literal, cons, married, white, male, age = i_age, educ)
out <- glm(con_index ~ ., data = reg)
coef_names <- c("White" = "white",
"Education" = "educ",
"Age" = "age",
"Male" = "male",
"Married" = "married",
"Church Attendance" = "attend",
"Biblical Literalism" = "literal",
"Politically Conservative" = "cons")
out <- plot_summs(out, scale = TRUE, robust = "HC3", colors = "firebrick3", coefs = coef_names)
out +
theme_rb() +
labs(x = "", y = "", title = "What Factors Predict Stronger Conspiratorial Thinking?",
caption = "@ryanburge + @religiondata\nData: Chapman Survey of American Fears, 2021")
# add_text(x = -9, y = 3.5, word = "Lower Fear Index", sz = 6)
save("conspiracy_plot_summs_chapman.png", ht = 4, wd = 9.5)
reg <- ch %>%
mutate(married = case_when(i_marital == 3 ~ 1,
TRUE ~ 0)) %>%
mutate(white = case_when(i_race == 1 ~ 1,
TRUE ~ 0)) %>%
mutate(male = case_when(i_sex == 1 ~ 1,
i_sex == 2 ~ 0)) %>%
mutate(id7 = 8-q4) %>%
mutate(bible = frcode(q3 == 1 ~ "Literal",
q3 == 2 ~ "Inspired, Not Literal",
q3 == 3 ~ "Some Errors",
q3 == 4 ~ "History and Legends")) %>%
mutate(literal = case_when(q3 == 1 ~ 1,
q3 == 2 | q3 == 3 | q3 == 4 | q3 == 5 ~ 0)) %>%
select(con_index, attend = q2, bible, id7, married, white, male, age = i_age, educ)
out <- glm(con_index ~ bible*id7 + ., data = reg)
library(interactions)
pp <- interact_plot(out, pred= id7, modx = bible, int.width = .76, interval = TRUE)
pp +
theme_rb(legend = TRUE) +
scale_x_continuous(breaks = c(1,2,3,4,5,6,7), labels = c("Extremely\nLiberal", "Liberal", "Leaning\nLiberal",
"Moderate",
"Leaning\nConservative", "Conservative", "Extremely\nConservative")) +
labs(x = "Political Ideology", y = "Average Number of Conspiracies Embraced", title = "Interaction of Political Ideology and Views of the Bible on Conspiratorial Thinking",
caption = "@ryanburge + @religiondata\nData: Chapman Survey of American Fears, 2021")
save("con_thinking_inteaction_bible_id7.png", ht = 8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment