Skip to content

Instantly share code, notes, and snippets.

@ryanburge
Created March 24, 2024 22:22
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/3ce1dcd356dac16bf2c8c7bd2759970f to your computer and use it in GitHub Desktop.
Save ryanburge/3ce1dcd356dac16bf2c8c7bd2759970f to your computer and use it in GitHub Desktop.
df <- read.fst("E://data/anes22.fst")
df <- df %>%
mutate(
relig = VCF0129,
attend = case_when(
VCF0130 %in% 1:3 ~ 2,
VCF0130 == 4 ~ 1,
TRUE ~ 0
),
race = VCF0106,
black = if_else(race == 2, 1, 0),
prot1 = if_else(relig == 100, 1, 0),
prot2 = if_else(relig == 101, 1, 0),
evan = case_when(
black != 1 & relig %in% c(123, 114, 122, 136, 131, 135, 139) ~ 1,
prot1 == 1 & attend == 2 ~ 1,
prot2 == 1 & attend == 2 ~ 1,
TRUE ~ 0
),
mline = case_when(
relig %in% c(110, 115, 116, 111, 124, 112, 155) ~ 1,
black != 1 & relig == 120 ~ 1,
prot1 == 1 & attend == 1 ~ 1,
prot2 == 1 & attend == 1 ~ 1,
TRUE ~ 0
),
bprot = if_else(
black == 1 & relig %in% c(123, 120, 136, 131, 135, 139), 1, 0
),
cath = if_else(relig %in% c(719, 200), 1, 0),
jew = if_else(relig == 300, 1, 0),
other = if_else(relig %in% c(152, 154, 156, 720, 721, 722, 790), 1, 0),
none = if_else(relig %in% c(800, 998), 1, 0),
reltrad = case_when(
none == 1 ~ 7,
other == 1 ~ 6,
jew == 1 ~ 5,
cath == 1 ~ 4,
bprot == 1 ~ 3,
mline == 1 ~ 2,
evan == 1 ~ 1,
TRUE ~ NA_real_ # Assigns NA to reltrad for cases that do not match any condition
)
)
# Re-evaluate prot1 and prot2 conditions considering 'black'
df <- df %>%
mutate(
prot1 = if_else(black == 1, 0, prot1),
prot2 = if_else(black == 1, 0, prot2)
)
graph1 <- df %>%
cces_pid3(VCF0301) %>%
filter(reltrad == 1 & race == 1) %>%
group_by(VCF0004) %>%
ct(pid3, wt = VCF0009z, show_na = FALSE)
df <- read.fst("E://data/anes22.fst")
# Copying variables
df$relig <- df$VCF0152
df$attend <- df$VCF0130
df$race <- df$VCF0106
# Recoding attend
df$attend <- case_when(
df$attend %in% 1:3 ~ 2,
df$attend == 4 ~ 1,
TRUE ~ 0
)
# Recoding race to black
df$black <- ifelse(df$race == 2, 1, 0)
# Creating prot variables and recoding
prot_vars <- c("prot1", "prot2", "prot3", "prot4", "prot5")
relig_codes <- c("010", "020", "030", "040", "99")
for (i in 1:length(prot_vars)) {
df[[prot_vars[i]]] <- ifelse(df$relig == relig_codes[i], 1, 0)
df[[prot_vars[i]]] <- ifelse(df$black == 1, 0, df[[prot_vars[i]]])
}
# Creating and recoding evan
evan_codes <- c(100, 102, 109, 120, 122, 123, 124, 125, 126, 127, 129, 130, 131, 132, 133, 134, 135,
147, 148, 149, 160, 161, 162, 164, 166, 167, 168, 170, 180, 181, 182, 183, 184, 185,
186, 199, 200, 201, 219, 221, 222, 223, 233, 234, 250, 251, 252, 253, 254, 255, 256,
260, 261, 262, 263, 264, 268, 269, 271, 272, 275, 276, 280, 291, 292)
df$evan <- ifelse(df$relig %in% evan_codes & df$black != 1, 1, 0)
df$evan <- ifelse(!df$relig %in% evan_codes, 0, df$evan)
# Recoding for attendance and prot variables
for (prot in prot_vars) {
df$evan <- ifelse(df[[prot]] == 1 & df$attend == 2, 1, df$evan)
}
# Creating and recoding mline
mline_codes <- c(121, 249, 110, 111, 150, 155, 163, 165, 220, 229, 230, 270, 279, 281, 282, 289, 290, 293)
df$mline <- ifelse(df$relig %in% mline_codes & df$black != 1, 1, 0)
df$mline <- ifelse(!df$relig %in% mline_codes, 0, df$mline)
# Recoding for attendance and prot variables
for (prot in prot_vars) {
df$mline <- ifelse(df[[prot]] == 1 & df$attend == 1, 1, df$mline)
}
# Creating and recoding bprot
bprot_codes <- c(120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 147, 148, 149, 231, 232, 233, 234, 240, 249, 257, 258, 267)
df$bprot <- ifelse(df$relig %in% bprot_codes & df$black == 1, 1, 0)
df$bprot <- ifelse(!df$relig %in% bprot_codes, 0, df$bprot)
df <- df %>%
mutate(
cath = if_else(relig %in% c(400, 700:708, 719), 1, 0),
jew = if_else(relig %in% c(500:503, 524), 1, 0),
other = if_else(relig %in% c(300:306, 309, 720:727, 729, 750, 790, 795, 997, 998), 1, 0),
none = if_else(relig %in% c(800, 801), 1, 0),
reltrad = case_when(
none == 1 ~ 7,
other == 1 ~ 6,
jew == 1 ~ 5,
cath == 1 ~ 4,
bprot == 1 ~ 3,
mline == 1 ~ 2,
evan == 1 ~ 1,
TRUE ~ NA_real_ # This assigns NA to reltrad where none of the conditions are met
)
)
graph2 <- df %>%
cces_pid3(VCF0301) %>%
filter(reltrad == 1 & race == 1) %>%
group_by(VCF0004) %>%
ct(pid3, wt = VCF0009z, show_na = FALSE)
both <- bind_rows(graph1, graph2)
both %>%
ggplot(., aes(x = VCF0004, y = pct, fill = pid3)) +
geom_area(alpha = .6, color = "black") +
pid3_fill() +
theme_rb() +
y_pct() +
labs(x = "", y = "Survey Year", title = "Partisanship of White Evangelicals", caption = "@ryanburge\nData: American National Election Study, 1960-2020") +
add_text(x = 1962, y = .10, word = " 30%", sz = 7) +
add_text(x = 1962, y = .31, word = " 9%", sz = 7) +
add_text(x = 1962, y = .68, word = " 61%", sz = 7) +
add_text(x = 2018, y = .35, word = "78%", sz = 7) +
add_text(x = 2018, y = .78, word = "8%", sz = 7) +
add_text(x = 2018, y = .92, word = "16%", sz = 7) +
add_text(x = 1990, y = .82, word = "Democrats", sz = 7) +
add_text(x = 1990, y = .57, word = "Independents", sz = 7) +
add_text(x = 1990, y = .28, word = "Republicans", sz = 7)
save("wht_evan_pid3_new.png")
pid <- both %>%
filter(pid3 == "Republican") %>%
select(year = VCF0004, pct) %>%
mutate(type = "Rep.\nID")
pres <- all %>%
filter(vote == "Republican") %>%
select(year, pct) %>%
mutate(type = "Rep.\nVote")
final <- bind_rows(pid, pres) %>%
filter(year == 1960 | year == 1964 | year == 1968 | year == 1972 | year == 1976 | year == 1980 | year == 1984 | year == 1988 | year == 1992 | year == 1996 | year == 2000 | year == 2004 | year == 2008 | year == 2012 | year == 2016 | year == 2020)
final %>%
mutate(lab = round(pct, 2)) %>%
ggplot(., aes(x = type, y = pct, fill = type)) +
geom_col(color = "black") +
facet_wrap(~ year) +
theme_rb() +
scale_fill_calc() +
scale_y_continuous(labels = percent, limits = c(0, .95)) +
geom_text(aes(y = pct + .08, label = paste0(lab*100, '%')), position = position_dodge(width = .9), size = 8, family = "font") +
labs(x = "", y = "", title = "Partisanship and Voting Behavior Among White Evangelicals", caption = "@ryanburge\nData: American National Election Study, 1960-2020")
save("wev_anes_vote_pid3.png", wd = 6, ht = 10)
pid <- both %>%
filter(pid3 == "Republican") %>%
select(year = VCF0004, pid = pct)
pres <- all %>%
filter(vote == "Republican") %>%
select(year, vote = pct)
graph <- left_join(pid, pres) %>%
filter(vote != "NA") %>%
mutate(diff = vote - pid)
graph %>%
ggplot(., aes(x = year, y = diff)) +
geom_line() +
geom_point(stroke = 1, shape = 21, fill = "white") +
geom_smooth(se = FALSE, color = "firebrick3", linetype = "twodash") +
theme_rb() +
scale_y_continuous(labels = percent, limits = c(0, .5)) +
labs(x = "", y = "", title = "Republican Vote Share - Republican Partisanship Among White Evangelicals", caption = "@ryanburge\nData: American National Election Study, 1960-2020")
save("pid_vote_wev_trendline.png", wd = 10, ht = 6)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment