Skip to content

Instantly share code, notes, and snippets.

@ryanburge
Created January 22, 2024 21:16
Show Gist options
  • Save ryanburge/b28354e8153a364ebcafbef4db433e98 to your computer and use it in GitHub Desktop.
Save ryanburge/b28354e8153a364ebcafbef4db433e98 to your computer and use it in GitHub Desktop.
bay <- import("E://data/baylor21.sav") %>% clean_names()
fun <- function(var, name){
bay %>%
mutate(reltrad = frcode(reltrad == 1 ~ "Evangelical",
reltrad == 2 ~ "Mainline",
reltrad == 3 ~ "Black Prot.",
reltrad == 4 ~ "Catholic",
reltrad == 5 | reltrad == 6 ~ "Other Faith",
reltrad == 7 ~ "Non-Affiliated")) %>%
mutate(vv = {{var}}) %>%
mutate(vv = frcode(vv == 1 ~ "Never",
vv == 2 ~ "Hardly Ever",
vv == 3 ~ "Some of the Time",
vv == 4 ~ "Most/All of the Time")) %>%
group_by(reltrad) %>%
ct(vv, wt = weight, show_na = FALSE) %>%
mutate(type = name)
}
var <- syms(c("q4a", "q4b", "q4c", "q4d", "q4e", "q4f", "q4g"))
name <- c("Felt Depressed", "Felt Sad", "Worried a Lot about Little Things", "Felt Tense and Anxious", "Felt Restless", "Felt Bored", "Felt Angry")
gg <- map2_df(var, name, fun, .progress = TRUE)
gg %>%
filter(reltrad != "NA") %>%
mutate(lab = round(pct, 2)) %>%
ggplot(., aes(x = fct_rev(reltrad), y = pct, fill = fct_rev(vv))) +
geom_col(color = "black") +
coord_flip() +
facet_wrap(~ type, ncol =2, 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(strip.text.x.top = element_text(size = 20)) +
# 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 = 4, family = "font", color = "black") +
geom_text(aes(label = ifelse(pct >.05 & vv == "Some of the Time", paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 4, family = "font", color = "white") +
geom_text(aes(label = ifelse(pct >.05 & vv == "Most/All of the Time", 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 = "In the Past Week, About How Often Have You Had the Following Feelings?", caption = "@ryanburge + @religiondata\nData: Baylor Religion Survey, 2021")
save("bay21_feelings.png", wd = 9, ht = 8)
gg <- bay %>%
mutate(sum = q4a + q4b + q4c + q4d + q4e + q4f + q4g) %>%
mutate(reltrad = frcode(reltrad == 1 ~ "Evangelical",
reltrad == 2 ~ "Mainline",
reltrad == 3 ~ "Black Prot.",
reltrad == 4 ~ "Catholic",
reltrad == 5 | reltrad == 6 ~ "Other Faith",
reltrad == 7 ~ "Non-Affiliated")) %>%
group_by(reltrad) %>%
ct(sum, wt = weight, show_na = FALSE) %>%
filter(reltrad != "NA") ## 15.9 is overall mean.
mean <- bay %>%
mutate(sum = q4a + q4b + q4c + q4d + q4e + q4f + q4g) %>%
mutate(reltrad = frcode(reltrad == 1 ~ "Evangelical",
reltrad == 2 ~ "Mainline",
reltrad == 3 ~ "Black Prot.",
reltrad == 4 ~ "Catholic",
reltrad == 5 | reltrad == 6 ~ "Other Faith",
reltrad == 7 ~ "Non-Affiliated")) %>%
group_by(reltrad) %>%
mean_ci(sum, wt = weight) %>%
filter(reltrad != "NA") %>%
mutate(mean = round(mean, 1)) %>%
select(reltrad, lab = mean) %>%
mutate(lab = paste("Mean:", lab))
gg %>%
ggplot(., aes(x = sum, y = pct, fill = reltrad)) +
geom_col(color = "black") +
facet_wrap(~ reltrad) +
scale_fill_manual(values = c("#033f63", "#28666e", "#e1e1e1", "#B5B682", "#FEDC97", "#5f2680")) +
theme_rb() +
scale_y_continuous(labels = percent) +
geom_text(x = 23, aes(label = lab, y = .14), data = mean, color = "black", family = "font", size = 6) +
theme(strip.text = element_text(size = 20)) +
labs(x = "", y = "", title = "Distribution of Scores of Mental Distress Battery", caption = "@ryanburge + @religiondata\nData: Baylor Religion Survey, 2021")
save("bay21_feelings_dist_reltrad.png")
gg <- bay %>%
mutate(sum = q4a + q4b + q4c + q4d + q4e + q4f + q4g) %>%
mutate(reltrad = frcode(reltrad == 1 ~ "Evangelical",
reltrad == 2 ~ "Mainline",
reltrad == 3 ~ "Black Prot.",
reltrad == 4 ~ "Catholic",
reltrad == 5 | reltrad == 6 ~ "Other Faith",
reltrad == 7 ~ "Non-Affiliated")) %>%
mutate(pid7 = q32) %>%
mutate(pid3 = frcode(pid7 == 5 | pid7 == 6 | pid7 == 7 ~ "Dem.",
pid7 == 4 ~ "Ind.",
pid7 == 1 | pid7 == 2 | pid7 == 3 ~ "Rep.")) %>%
group_by(reltrad, pid3) %>%
mean_ci(sum, wt = weight, ci = .84) %>%
filter(reltrad != "NA") %>%
filter(pid3 != "NA")
gg %>%
mutate(lab = round(mean, 1)) %>%
ggplot(., aes(x = pid3, y = mean, fill = pid3)) +
geom_col(color = "black") +
facet_wrap(~ reltrad) +
pid3_fill() +
error_bar() +
geom_text(aes(y = 2, label = lab), position = position_dodge(width = .9), size = 6, family = "font") +
theme_rb() +
theme(strip.text = element_text(size = 16)) +
labs(x = "", y = "", title = "Mean Mental Distress Score by Religion and Partisanship", caption = "@ryanburge + @religiondata\nData: Baylor Religion Survey, 2021")
save("summed_score_reltrad_pid3.png", wd = 6)
fun <- function(var, name){
bay %>%
mutate(reltrad = frcode(reltrad == 1 ~ "Evangelical",
reltrad == 2 ~ "Mainline",
reltrad == 3 ~ "Black Prot.",
reltrad == 4 ~ "Catholic",
reltrad == 5 | reltrad == 6 ~ "Other Faith",
reltrad == 7 ~ "Non-Affiliated")) %>%
mutate(vv = {{var}}) %>%
mutate(vv = frcode(vv == 1 ~ "Less Often",
vv == 2 ~ "About the Same",
vv == 3 | vv == 4 ~ "More Often")) %>%
group_by(reltrad) %>%
ct(vv, wt = weight, show_na = FALSE) %>%
mutate(type = name)
}
var <- syms(c("q14a", "q14b", "q14c", "q14d", "q14e", "q14f", "q14g", "q14g", "q14i"))
name <- c("Happy", "Sad", "Worried", "Confident", "Tense", "Relaxed", "Lonely", "Cared For", "Angry")
gg <- map2_df(var, name, fun, .progress = TRUE)
gg %>%
filter(reltrad != "NA") %>%
mutate(lab = round(pct, 2)) %>%
ggplot(., aes(x = fct_rev(reltrad), y = pct, fill = fct_rev(vv))) +
geom_col(color = "black") +
coord_flip() +
facet_wrap(~ type, ncol =2, strip.position = "top") +
theme_rb() +
scale_fill_manual(values = c("#033f63", "#e1e1e1", "#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(strip.text.x.top = element_text(size = 20)) +
# 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 = 4, family = "font", color = "black") +
geom_text(aes(label = ifelse(pct >.05 & vv == "More Often", paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 4, family = "font", color = "white") +
# geom_text(aes(label = ifelse(pct >.05 & vv == "Most/All of the Time", 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 = "During the COVID-19 pandemic, compared to your life before, how often do you feel...", caption = "@ryanburge + @religiondata\nData: Baylor Religion Survey, 2021")
save("bay21_feelings_from_covid.png", wd = 9, ht = 10)
regg <- bay %>%
mutate(sum = q4a + q4b + q4c + q4d + q4e + q4f + q4g) %>%
mutate(educ = q77a) %>%
mutate(married = case_when(q76 == 2 ~ 1, TRUE ~ 0)) %>%
mutate(white = case_when(q75_1 == 1 ~ 1, TRUE ~ 0)) %>%
mutate(male = case_when(q71 == 1 ~ 1, TRUE ~ 0)) %>%
mutate(income = q85) %>%
mutate(reltrad = frcode(reltrad == 1 ~ "Evangelical",
reltrad == 2 ~ "Mainline",
reltrad == 3 ~ "Black Prot.",
reltrad == 4 ~ "Catholic",
reltrad == 5 | reltrad == 6 ~ "Other Faith",
reltrad == 7 ~ "Non-Affiliated")) %>%
mutate(pid7 = q32) %>%
mutate(pid3 = frcode(pid7 == 5 | pid7 == 6 | pid7 == 7 ~ "Dem.",
pid7 == 4 ~ "Ind.",
pid7 == 1 | pid7 == 2 | pid7 == 3 ~ "Rep.")) %>%
mutate(age = i_age) %>%
select(sum, educ, married, white, male, income, reltrad, pid3, age)
reg1 <- lm(sum ~ pid3*reltrad + educ + married + white + male + income + age, data = regg)
library(interactions)
pp <- cat_plot(reg1, pred= reltrad, modx = pid3, int.width = .76, errorbar.width = .1, geom = "bar", geom.alpha = .8)
pp +
theme_rb(legend = TRUE) +
pid3_color() +
pid3_fill() +
theme(plot.title = element_text(size = 15)) +
labs(x = "Religious Tradition", y = "Mean Mental Distress Score", title = "Mean Score on Mental Illness Scale by Partisanship and Religious Tradition",
subtitle = "Controls for Education, Age, Gender, Income, Marital Status and Race",
caption = "@ryanburge + @religiondata\nData: Baylor Religion Survey, 2021")
save("interact_mh_baylor.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment