Skip to content

Instantly share code, notes, and snippets.

@ryanburge
Created February 16, 2025 20:29
Show Gist options
  • Save ryanburge/2e5bba36e700c8afb4a984dbd074f247 to your computer and use it in GitHub Desktop.
Save ryanburge/2e5bba36e700c8afb4a984dbd074f247 to your computer and use it in GitHub Desktop.
all <- prri %>%
select(weight, q28_a:q28_e) %>%
pivot_longer(cols = q28_a:q28_e, names_to = "topic", values_to = "qq") %>%
mutate(
qq = frcode(qq == 1 ~ "Often",
qq == 2 ~ "Sometimes",
qq == 3 ~ "Rarely",
qq == 4 ~ "Never"),
qq = fct_relevel(qq, "Never", "Rarely", "Sometimes", "Often"), # Reorder factor levels
type = dplyr::recode(topic,
"q28_a" = "Abortion",
"q28_b" = "Donald Trump",
"q28_c" = "Election/Voter Fraud",
"q28_d" = "Racism",
"q28_e" = "Poverty and Inequality")
) %>%
group_by(type) %>% # Group by `type` so `ct()` retains it
ct(qq, wt = weight, show_na = FALSE) %>%
ungroup() # Ungroup for safety
# Reorder 'type' based on 'Never' percentage
never_order <- all %>%
filter(qq == "Never") %>% # Select only 'Never' responses
arrange(desc(pct)) %>% # Sort in descending order
pull(type) # Extract ordered factor levels
# Apply ordering to 'type' before plotting
all <- all %>%
mutate(type = factor(type, levels = never_order)) # Reorder factor levels
# Now, plot using the new order
all %>%
mutate(lab = round(pct, 2)) %>%
ggplot(aes(x = 1, y = pct, fill = fct_rev(qq))) +
geom_col(color = "black") +
coord_flip() +
facet_wrap(~ type, ncol =1, strip.position = "left") + # Now ordered by "Never"
theme_rb() +
theme(legend.position = "bottom") +
scale_fill_manual(values = c("Never" = "#440154",
"Rarely" = "#31688E",
"Sometimes" = "#35B779",
"Often" = "#FDE725")) +
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 = "black") +
theme(plot.title = element_text(size = 16)) +
theme(strip.text.y.left = element_text(angle = 0, hjust = 1)) +
geom_text(aes(label = ifelse(qq == "Never", paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 8, family = "font", color = "white") +
geom_text(aes(label = ifelse(qq == "Rarely", paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 8, family = "font", color = "white") +
labs(x = "", y = "",
title = "How often does the clergy at your church ever talk about...",
caption = "@ryanburge + @religiondata\nData: PRRI 2022 Health of Congregations Survey")
# Save the plot
save("sermon_topics.png", wd = 9, ht = 4)
all <- prri %>%
mutate(relig = frcode(q22 == 2 & q23 == 1 ~ "Evangelical",
q22 == 2 & q23 == 2 ~ "Non-Evangelical",
q22 == 1 ~ "Catholic",
q22 == 4 ~ "LDS")) %>%
select(weight, relig, q28_a:q28_e) %>% # Include relig in selection
pivot_longer(cols = q28_a:q28_e, names_to = "topic", values_to = "qq") %>%
mutate(
qq = frcode(qq == 1 ~ "Often",
qq == 2 ~ "Sometimes",
qq == 3 ~ "Rarely",
qq == 4 ~ "Never"),
qq_bin = case_when(qq %in% c("Never", "Rarely") ~ 0, # Recode Never/Rarely as 0
qq %in% c("Sometimes", "Often") ~ 1), # Recode Sometimes/Often as 1
type = dplyr::recode(topic,
"q28_a" = "Abortion",
"q28_b" = "Donald Trump",
"q28_c" = "Election/Voter Fraud",
"q28_d" = "Racism",
"q28_e" = "Poverty and Inequality")
) %>%
group_by(relig, type) %>% # Now grouped by both `relig` and `type`
mean_ci(qq_bin, wt = weight, ci = .84) %>%
ungroup()
all %>%
filter(relig != 'NA') %>%
ggplot(., aes(x = fct_rev(relig), y = mean, fill = fct_rev(relig))) +
geom_col(color = "black", position = "dodge") +
facet_wrap(~ type) +
coord_flip() +
theme_rb() +
scale_fill_manual(values = c("Evangelical Protestant" = "#1F77B4",
"Non-Evangelical" = "#FF7F0E",
"Catholic" = "#2CA02C",
"LDS" = "#D62728",
"Jewish" = "#9467BD")) +
lab_bar(above = TRUE, type = mean, pos = .06, sz = 5) +
y_pct() +
labs(
title = "How often does the clergy talk about these topics?",
subtitle = "Proportion of sometimes/often mentions by religious group",
x = "",
y = "",
caption = "@ryanburge + @religiondata\nData: PRRI 2022 Health of Congregations Survey"
)
save("sermon_topics_by_relig.png", ht = 5, wd = 12)
gg <- prri %>%
mutate(relig = frcode(q22 == 2 & q23 == 1 ~ "Evangelical",
q22 == 2 & q23 == 2 ~ "Non-Evangelical",
q22 == 1 ~ "Catholic",
q22 == 4 ~ "LDS")) %>%
mutate(divide = q31_b) %>%
mutate(divide = frcode(divide == 1 ~ "Yes",
divide == 2 ~ "No",
divide == 3 ~ "Unsure")) %>%
group_by(relig) %>%
ct(divide, wt = weight, show_na = FALSE) %>%
filter(relig != "NA") %>%
mutate(divide = fct_rev(divide))
# Now, plot using the new order
gg %>%
mutate(lab = round(pct, 2)) %>%
ggplot(aes(x = 1, y = pct, fill = fct_rev(divide))) +
geom_col(color = "black") +
coord_flip() +
facet_wrap(~ relig, ncol =1, strip.position = "left") + # Now ordered by "Never"
theme_rb() +
theme(legend.position = "bottom") +
scale_fill_manual(values = c("Yes" = "#E15759",
"No" = "#4E79A7",
"Unsure" = "#F28E2B")) +
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 = "black") +
theme(plot.title = element_text(size = 16)) +
theme(strip.text.y.left = element_text(angle = 0, hjust = 1)) +
# geom_text(aes(label = ifelse(qq == "Never", paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 8, family = "font", color = "white") +
# geom_text(aes(label = ifelse(qq == "Rarely", paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 8, family = "font", color = "white") +
labs(x = "", y = "",
title = "Is your church more divided by politics than it was five years ago?",
caption = "@ryanburge + @religiondata\nData: PRRI 2022 Health of Congregations Survey")
# Save the plot
save("politically_divided.png", wd = 9, ht = 4)
gg <- prri %>%
mutate(relig = frcode(q22 == 2 & q23 == 1 ~ "Evangelical",
q22 == 2 & q23 == 2 ~ "Non-Evangelical",
q22 == 1 ~ "Catholic",
q22 == 4 ~ "LDS")) %>%
select(weight, relig, q30_a:q30_e) %>% # Select relevant columns
pivot_longer(cols = q30_a:q30_e, names_to = "topic", values_to = "issue") %>%
mutate(
issue = frcode(issue == 4 ~ "Not well at all",
issue == 3 ~ "Not very well",
issue == 2 ~ "Somewhat well",
issue == 1 ~ "Very well"),
type = dplyr::recode(topic,
"q30_a" = "Racial Justice",
"q30_b" = "LGBTQ+ Discrimination",
"q30_c" = "Hatred Toward Immigrants",
"q30_d" = "White Supremacy",
"q30_e" = "Abortion")
) %>%
group_by(relig, type) %>%
ct(issue, wt = weight, show_na = FALSE) %>%
na.omit() %>% filter(issue == "Very well")
gg %>%
ggplot(., aes(x = type, y = pct, fill = type)) +
geom_col(color = "black") +
coord_flip() +
lab_bar(type = pct, above = TRUE, pos = .06, sz = 6) +
facet_wrap(~ relig) +
theme_rb() +
scale_fill_manual(values = c("Racial Justice" = "#1F77B4",
"LGBTQ+ Discrimination" = "#FF7F0E",
"Hatred Toward Immigrants" = "#2CA02C",
"White Supremacy" = "#D62728",
"Abortion" = "#9467BD")) +
scale_y_continuous(labels = percent, limits = c(0, .75)) +
labs(x = "", y = "", title = "Share Saying My Church is Handling the Following Issues Very Well",
caption = "@ryanburge + @religiondata\nData: PRRI 2022 Health of Congregations Survey")
save("well_handled_church.png")
gg <- prri %>%
mutate(relig = frcode(q22 == 2 & q23 == 1 ~ "Evangelical",
q22 == 2 & q23 == 2 ~ "Non-Evangelical",
q22 == 1 ~ "Catholic",
q22 == 4 ~ "LDS")) %>%
mutate(div = q32_b) %>%
mutate(div = frcode(div == 4 ~ "Completely Disagree",
div == 3 ~ "Mostly Disagree",
div == 2 ~ "Mostly Agree",
div == 1 ~ "Completely Agree")) %>%
group_by(relig) %>%
ct(div, wt = weight, show_na = FALSE) %>%
na.omit()
# Now, plot using the new order
gg %>%
mutate(lab = round(pct, 2)) %>%
ggplot(aes(x = 1, y = pct, fill = fct_rev(div))) +
geom_col(color = "black") +
coord_flip() +
facet_wrap(~ relig, ncol =1, strip.position = "left") + # Now ordered by "Never"
theme_rb() +
theme(legend.position = "bottom") +
scale_fill_manual(values = c("Completely Disagree" = "#D62728",
"Mostly Disagree" = "#FF7F0E",
"Mostly Agree" = "#1F77B4",
"Completely Agree" = "#2CA02C")) +
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 = "black") +
theme(plot.title = element_text(size = 16)) +
theme(strip.text.y.left = element_text(angle = 0, hjust = 1)) +
geom_text(aes(label = ifelse(div == "Completely Disagree", paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 8, family = "font", color = "white") +
# geom_text(aes(label = ifelse(qq == "Rarely", paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 8, family = "font", color = "white") +
labs(x = "", y = "",
title = "I wish my church talked more about political division in this country.",
caption = "@ryanburge + @religiondata\nData: PRRI 2022 Health of Congregations Survey")
save("talk_about_political_division.png", ht = 4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment