Skip to content

Instantly share code, notes, and snippets.

@ryanburge
Created January 11, 2024 23:35
Show Gist options
  • Save ryanburge/5f2dd02204e5d8eee69678fdcfb44470 to your computer and use it in GitHub Desktop.
Save ryanburge/5f2dd02204e5d8eee69678fdcfb44470 to your computer and use it in GitHub Desktop.
library(rio)
library(janitor)
spirit <- import("E://data/state_spiritual.dta") %>% clean_names()
fun <- function(var, name){
spirit %>%
mutate(vv = {{var}}) %>%
mean_ci(vv, ci = .84, wt = weight) %>%
mutate(type = name)
}
var <- syms(c("q2_1", "q2_2", "q2_3", "q2_4", "q2_5", "q2_6", "q2_7", "q2_8", "q2_9", "q2_10", "q2_11", "q2_12", "q2_13"))
name <- c("Purpose", "Morality", "Values", "Belonging", "Peace", "Inspiration", "Security", "Love", "Structure", "Wonder", "Transformation", "Clarity", "Transcendence")
gg1 <- map2_df(var, name, fun, .progress = TRUE) %>% mutate(cat = "Spirituality")
var <- syms(c("q4_1", "q4_2", "q4_3", "q4_4", "q4_5", "q4_6", "q4_7", "q4_8", "q4_9", "q4_10", "q4_11", "q4_12", "q4_13"))
name <- c("Purpose", "Morality", "Values", "Belonging", "Peace", "Inspiration", "Security", "Love", "Structure", "Wonder", "Transformation", "Clarity", "Transcendence")
gg2 <- map2_df(var, name, fun, .progress = TRUE) %>% mutate(cat = "Religion")
both <- bind_rows(gg1, gg2)
both %>%
mutate(lab = round(mean, 2)) %>%
ggplot(., aes(x = cat, y = mean, fill = cat)) +
geom_col(color = "black") +
facet_wrap(~ type) +
scale_fill_calc() +
theme_rb() +
y_pct() +
error_bar() +
geom_text(aes(y = .065, label = paste0(lab*100, '%')), position = position_dodge(width = .9), size = 7, family = "font", color = "white") +
labs(x = "", y = "Share Saying This Word Describes Religion/Spirituality to Them", title = "What Does Religion/Spirituality Mean to Americans?", caption = "@ryanburge\nData: The National Religion and Spirituality Survey, November 2022")
save("rel_spiritual_words.png", wd = 6, ht = 10)
god <- spirit %>%
mutate(god = q14) %>%
mutate(god = frcode(god == 5 ~ "Believe, No Doubts",
god == 4 ~ "Believe More than Doubt",
god == 3 ~ "Doubt More than Believe",
god == 2 ~ "Some Higher Power",
god == 1 ~ "Don't Believe")) %>%
ct(god, wt = weight, show_na = FALSE)
god %>%
mutate(lab = round(pct, 2)) %>%
ggplot(., aes(x = fct_rev(god), y = pct, fill = god)) +
geom_col(color = "black") +
coord_flip() +
theme_rb() +
scale_fill_manual(values = c("#033f63", "#28666e", "#e1e1e1", "#B5B682", "#FEDC97")) +
y_pct() +
geom_text(aes(y = pct + .025, label = ifelse(pct < .10, paste0(lab*100, '%'),"")), position = position_dodge(width = .9), size = 9, family = "font") +
geom_text(aes(y = pct + .027, label = ifelse(pct > .10, paste0(lab*100, '%'),"")), position = position_dodge(width = .9), size = 9, family = "font") +
theme(plot.title = element_text(hjust = 2)) +
labs(x = "", y = "", title = "Which statement comes closest to expressing what you believe about a higher power", caption = "@ryanburge\nData: The National Religion and Spirituality Survey, November 2022")
save("belief_god_nov22.png", ht = 4)
c1 <- spirit %>%
mutate(var = q16_1) %>%
mutate(var = frcode(var == 1 ~ "Not at all",
var == 2 ~ "Slightly",
var == 3 ~ "Moderately",
var == 4 ~ "Highly")) %>%
ct(var, wt = weight, show_na = FALSE) %>%
mutate(type = "A Higher Power")
c2 <- spirit %>%
mutate(var = q16_2) %>%
mutate(var = frcode(var == 1 ~ "Not at all",
var == 2 ~ "Slightly",
var == 3 ~ "Moderately",
var == 4 ~ "Highly")) %>%
ct(var, wt = weight, show_na = FALSE) %>%
mutate(type = "All of Humanity")
c3 <- spirit %>%
mutate(var = q16_3) %>%
mutate(var = frcode(var == 1 ~ "Not at all",
var == 2 ~ "Slightly",
var == 3 ~ "Moderately",
var == 4 ~ "Highly")) %>%
ct(var, wt = weight, show_na = FALSE) %>%
mutate(type = "The Natural Environment")
all <- bind_rows(c1, c2, c3)
all %>%
mutate(lab = round(pct, 2)) %>%
ggplot(., aes(x = 1, y = pct, fill = fct_rev(var))) +
geom_col(color = "black") +
coord_flip() +
facet_wrap(~ type, ncol =1, strip.position = "top") +
theme_rb() +
scale_fill_manual(values = c("#033f63", "#28666e", "#B5B682", "#FEDC97")) +
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 = "black") +
geom_text(aes(label = ifelse(var == "Moderately", paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 8, family = "font", color = "white") +
geom_text(aes(label = ifelse(var == "Highly", 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 = element_text(size = 20)) +
theme(strip.text.y.left = element_text(angle = 0, hjust = 1)) +
labs(x = "", y = "", title = "How Connected Do You Feel To the Following:", caption = "@ryanburge\nData: The National Religion and Spirituality Survey, November 2022")
save("connected_spiritual.png", wd = 9, ht = 4)
aa1 <- spirit %>%
mutate(vv = q31a) %>%
mutate(vv = frcode(vv == 5 ~ "Strongly Disagree",
vv == 4 ~ "Somewhat Disagree",
vv == 3 ~ "Neither",
vv == 2 ~ "Somewhat Agree",
vv == 1 ~ "Strongly Agree")) %>%
ct(vv, wt = weight, show_na = FALSE) %>%
mutate(type = "My Spirituality Impacts My Poltical Views")
aa2 <- spirit %>%
mutate(vv = q31e) %>%
mutate(vv = frcode(vv == 5 ~ "Strongly Disagree",
vv == 4 ~ "Somewhat Disagree",
vv == 3 ~ "Neither",
vv == 2 ~ "Somewhat Agree",
vv == 1 ~ "Strongly Agree")) %>%
ct(vv, wt = weight, show_na = FALSE) %>%
mutate(type = "My Religion Impacts My Poltical Views")
graph <- bind_rows(aa1, aa2)
graph %>%
mutate(lab = round(pct, 2)) %>%
ggplot(., aes(x = 1, y = pct, fill = fct_rev(vv))) +
geom_col(color = "black") +
coord_flip() +
facet_wrap(~ type, ncol =1, strip.position = "top") +
theme_rb() +
scale_fill_manual(values = c("#033f63", "#28666e", "#e1e1e1", "#B5B682", "#FEDC97")) +
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 = "black") +
geom_text(aes(label = ifelse(vv == "Somewhat Agree", paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 8, family = "font", color = "white") +
geom_text(aes(label = ifelse(vv == "Strongly Agree", 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 = element_text(size = 20)) +
theme(strip.text.y.left = element_text(angle = 0, hjust = 1)) +
labs(x = "", y = "", title = "The Impact of Spirituality/Religion on Politics", caption = "@ryanburge\nData: The National Religion and Spirituality Survey, November 2022")
save("rs_voting.png", wd = 9, ht = 3.5)
aa1 <- spirit %>%
mutate(vv = q31b) %>%
mutate(vv = frcode(vv == 5 ~ "Strongly Disagree",
vv == 4 ~ "Somewhat Disagree",
vv == 3 ~ "Neither",
vv == 2 ~ "Somewhat Agree",
vv == 1 ~ "Strongly Agree")) %>%
ct(vv, wt = weight, show_na = FALSE) %>%
mutate(type = "My Spirituality Impacts My Poltical Activity")
aa2 <- spirit %>%
mutate(vv = q31f) %>%
mutate(vv = frcode(vv == 5 ~ "Strongly Disagree",
vv == 4 ~ "Somewhat Disagree",
vv == 3 ~ "Neither",
vv == 2 ~ "Somewhat Agree",
vv == 1 ~ "Strongly Agree")) %>%
ct(vv, wt = weight, show_na = FALSE) %>%
mutate(type = "My Religion Impacts My Poltical Activity")
graph <- bind_rows(aa1, aa2)
graph %>%
mutate(lab = round(pct, 2)) %>%
ggplot(., aes(x = 1, y = pct, fill = fct_rev(vv))) +
geom_col(color = "black") +
coord_flip() +
facet_wrap(~ type, ncol =1, strip.position = "top") +
theme_rb() +
scale_fill_manual(values = c("#033f63", "#28666e", "#e1e1e1", "#B5B682", "#FEDC97")) +
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 = "black") +
geom_text(aes(label = ifelse(vv == "Somewhat Agree", paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 8, family = "font", color = "white") +
geom_text(aes(label = ifelse(vv == "Strongly Agree", 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 = element_text(size = 20)) +
theme(strip.text.y.left = element_text(angle = 0, hjust = 1)) +
labs(x = "", y = "", title = "The Impact of Spirituality/Religion on Politics", caption = "@ryanburge\nData: The National Religion and Spirituality Survey, November 2022")
save("rs_activity.png", wd = 9, ht = 3.5)
aa1 <- spirit %>%
mutate(vv = q31c) %>%
mutate(vv = frcode(vv == 5 ~ "Strongly Disagree",
vv == 4 ~ "Somewhat Disagree",
vv == 3 ~ "Neither",
vv == 2 ~ "Somewhat Agree",
vv == 1 ~ "Strongly Agree")) %>%
ct(vv, wt = weight, show_na = FALSE) %>%
mutate(type = "My Spirituality Impacts My Civic Engagement")
aa2 <- spirit %>%
mutate(vv = q31g) %>%
mutate(vv = frcode(vv == 5 ~ "Strongly Disagree",
vv == 4 ~ "Somewhat Disagree",
vv == 3 ~ "Neither",
vv == 2 ~ "Somewhat Agree",
vv == 1 ~ "Strongly Agree")) %>%
ct(vv, wt = weight, show_na = FALSE) %>%
mutate(type = "My Religion Impacts My Civic Engagement")
graph <- bind_rows(aa1, aa2)
graph %>%
mutate(lab = round(pct, 2)) %>%
ggplot(., aes(x = 1, y = pct, fill = fct_rev(vv))) +
geom_col(color = "black") +
coord_flip() +
facet_wrap(~ type, ncol =1, strip.position = "top") +
theme_rb() +
scale_fill_manual(values = c("#033f63", "#28666e", "#e1e1e1", "#B5B682", "#FEDC97")) +
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 = "black") +
geom_text(aes(label = ifelse(vv == "Somewhat Agree", paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 8, family = "font", color = "white") +
geom_text(aes(label = ifelse(vv == "Strongly Agree", 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 = element_text(size = 20)) +
theme(strip.text.y.left = element_text(angle = 0, hjust = 1)) +
labs(x = "", y = "", title = "The Impact of Spirituality/Religion on Civic Engagement (Volunteering/Donating to Charity)", caption = "@ryanburge\nData: The National Religion and Spirituality Survey, November 2022")
save("rs_civic.png", wd = 9, ht = 3.5)
heat <- spirit %>%
mutate(rel = q10) %>%
mutate(rel = frcode(rel == 4 ~ "Not at all",
rel == 3 ~ "Slightly",
rel == 2 ~ "Moderately",
rel == 1 ~ "Very")) %>%
mutate(spirit = q6) %>%
mutate(spirit = frcode(spirit == 4 ~ "Not at all",
spirit == 3 ~ "Slightly",
spirit == 2 ~ "Moderately",
spirit == 1 ~ "Very")) %>%
group_by(rel) %>%
ct(spirit, wt = weight, show_na = FALSE) %>%
filter(rel != "NA") %>%
mutate(pct = n/3587)
heat %>%
mutate(lab = round(pct, 3)) %>%
ggplot(., aes(x= rel, y = spirit)) +
geom_tile(aes(fill = pct), color = "black") +
scale_fill_gradient2(low = "#033f63", mid = "#e1e1e1", high = "#FEDC97", midpoint = .1) +
theme_rb() +
theme(plot.subtitle = element_text(size = 24)) +
geom_text(aes(x= rel, y = spirit, label = paste0(lab*100, '%')), size = 9, family = "font") +
labs(x= "How Religious Are You?", y = "How Spiritual Are You?", title = "The Relationship Between Religion and Spirituality", caption = "@ryanburge\nData: The National Religion and Spirituality Survey, November 2022")
save('heat_rel_spirit.png', wd = 6)
gg1 <- spirit %>%
mutate(rel = q10) %>%
mutate(rel = case_when(rel == 1 | rel == 2 ~ 1,
rel == 3 | rel == 4 ~ 0)) %>%
group_by(age) %>%
mean_ci(rel, wt = weight, ci = .84) %>%
mutate(type = "Religious")
gg2 <- spirit %>%
mutate(rel = q6) %>%
mutate(rel = case_when(rel == 1 | rel == 2 ~ 1,
rel == 3 | rel == 4 ~ 0)) %>%
group_by(age) %>%
mean_ci(rel, wt = weight, ci = .84) %>%
mutate(type = "Spiritual")
both <- bind_rows(gg1, gg2)
both %>%
filter(age <= 75) %>%
ggplot(., aes(x = age, y = mean, color = type)) +
geom_point(stroke = .25, shape = 21, alpha = .75) +
geom_labelsmooth(aes(label = type), method = "loess", formula = y ~ x, family = "font", linewidth = 1, text_smoothing = 30, size = 7, linewidth = 1, boxlinewidth = 0.3) +
scale_color_calc() +
theme_rb() +
scale_y_continuous(labels = percent, limits = c(0, .8)) +
labs(x = "Age", y = "", title = "Share Saying They Are Moderately/Very Spiritual or Religious", caption = "@ryanburge\nData: The National Religion and Spirituality Survey, November 2022")
save("spirit_rel_age.png")
heat %>%
mutate(lab = round(pct, 3)) %>%
ggplot(., aes(x= rel, y = spirit)) +
geom_tile(aes(fill = pct), color = "black") +
scale_fill_gradient2(low = "#033f63", mid = "#e1e1e1", high = "#FEDC97", midpoint = .1) +
theme_rb() +
theme(plot.subtitle = element_text(size = 24)) +
geom_text(aes(x= rel, y = spirit, label = paste0(lab*100, '%')), size = 9, family = "font") +
labs(x= "How Religious Are You?", y = "How Spiritual Are You?", title = "The Relationship Between Religion and Spirituality", caption = "@ryanburge\nData: The National Religion and Spirituality Survey, November 2022")
save('heat_rel_spirit.png', wd = 6)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment