Skip to content

Instantly share code, notes, and snippets.

@ryanburge
Created August 14, 2025 13:07
Show Gist options
  • Select an option

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

Select an option

Save ryanburge/4449be616cd44e3a0834ff06f56d05be to your computer and use it in GitHub Desktop.
library(rio)
library(janitor)
pew <- import("E://data/rls24.sav")
pew <- pew %>% clean_names()
gg1 <- pew %>%
mutate(moral = qb2d) %>%
mutate(moral = frcode(moral == 1 ~ "There are clear absolue standards",
moral == 2 ~ "It depends on the situation",
moral == 99 ~ "Don't Know/Refused")) %>%
ct(moral, wt = weight, show_na = FALSE)
gg1 %>%
ggplot(., aes(x = reorder(moral, pct), y = pct, fill = moral)) +
geom_col(color = "black") +
coord_flip()+
theme_rb() +
scale_fill_manual(values = c(
"There are clear absolute standards" = "#4E79A7", # slate blue
"It depends on the situation" = "#F28E2B", # orange
"Don't Know/Refused" = "#A0CBE8" # light blue
)) +
scale_y_continuous(labels = percent, limits = c(0, .65)) +
lab_bar(above = TRUE, pos = .045, sz = 9, type = pct) +
labs(x = "", y = "", title = "Are There Clear Standards or Is it Situational?", caption = "@ryanburge | Data: Pew Religious Landscape Survey, 2023-2024")
save("pew_clear_morals.png", ht = 2.5, wd = 8)
gg1 <- pew %>%
mutate(trad = frcode(
reltrad == 1100 ~ "Evangelical",
reltrad == 1200 ~ "Mainline",
reltrad == 1300 ~ "Black Protestant",
reltrad == 10000 ~ "Catholic",
reltrad >= 20000 & reltrad <= 99999 ~ "Other World Religions",
reltrad == 100000 & unaffildetail %in% c(1, 2) ~ "Atheist/Agnostic",
reltrad == 100000 & unaffildetail == 3 ~ "Nothing in Particular")) %>%
mutate(moral = qb2d) %>%
mutate(moral = frcode(moral == 1 ~ "There are clear absolute standards",
moral == 2 ~ "It depends on the situation",
moral == 99 ~ "Don't Know/Refused")) %>%
group_by(trad) %>%
ct(moral, wt = weight, show_na = FALSE)
gg1 %>%
filter(trad != "NA") %>%
mutate(lab = round(pct, 2)) %>%
ggplot(., aes(x = 1, y = pct, fill = fct_rev(moral))) +
geom_col(color = "black") +
coord_flip() +
facet_wrap(~ trad, ncol =1, strip.position = "left") +
theme_rb() +
scale_fill_manual(values = c(
"There are clear absolute standards" = "#4E79A7", # slate blue
"It depends on the situation" = "#F28E2B", # orange
"Don't Know/Refused" = "#A0CBE8" # light blue
)) +
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 = 7, 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 = 16)) +
theme(strip.text.y.left = element_text(angle = 0, hjust = 1)) +
labs(x = "", y = "", title = "Are There Clear Standards or Is it Situational?", caption = "@ryanburge | Data: Pew Religious Landscape Survey, 2023-2024")
save("clear_moral_reltrad.png", wd = 9, ht = 4)
gg1 <- pew %>%
mutate(decade = frcode(
birthdecade == 1 ~ "1940s",
birthdecade == 2 ~ "1950s",
birthdecade == 3 ~ "1960s",
birthdecade == 4 ~ "1970s",
birthdecade == 5 ~ "1980s",
birthdecade == 6 ~ "1990s",
birthdecade == 7 ~ "2000s"
)) %>%
mutate(moral = qb2d) %>%
mutate(moral = frcode(moral == 1 ~ "There are clear absolute standards",
moral == 2 ~ "It depends on the situation",
moral == 99 ~ "Don't Know/Refused")) %>%
group_by(decade) %>%
ct(moral, wt = weight, show_na = FALSE)
gg1 %>%
filter(decade != "NA") %>%
mutate(lab = round(pct, 2)) %>%
ggplot(., aes(x = 1, y = pct, fill = fct_rev(moral))) +
geom_col(color = "black") +
coord_flip() +
facet_wrap(~ decade, ncol =1, strip.position = "left") +
theme_rb() +
scale_fill_manual(values = c(
"There are clear absolute standards" = "#4E79A7", # slate blue
"It depends on the situation" = "#F28E2B", # orange
"Don't Know/Refused" = "#A0CBE8" # light blue
)) +
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 = 7, 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 = 16)) +
theme(strip.text.y.left = element_text(angle = 0, hjust = 1)) +
labs(x = "", y = "", title = "Are There Clear Standards or Is it Situational?", caption = "@ryanburge | Data: Pew Religious Landscape Survey, 2023-2024")
save("clear_moral_decades.png", wd = 9, ht = 4.5)
gg1 <- pew %>%
mutate(trad = frcode(
reltrad == 1100 ~ "Evangelical",
reltrad == 1200 ~ "Mainline",
reltrad == 1300 ~ "Black Protestant",
reltrad == 10000 ~ "Catholic",
reltrad >= 20000 & reltrad <= 99999 ~ "Other World Religions",
reltrad == 100000 ~ "Non-\nReligious")) %>%
mutate(decade = frcode(
birthdecade == 1 ~ "1940s",
birthdecade == 2 ~ "1950s",
birthdecade == 3 ~ "1960s",
birthdecade == 4 ~ "1970s",
birthdecade == 5 ~ "1980s",
birthdecade == 6 ~ "1990s",
birthdecade == 7 ~ "2000s"
)) %>%
filter(trad == "Evangelical" | trad == "Catholic" | trad == "Mainline" | trad == "Non-\nReligious") %>%
group_by(trad, decade) %>%
mutate(moral = qb2d) %>%
mutate(moral = case_when(moral == 1 ~ 1,
moral == 2 | moral == 3 ~ 0)) %>%
mean_ci(moral, wt = weight, ci = .84) %>% filter(decade != 'NA')
gg1 %>%
ggplot(., aes(x = trad, y = mean, fill = trad)) +
geom_col(color = "black") +
facet_wrap(~ decade, ncol = 2) +
theme_rb() +
error_bar() +
y_pct() +
theme(axis.text.x = element_text(size = 10)) +
scale_fill_manual(values = c(
"Evangelical" = "#1b9e77", # green
"Catholic" = "#d95f02", # orange
"Mainline" = "#7570b3", # purple
"Non-\nReligious" = "#e7298a" # pink
)) +
lab_bar(above = FALSE, type = mean, pos = .065, sz = 7) +
labs(x = "", y = "", title = "Share Saying There are Clear Standards\nof Right and Wrong", caption = "@ryanburge | Data: Pew Religious Landscape Survey, 2023-2024")
save('clear_moral_decade_reltrad.png', ht = 10, wd = 5.5)
gg1 <- pew %>%
mutate(ideo = frcode(ideo == 1 ~ "Very\nConservative",
ideo == 2 ~ "Conservative",
ideo == 3 ~ "Moderate",
ideo == 4 ~ "Liberal",
ideo == 5 ~ "Very\nLiberal")) %>%
mutate(moral = qb2d) %>%
mutate(moral = case_when(moral == 1 ~ 1,
moral == 2 | moral == 3 ~ 0)) %>%
mutate(relig = frcode(reltrad <= 99999 ~ "Religious",
reltrad == 100000 ~ "Non-Religious")) %>%
group_by(ideo, relig) %>%
mean_ci(moral, wt = weight, ci = .84) %>% na.omit()
gg1 %>%
ggplot(aes(x = ideo, y = mean, fill = relig)) +
geom_col(position = position_dodge(width = 0.9), color = "black") +
error_bar() +
scale_y_continuous(labels = scales::percent_format()) +
scale_fill_manual(values = c(
"Religious" = "#264653",
"Non-Religious" = "#E76F51"
)) +
lab_bar_white(above = FALSE, type = mean, pos = .04, sz = 6.25) +
labs(
x = "Political Ideology",
y = "",
title = "Share Saying There are Clear Standards of Right and Wrong",
caption = "@ryanburge | Data: Pew Religious Landscape Survey, 2023–2024",
fill = ""
) +
theme_rb(legend = TRUE)
save("ideo_clear_moral_relig2.png", wd = 6)
regg <- pew %>%
filter(birthdecade != 99) %>%
mutate(moral = qb2d) %>%
mutate(moral = case_when(moral == 1 ~ 1,
moral == 2 | moral == 3 ~ 0)) %>%
mutate(white = case_when(
hisp == 2 & racecmb == 1 ~ 1,
TRUE ~ 0
)) %>%
mutate(male = case_when(gender == 1 ~ 1,
TRUE ~ 0)) %>%
mutate(educ = educrec) %>%
mutate(age = 8 - birthdecade) %>%
mutate(income = inc_sdt1) %>%
mutate(cons = case_when(ideo == 1 | ideo == 2 ~ 1,
ideo <= 5 ~ 0)) %>%
mutate(relig = case_when(reltrad <= 99999 ~ 1,
reltrad == 100000 ~ 0)) %>%
mutate(attend = 7 - attndperrls) %>%
select(moral, white, male, educ, age, income, cons, relig, attend, weight)
fit <- glm(moral ~ white + male + educ + relig + attend + income + age + cons,
data = regg, family = binomial())
coef_or <- tidy(fit, conf.int = TRUE, conf.level = 0.95, exponentiate = TRUE) %>%
filter(term != "(Intercept)") %>%
mutate(term = dplyr::recode(term,
white = "White",
male = "Male",
relig = "Religious",
attend = "Religious Attendance",
cons = "Conservative",
age = "Age",
educ = "Education",
income= "Income"
))
ggplot(coef_or, aes(x = estimate, y = reorder(term, estimate))) +
geom_vline(xintercept = 1, linetype = 2) +
geom_errorbarh(aes(xmin = conf.low, xmax = conf.high), height = 0.18) +
geom_point(size = 2.8, stroke = 1, shape = 21, fill = "white", color = "firebrick3") +
scale_x_log10() +
labs(
x = "Odds ratio (log scale, 95% CI)",
y = "",
title = "What Factors Predict a More Black and White Worldview?",
caption = "@ryanburge | Pew Religious Landscape Survey, 2023–2024"
) +
theme_rb()
save("coef_clear_moral.png", ht = 3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment