Skip to content

Instantly share code, notes, and snippets.

@ryanburge
Created February 6, 2024 20:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanburge/bb5e5ed4ec858d17bf14e155857bb8ae to your computer and use it in GitHub Desktop.
Save ryanburge/bb5e5ed4ec858d17bf14e155857bb8ae to your computer and use it in GitHub Desktop.
library(rio)
library(janitor)
## Full Sample ####
bay21 <- import("E://data/baylor21.sav") %>% clean_names()
fun <- function(var, name){
bay21 %>%
mutate(vv = {{var}}) %>%
mutate(vv = frcode(vv == 1 ~ "Strongly Disagree",
vv == 2 ~ "Somewhat Disagree",
vv == 3 ~ "Undecided",
vv == 4 ~ "Somewhat Agree",
vv == 5 ~ "Strongly Agree")) %>%
ct(vv, wt = weight, show_na = FALSE) %>%
mutate(type = name)
}
var <- syms(c("q35a", "q35b", "q35c", "q35d", "q35e", "q35f"))
name <- c("The federal government should declare the United States a Christian nation", "The federal government should advocate Christian values", "The federal government should enforce strict separation of church and state",
"The federal government should allow the display of religious symbols in public spaces", "The success of the United States is part of God's plan", "The federal government should allow prayer in public schools")
gg1 <- map2_df(var, name, fun, .progress = TRUE) %>%
mutate(year = "2021")
bay07 <- import("E://data/baylor2007.DTA") %>% clean_names()
fun <- function(var, name){
bay07 %>%
mutate(vv = {{var}}) %>%
mutate(vv = frcode(vv == 1 ~ "Strongly Disagree",
vv == 2 ~ "Somewhat Disagree",
vv == 8 ~ "Undecided",
vv == 3 ~ "Somewhat Agree",
vv == 4 ~ "Strongly Agree")) %>%
ct(vv, wt = weight, show_na = FALSE) %>%
mutate(type = name)
}
var <- syms(c("chrnatn", "advochr", "separat", "disprel", "godsplan", "prayschl"))
name <- c("The federal government should declare the United States a Christian nation", "The federal government should advocate Christian values", "The federal government should enforce strict separation of church and state",
"The federal government should allow the display of religious symbols in public spaces", "The success of the United States is part of God's plan", "The federal government should allow prayer in public schools")
gg2 <- map2_df(var, name, fun, .progress = TRUE) %>%
mutate(year = "2007")
both <- bind_rows(gg1, gg2)
both %>%
mutate(lab = round(pct, 2)) %>%
ggplot(., aes(x = fct_rev(year), 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", "#5f2680")) +
theme(legend.position = "bottom") +
scale_y_continuous(labels = percent) +
theme(strip.text.y.left = element_text(angle=0)) +
guides(fill = guide_legend(reverse=T, ncol = 1)) +
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 = 6, family = "font", color = "black") +
geom_text(aes(label = ifelse(pct >.05 & vv == "Strongly Agree", paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 6, family = "font", color = "white") +
geom_text(aes(label = ifelse(pct >.05 & vv == "Somewhat Agree", paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 6, family = "font", color = "white") +
theme(plot.title = element_text(size = 16)) +
theme(strip.text.y.left = element_text(angle = 0, hjust = 1)) +
theme(strip.text.x.top = element_text(size = 18)) +
guides(fill = guide_legend(reverse=T, ncol = 5)) + # Adjust 'ncol' to the number of your legend items or as needed for a horizontal layout
labs(x = "", y = "", title = "Christian Nationalism Questions in 2007 and 2021", caption = "@ryanburge + @religiondata\nData: Baylor Religion Survey, Waves II and VI")
save("xtn_national_baylor.png", wd = 9, ht = 8)
## No NONES ####
bay21 <- import("E://data/baylor21.sav") %>% clean_names()
fun <- function(var, name){
bay21 %>%
filter(reltrad != 7) %>%
mutate(vv = {{var}}) %>%
mutate(vv = frcode(vv == 1 ~ "Strongly Disagree",
vv == 2 ~ "Somewhat Disagree",
vv == 3 ~ "Undecided",
vv == 4 ~ "Somewhat Agree",
vv == 5 ~ "Strongly Agree")) %>%
ct(vv, wt = weight, show_na = FALSE) %>%
mutate(type = name)
}
var <- syms(c("q35a", "q35b", "q35c", "q35d", "q35e", "q35f"))
name <- c("The federal government should declare the United States a Christian nation", "The federal government should advocate Christian values", "The federal government should enforce strict separation of church and state",
"The federal government should allow the display of religious symbols in public spaces", "The success of the United States is part of God's plan", "The federal government should allow prayer in public schools")
gg1 <- map2_df(var, name, fun, .progress = TRUE) %>%
mutate(year = "2021")
bay07 <- import("E://data/baylor2007.DTA") %>% clean_names()
fun <- function(var, name){
bay07 %>%
filter(reltrad != 7) %>%
mutate(vv = {{var}}) %>%
mutate(vv = frcode(vv == 1 ~ "Strongly Disagree",
vv == 2 ~ "Somewhat Disagree",
vv == 8 ~ "Undecided",
vv == 3 ~ "Somewhat Agree",
vv == 4 ~ "Strongly Agree")) %>%
ct(vv, wt = weight, show_na = FALSE) %>%
mutate(type = name)
}
var <- syms(c("chrnatn", "advochr", "separat", "disprel", "godsplan", "prayschl"))
name <- c("The federal government should declare the United States a Christian nation", "The federal government should advocate Christian values", "The federal government should enforce strict separation of church and state",
"The federal government should allow the display of religious symbols in public spaces", "The success of the United States is part of God's plan", "The federal government should allow prayer in public schools")
gg2 <- map2_df(var, name, fun, .progress = TRUE) %>%
mutate(year = "2007")
both <- bind_rows(gg1, gg2)
both %>%
mutate(lab = round(pct, 2)) %>%
ggplot(., aes(x = fct_rev(year), 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", "#5f2680")) +
theme(legend.position = "bottom") +
scale_y_continuous(labels = percent) +
theme(strip.text.y.left = element_text(angle=0)) +
guides(fill = guide_legend(reverse=T, ncol = 1)) +
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 = 6, family = "font", color = "black") +
geom_text(aes(label = ifelse(pct >.05 & vv == "Strongly Agree", paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 6, family = "font", color = "white") +
geom_text(aes(label = ifelse(pct >.05 & vv == "Somewhat Agree", paste0(lab*100, '%'), '')), position = position_stack(vjust = 0.5), size = 6, family = "font", color = "white") +
theme(plot.title = element_text(size = 16)) +
theme(strip.text.y.left = element_text(angle = 0, hjust = 1)) +
theme(strip.text.x.top = element_text(size = 18)) +
guides(fill = guide_legend(reverse=T, ncol = 5)) + # Adjust 'ncol' to the number of your legend items or as needed for a horizontal layout
labs(x = "", y = "", title = "Christian Nationalism Questions in 2007 and 2021 - Nones Removed", caption = "@ryanburge + @religiondata\nData: Baylor Religion Survey, Waves II and VI")
save("xtn_national_baylor_nones_gone.png", wd = 9, ht = 8)
gss_reltrad6 <- function(df, var){
var <- enquo(var)
df %>%
# ungroup(!! var) %>%
mutate(reltrad = frcode(!! var == 1 ~ "Evan.",
!! var == 2 ~ "Mainline",
!! var == 3 ~ "Black\nProt.",
!! var == 4 ~ "Catholic",
!! var == 5 | !! var == 6 ~ "Other\nFaith",
!! var == 7 ~ "None"))
}
fun <- function(var, name){
bay21 %>%
gss_reltrad6(reltrad) %>%
mutate(vv = {{var}}) %>%
mutate(vv = case_when(vv == 4 | vv == 5 ~ 1,
vv == 3 | vv == 2 | vv == 1 ~ 0)) %>%
group_by(reltrad) %>%
mean_ci(vv, wt = weight, ci = .84) %>%
mutate(type = name)
}
var <- syms(c("q35a", "q35b", "q35c", "q35d", "q35e", "q35f"))
name <- c("The federal government should declare the United States a Christian nation", "The federal government should advocate Christian values", "The federal government should enforce strict separation of church and state",
"The federal government should allow the display of religious symbols in public spaces", "The success of the United States is part of God's plan", "The federal government should allow prayer in public schools")
gg2 <- map2_df(var, name, fun, .progress = TRUE) %>%
mutate(year = "2021")
fun <- function(var, name){
bay07 %>%
gss_reltrad6(reltrad) %>%
mutate(vv = {{var}}) %>%
mutate(vv = case_when(vv == 3 | vv == 4 ~ 1,
vv == 1 | vv == 2 | vv == 8 ~ 0)) %>%
group_by(reltrad) %>%
mean_ci(vv, wt = weight, ci = .84) %>%
mutate(type = name)
}
var <- syms(c("chrnatn", "advochr", "separat", "disprel", "godsplan", "prayschl"))
name <- c("The federal government should declare the United States a Christian nation", "The federal government should advocate Christian values", "The federal government should enforce strict separation of church and state",
"The federal government should allow the display of religious symbols in public spaces", "The success of the United States is part of God's plan", "The federal government should allow prayer in public schools")
gg1 <- map2_df(var, name, fun, .progress = TRUE) %>%
mutate(year = "2007")
both <- bind_rows(gg1, gg2) %>% filter(reltrad != "NA")
both %>%
mutate(lab = round(mean, 2)) %>%
ggplot(., aes(x = reltrad, y = mean, fill = year)) +
geom_col(color = "black", position = "dodge") +
theme_rb(legend = TRUE) +
scale_fill_manual(values = c("#033F63", "#FEDC98")) +
facet_wrap(~type, , nrow = 3, labeller = labeller(type = label_wrap_gen(width = 55))) +
lab_bar(top = FALSE, type = lab, pos = .06, sz = 5) +
geom_text(aes(y = .06, label = ifelse(year == "2007", paste0(lab*100, '%'), "")), position = position_dodge(width = .9), size = 5, family = "font", color = "white") +
error_bar() +
y_pct() +
labs(x = "", y = "", title = "Share Agreeing to Each Statement in the Christian Nationalism Scale", caption = "@ryanburge + @religiondata\nData: Baylor Religion Survey, Waves II and VI")
save("cn_reltrad_baylor.png", wd = 10, ht = 10)
gss_reltrad6 <- function(df, var){
var <- enquo(var)
df %>%
# ungroup(!! var) %>%
mutate(reltrad = frcode(!! var == 1 ~ "Evangelical",
!! var == 2 ~ "Mainline",
!! var == 3 ~ "Black Protestant",
!! var == 4 ~ "Catholic",
!! var == 5 | !! var == 6 ~ "Other Faith",
!! var == 7 ~ "No Religion"))
}
rfun <- function(df, var1, var2) {
df %>%
mutate(vv = {{ var1 }}) %>%
mutate(!!var2 := case_when(vv == 5 ~ 5,
vv == 4 ~ 4,
vv == 3 ~ 3,
vv == 2 ~ 2,
vv == 1 ~ 1))
}
rfun_rev <- function(df, var1, var2) {
df %>%
mutate(vv = {{ var1 }}) %>%
mutate(!!var2 := case_when(vv == 5 ~ 1,
vv == 4 ~ 2,
vv == 3 ~ 3,
vv == 2 ~ 4,
vv == 1 ~ 5))
}
g21 <- bay21 %>%
gss_reltrad6(reltrad) %>%
rfun(q35a, "cn1") %>%
rfun(q35b, "cn2") %>%
rfun_rev(q35c, "cn3") %>%
rfun(q35d, "cn4") %>%
rfun(q35e, "cn5") %>%
rfun(q35a, "cn6") %>%
mutate(cn = cn1 + cn2 + cn3 + cn4 + cn5 + cn6) %>%
mutate(cn = cn - 6) %>%
group_by(reltrad) %>%
mean_ci(cn, wt = weight, ci = .84) %>%
filter(reltrad != "NA") %>%
mutate(year = "2021")
rfun <- function(df, var1, var2) {
df %>%
mutate(vv = {{ var1 }}) %>%
mutate(!!var2 := case_when(vv == 4 ~ 5,
vv == 3 ~ 4,
vv == 8 ~ 3,
vv == 2 ~ 2,
vv == 1 ~ 1))
}
rfun_rev <- function(df, var1, var2) {
df %>%
mutate(vv = {{ var1 }}) %>%
mutate(!!var2 := case_when(vv == 4 ~ 1,
vv == 3 ~ 2,
vv == 8 ~ 3,
vv == 2 ~ 4,
vv == 1 ~ 5))
}
g07 <- bay07 %>%
gss_reltrad6(reltrad) %>%
rfun(chrnatn, "cn1") %>%
rfun(advochr, "cn2") %>%
rfun_rev(separat, "cn3") %>%
rfun(disprel, "cn4") %>%
rfun(godsplan, "cn5") %>%
rfun(prayschl, "cn6") %>%
mutate(cn = cn1 + cn2 + cn3 + cn4 + cn5 + cn6) %>%
mutate(cn = cn - 6) %>%
group_by(reltrad) %>%
mean_ci(cn, wt = weight, ci = .84) %>%
filter(reltrad != "NA") %>%
mutate(year = "2007")
both <- bind_rows(g07, g21)
both %>%
ggplot(., aes(x = reorder(reltrad, mean), y = mean, color = year)) +
geom_errorbar(aes(ymin=lower, ymax=upper), width=0, linewidth = 1) +
geom_point(stroke = 1, shape = 21, fill = "white", size = 4) +
coord_flip() +
theme_rb(legend = TRUE) +
scale_color_manual(values = c("#033F63", "#FEDC98")) +
labs(x = "", y = "", title = "Mean Score on Christian Nationalism Scale - Max is 24", caption = "@ryanburge + @religiondata\nData: Baylor Religion Survey, Waves II and VI")
save("cn_mean_compare_reltrad.png", ht = 4)
g21 <- bay21 %>%
gss_reltrad6(reltrad) %>%
rfun(q35a, "cn1") %>%
rfun(q35b, "cn2") %>%
rfun(q35c, "cn3") %>%
rfun(q35d, "cn4") %>%
rfun(q35e, "cn5") %>%
rfun(q35a, "cn6") %>%
mutate(cn = cn1 + cn2 + cn3 + cn4 + cn5 + cn6) %>%
mutate(cn = cn - 6) %>%
group_by(reltrad) %>%
mean_ci(cn, wt = weight, ci = .84) %>%
filter(reltrad != "NA") %>%
mutate(year = "2021")
bay_pid3 <- function(df, var){
var <- enquo(var)
df %>%
mutate(pid3 = frcode(!! var == 5 | !! var == 6 | !! var == 7 ~ "Dem.",
!! var == 4 ~ "Ind.",
!! var == 1 | !! var == 2 | !! var == 3 ~ "Rep."))
}
fun <- function(var, name){
bay21 %>%
bay_pid3(q32) %>%
mutate(vv = {{var}}) %>%
mutate(vv = case_when(vv == 4 | vv == 5 ~ 1,
vv == 3 | vv == 2 | vv == 1 ~ 0)) %>%
group_by(pid3) %>%
mean_ci(vv, ci = .84) %>%
mutate(type = name)
}
var <- syms(c("q35a", "q35b", "q35c", "q35d", "q35e", "q35f"))
name <- c("The federal government should declare the United States a Christian nation", "The federal government should advocate Christian values", "The federal government should enforce strict separation of church and state",
"The federal government should allow the display of religious symbols in public spaces", "The success of the United States is part of God's plan", "The federal government should allow prayer in public schools")
gg2 <- map2_df(var, name, fun, .progress = TRUE) %>%
mutate(year = "2021") %>%
filter(pid3 != "NA")
bay_pid3 <- function(df, var){
var <- enquo(var)
df %>%
mutate(pid3 = frcode(!! var == 10 | !! var == 11 | !! var == 12 ~ "Dem.",
!! var == 9 ~ "Ind.",
!! var == 6 | !! var == 7 | !! var == 8 ~ "Rep."))
}
fun <- function(var, name){
bay07 %>%
bay_pid3(partyid) %>%
mutate(vv = {{var}}) %>%
mutate(vv = case_when(vv == 3 | vv == 4 ~ 1,
vv == 1 | vv == 2 | vv == 8 ~ 0)) %>%
group_by(pid3) %>%
mean_ci(vv, wt = weight, ci = .84) %>%
mutate(type = name)
}
var <- syms(c("chrnatn", "advochr", "separat", "disprel", "godsplan", "prayschl"))
name <- c("The federal government should declare the United States a Christian nation", "The federal government should advocate Christian values", "The federal government should enforce strict separation of church and state",
"The federal government should allow the display of religious symbols in public spaces", "The success of the United States is part of God's plan", "The federal government should allow prayer in public schools")
gg1 <- map2_df(var, name, fun, .progress = TRUE) %>%
mutate(year = "2007")
both <- bind_rows(gg1, gg2) %>% filter(pid3 != "NA")
both %>%
mutate(lab = round(mean, 2)) %>%
ggplot(., aes(x = pid3, y = mean, fill = year)) +
geom_col(color = "black", position = "dodge") +
theme_rb(legend = TRUE) +
scale_fill_manual(values = c("#033F63", "#FEDC98")) +
facet_wrap(~type, , nrow = 3, labeller = labeller(type = label_wrap_gen(width = 55))) +
lab_bar(top = FALSE, type = lab, pos = .07, sz = 7) +
geom_text(aes(y = .07, label = ifelse(year == "2007", paste0(lab*100, '%'), "")), position = position_dodge(width = .9), size = 7, family = "font", color = "white") +
error_bar() +
y_pct() +
theme(axis.text.x = element_text(size = 20)) +
labs(x = "", y = "", title = "Share Agreeing to Each Statement in the Christian Nationalism Scale", caption = "@ryanburge + @religiondata\nData: Baylor Religion Survey, Waves II and VI")
save("cn_pid3_baylor.png", wd = 8, ht = 10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment