Skip to content

Instantly share code, notes, and snippets.

@ryanburge
Created March 11, 2025 18:50
Show Gist options
  • Select an option

  • Save ryanburge/933200bb468b50e56d816dd59893271f to your computer and use it in GitHub Desktop.

Select an option

Save ryanburge/933200bb468b50e56d816dd59893271f to your computer and use it in GitHub Desktop.
library(rio)
library(janitor)
st <- import("E://data/springtide.sav") %>%
clean_names()
# Select and rename relevant variables
flourish_long <- st %>%
select(id, starts_with("flourish_")) %>%
pivot_longer(cols = -id,
names_to = "Question",
values_to = "Response") %>%
filter(!is.na(Response)) %>% # Remove missing values
mutate(
Response = factor(Response, levels = c(1, 2, 3, 4, 5),
labels = c("Strongly Disagree", "Disagree", "Neither", "Agree", "Strongly Agree")),
Question = factor(Question,
levels = c("flourish_purpose", "flourish_social", "flourish_engage",
"flourish_others", "flourish_capable", "flourish_good",
"flourish_future", "flourish_respect"),
labels = c("I Live a Purposeful & Meaningful Life", "My social relationships are supportive and rewarding", "I am engaged and interested in my daily activities",
"I actively contribute to the happiness and well-being of others", "I am competent and capable in the activities that are important to me", "I am a good person and live a good life",
"I am optimistic about my future", "People respect me"))
)
flourish_summary <- flourish_long %>%
group_by(Question, Response) %>%
summarise(n = n(), .groups = "drop") %>%
group_by(Question) %>%
mutate(pct = n / sum(n))
flourish_summary %>%
mutate(lab = round(pct, 2)) %>%
ggplot(., aes(x = 1, y = pct, fill = fct_rev(Response))) +
geom_col(color = "black") +
coord_flip() +
facet_wrap(~ Question, ncol =1, strip.position = "top") +
theme_rb() +
scale_fill_manual(values = c("#d73027", "#fc8d59", "#fee08b", "#91bfdb", "#4575b4")) + # Custom color palette
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(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 = "Human Flourishing Questions Among 13-25 Year Olds", caption = "@ryanburge + @religiondata\nData: Springtide Survey of Young People and Civic Life, 2024")
save("springtide_flourishing_questions.png", ht = 8)
# Process the data: Count flourishing statements agreed to
flourish_counts <- st %>%
select(id, r_religious, starts_with("flourish_")) %>%
pivot_longer(cols = starts_with("flourish_"), names_to = "Flourish_Question", values_to = "Response") %>%
filter(!is.na(Response)) %>% # Remove missing responses
mutate(Agreed = ifelse(Response >= 4, 1, 0)) %>% # 1 if Agree (4) or Strongly Agree (5), else 0
group_by(id, r_religious) %>%
summarise(Num_Agreed = sum(Agreed), .groups = "drop") %>%
filter(!is.na(r_religious)) # Remove missing religiosity values
# Compute mean agreement count per religiosity level
rel_summary <- flourish_counts %>%
group_by(r_religious) %>%
mean_ci(Num_Agreed, ci = .84) %>%
mutate(Religiosity = factor(r_religious, levels = 1:4,
labels = c("Not", "Slightly",
"Moderately", "Very"))) %>%
mutate(type = "How Religious Are You?")
# Process the data: Count flourishing statements agreed to
flourish_counts <- st %>%
select(id, r_spiritual, starts_with("flourish_")) %>%
pivot_longer(cols = starts_with("flourish_"), names_to = "Flourish_Question", values_to = "Response") %>%
filter(!is.na(Response)) %>% # Remove missing responses
mutate(Agreed = ifelse(Response >= 4, 1, 0)) %>% # 1 if Agree (4) or Strongly Agree (5), else 0
group_by(id, r_spiritual) %>%
summarise(Num_Agreed = sum(Agreed), .groups = "drop") %>%
filter(!is.na(r_spiritual)) # Remove missing religiosity values
# Compute mean agreement count per religiosity level
sp_summary <- flourish_counts %>%
group_by(r_spiritual) %>%
mean_ci(Num_Agreed, ci = .84) %>%
mutate(Religiosity = factor(r_spiritual, levels = 1:4,
labels = c("Not", "Slightly",
"Moderately", "Very"))) %>%
mutate(type = "How Spiritual Are You?")
gg <- bind_rows(rel_summary, sp_summary) %>%
select(relig = Religiosity, mean, lower, upper, type)
gg %>%
ggplot(., aes(x = relig, y = mean, fill = relig)) +
geom_col(color = 'black') +
facet_wrap(~ type) +
theme_rb() +
error_bar() +
scale_fill_manual(values = c(
"Not" = "#d73027", # Dark Red
"Slightly" = "#fc8d59", # Orange
"Moderately" = "#fee08b", # Yellow
"Very" = "#91bfdb" # Light Blue
)) +
geom_text(aes(y = .5, label = round(mean, 2)), position = position_dodge(width = .9), size = 11, family = "font") +
labs(x = "", y = "", title = "Does Religion and/or Spirituality Lead to More Human Flourishing?",
subtitle = "Number of Flourishing Items Agreed To",
caption = "@ryanburge + @religiondata\nData: Springtide Survey of Young People and Civic Life, 2024")
save("flourishing_spirit_rel.png")
# Compute the mean flourishing agreement for each religiosity-spirituality combination
flourish_heatmap <- st %>%
select(id, r_religious, r_spiritual, starts_with("flourish_")) %>%
pivot_longer(cols = starts_with("flourish_"), names_to = "Flourish_Question", values_to = "Response") %>%
filter(!is.na(Response)) %>% # Remove missing responses
mutate(Agreed = ifelse(Response >= 4, 1, 0)) %>% # 1 if Agree (4 or 5), else 0
group_by(id, r_religious, r_spiritual) %>%
summarise(Num_Agreed = sum(Agreed), .groups = "drop") %>%
filter(!is.na(r_religious), !is.na(r_spiritual)) %>% # Remove missing values
group_by(r_religious, r_spiritual) %>%
summarise(Avg_Agreed = mean(Num_Agreed), .groups = "drop") %>%
mutate(
Religiosity = factor(r_religious, levels = 1:4, labels = c("Not", "Slightly", "Moderately", "Very")),
Spirituality = factor(r_spiritual, levels = 1:4, labels = c("Not", "Slightly", "Moderately", "Very"))
)
# Create the heatmap
ggplot(flourish_heatmap, aes(x = Religiosity, y = Spirituality, fill = Avg_Agreed)) +
geom_tile(color = "black") + # Creates the heatmap squares
geom_text(aes(label = round(Avg_Agreed, 2),
color = ifelse(Avg_Agreed > 6.25, "white", "black")),
size = 9, family = "font") + # Adjusts text color conditionally
scale_fill_gradient(low = "#fee08b", high = "#d73027") + # Gradient from yellow to red
scale_color_identity() + # Ensures color mapping is applied
labs(x = "How Religious Are You?", y = "How Spiritual Are You?",
title = "Flourishing by Religious and Spiritual Identification",
subtitle = "Average Number of Flourishing Statements Agreed To",
fill = "Avg. Agreement",
caption = "@ryanburge + @religiondata\nData: Springtide Survey of Young People and Civic Life, 2024") +
theme_rb() +
theme(panel.grid = element_blank())
# Save the plot
save("flourishing_heatmap.png", wd = 6)
# Load necessary libraries
library(tidyverse)
# Function to calculate flourishing agreement by a given grouping variable
process_flourish_growth <- function(data, group_var, group_labels) {
data %>%
select(id, !!sym(group_var), starts_with("flourish_")) %>%
pivot_longer(cols = starts_with("flourish_"), names_to = "Flourish_Question", values_to = "Response") %>%
filter(!is.na(Response)) %>% # Remove missing responses
mutate(Agreed = ifelse(Response >= 4, 1, 0)) %>% # 1 if Agree (4 or 5), else 0
group_by(id, !!sym(group_var)) %>%
summarise(Num_Agreed = sum(Agreed), .groups = "drop") %>%
filter(!is.na(!!sym(group_var))) %>% # Remove missing values
group_by(!!sym(group_var)) %>%
summarise(Avg_Agreed = mean(Num_Agreed), .groups = "drop") %>%
mutate(
Category = factor(!!sym(group_var), levels = c(-1, 0, 1), labels = group_labels),
Type = ifelse(group_var == "relig_growth", "Change in Religiousness Over Prior Year", "Change in Spirituality Over Prior Year")
)
}
# Process flourishing agreement for religious and spiritual growth
flourish_relig_growth <- process_flourish_growth(st, "relig_growth",
c("Less", "No Change", "More"))
flourish_spirit_growth <- process_flourish_growth(st, "spirit_growth",
c("Less", "No Change", "More"))
# Combine both datasets
flourish_combined_growth <- bind_rows(flourish_relig_growth, flourish_spirit_growth) %>% select(Avg_Agreed, Category, Type)
# Load necessary libraries
library(tidyverse)
# Load necessary libraries
library(tidyverse)
# Create the visualization
ggplot(flourish_combined_growth, aes(x = Category, y = Avg_Agreed, fill = Category)) +
geom_col(color = "black") +
geom_text(aes(label = round(Avg_Agreed, 2)), hjust = -0.3, size = 8, family = "font") + # Add labels inside bars
scale_fill_manual(values = c(
"Less" = "#d73027", # Red-Orange
"No Change" = "#fdae61", # Warm Yellow-Orange
"More" = "#4575b4" # Deep Blue
)) +
labs(x = "", y = "Avg. # of Flourishing Statements Agreed To",
title = "Human Flourishing by Change in Religiousness & Spirituality",
subtitle = "Number of Flourishing Items Agreed To by Self-Reported Change",
caption = "@ryanburge + @religiondata\nData: Springtide Survey of Young People and Civic Life, 2024") +
facet_wrap(Type ~ ., ncol = 1) + # Facet vertically instead of horizontally
coord_flip() +
scale_y_continuous(limits = c(0, 7.25)) +
theme_rb(legend = FALSE)
# Save the plot
save("flourishing_growth_vertical.png")
# Load necessary libraries
library(tidyverse)
# Compute the mean flourishing agreement for each religious-spiritual growth combination
flourish_heatmap <- st %>%
select(id, relig_growth, spirit_growth, starts_with("flourish_")) %>%
pivot_longer(cols = starts_with("flourish_"), names_to = "Flourish_Question", values_to = "Response") %>%
filter(!is.na(Response)) %>% # Remove missing responses
mutate(Agreed = ifelse(Response >= 4, 1, 0)) %>% # 1 if Agree (4 or 5), else 0
group_by(id, relig_growth, spirit_growth) %>%
summarise(Num_Agreed = sum(Agreed), .groups = "drop") %>%
filter(!is.na(relig_growth), !is.na(spirit_growth)) %>% # Remove missing values
group_by(relig_growth, spirit_growth) %>%
summarise(Avg_Agreed = mean(Num_Agreed), .groups = "drop") %>%
mutate(
Relig_Growth = factor(relig_growth, levels = c(-1, 0, 1),
labels = c("Less Religious", "No Change", "More Religious")),
Spirit_Growth = factor(spirit_growth, levels = c(-1, 0, 1),
labels = c("Less Spiritual", "No Change", "More Spiritual"))
)
# Create the heatmap
ggplot(flourish_heatmap, aes(x = Relig_Growth, y = Spirit_Growth, fill = Avg_Agreed)) +
geom_tile(color = "black") + # Creates the heatmap squares
geom_text(aes(label = round(Avg_Agreed, 2),
color = ifelse(Avg_Agreed > 6.25, "white", "black")),
size = 13, family = "font") + # Adjusts text color based on value
scale_fill_gradient(low = "#fee08b", high = "#d73027") + # Gradient from yellow to red
scale_color_identity() + # Ensures color mapping is applied
labs(x = "Change in Religiousness", y = "Change in Spirituality",
title = "Flourishing by Change in Religiousness & Spirituality",
subtitle = "Average Number of Flourishing Statements Agreed To",
fill = "Avg. Agreement",
caption = "@ryanburge + @religiondata\nData: Springtide Survey of Young People and Civic Life, 2024") +
theme_rb() +
theme(panel.grid = element_blank())
# Save the plot
save("flourishing_9square_heatmap.png", wd = 6)
# Load necessary libraries
library(tidyverse)
# Prepare the dataset
flourish_reg_data <- st %>%
select(id, starts_with("flourish_"), r_religious, r_spiritual, h_income, r_community, i_race, i_gender, r_age) %>%
pivot_longer(cols = starts_with("flourish_"), names_to = "Flourish_Question", values_to = "Response") %>%
filter(!is.na(Response)) %>% # Remove missing values
mutate(Agreed = ifelse(Response >= 4, 1, 0)) %>% # Convert to agreement (1 = Agree/Strongly Agree, 0 = Otherwise)
group_by(id, r_religious, r_spiritual, h_income, r_community, i_race, i_gender, r_age) %>%
summarise(Num_Agreed = sum(Agreed), .groups = "drop") %>% # Sum of agreements per respondent
mutate(age = r_age,
male = ifelse(i_gender == 2, 1, 0), # Dichotomous variable for Male
white = ifelse(i_race == 1, 1, 0), # Dichotomous variable for White
h_income = ifelse(h_income == 999, NA, h_income)) %>%
drop_na() # Remove missing values
# Run the regression model
flourish_model <- lm(Num_Agreed ~ r_religious + r_spiritual + h_income + age + white + male, data = flourish_reg_data)
# Display model summary
summary(flourish_model)
# Load necessary libraries
library(tidyverse)
library(broom)
# Extract regression coefficients and confidence intervals
coefs <- tidy(flourish_model, conf.int = TRUE) %>%
filter(term != "(Intercept)") %>% # Remove intercept for cleaner plot
mutate(term = dplyr::recode(term,
"r_religious" = "Religiosity",
"r_spiritual" = "Spirituality",
"h_income" = "Household Income",
"age" = "Age",
"white" = "White",
"male" = "Male"
))
# Create coefficient plot
ggplot(coefs, aes(x = reorder(term, estimate), y = estimate, ymin = conf.low, ymax = conf.high)) +
geom_pointrange(color = "#1f78b4", stroke = 1, shape = 21, fill = "white") + # Blue points and confidence intervals
geom_hline(yintercept = 0, linetype = "dashed", color = "red") + # Reference line at 0
coord_flip() + # Flip for readability
labs(
x = "",
y = "Coefficient Estimate",
title = "Regression Coefficients for Flourishing Model",
subtitle = "95% Confidence Intervals Shown",
caption = "@ryanburge + @religiondata\nData: Springtide Survey of Young People and Civic Life, 2024"
) +
theme_rb()
# Save the plot
save("flourishing_coefficients.png", ht = 3.5)
# Load necessary libraries
library(tidyverse)
# Compute average flourishing score by age
flourish_by_age <- st %>%
select(id, r_age, starts_with("flourish_")) %>%
pivot_longer(cols = starts_with("flourish_"), names_to = "Flourish_Question", values_to = "Response") %>%
filter(!is.na(Response)) %>% # Remove missing responses
mutate(Agreed = ifelse(Response >= 4, 1, 0)) %>% # 1 if Agree (4 or 5), else 0
group_by(id, r_age) %>%
summarise(Num_Agreed = sum(Agreed), .groups = "drop") %>%
group_by(r_age) %>%
mean_ci(Num_Agreed, ci = .84)
flourish_by_age %>%
ggplot(aes(x = r_age, y = mean, fill = mean)) +
geom_col(color = 'black') +
theme_rb() +
error_bar() +
scale_fill_gradientn(colors = c("#d73027", "#fee08b", "#1a9641")) + # Green → Yellow → Red
geom_text(aes(y = .5, label = round(mean, 2)), position = position_dodge(width = .9), size = 6, family = "font") +
geom_vline(xintercept = 17.5, linetype = "dashed", color = "black", linewidth = .65) + # Vertical dashed line between 17 and 18
labs(x = "Age of Respondent", y = "", title = "Human Flourishing Score by Age of Respondent",
subtitle = "Number of Flourishing Items Agreed To",
caption = "@ryanburge + @religiondata\nData: Springtide Survey of Young People and Civic Life, 2024")
# Save the plot
save("flourishing_age_compared.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment