Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Immigration or Abortion
calc_fun <- function(df, var, type){
df %>%
mutate(nn = car::recode({{var}}, "1=5; 2=4; 3=3; 4=2; 5=1; else=99")) %>%
mutate(age = 2016 - birthyr) %>%
mutate(age2 = frcode(age >= 18 & age <= 35 ~ "18-35",
age >= 36 & age <= 44 ~ "36-44",
age >= 45 & age <= 54 ~ "45-54",
age >= 55 & age <= 64 ~ "55-64",
age >= 65 & age <= 74 ~ "65-74",
age >= 75 ~ "75 and Older")) %>%
filter(race ==1) %>%
filter(evangelical ==1) %>%
filter(nn != 99) %>%
group_by(age2) %>%
mean_ci(nn) %>%
mutate(issue = type)
}
ttt1 <- cces16 %>% calc_fun(CC16_301a, "Gun Control")
ttt2 <- cces16 %>% calc_fun(CC16_301b, "Abortion")
ttt3 <- cces16 %>% calc_fun(CC16_301c, "Taxes")
ttt4 <- cces16 %>% calc_fun(CC16_301d, "Immigration")
ttt5 <- cces16 %>% calc_fun(CC16_301e, "Deficit")
ttt6 <- cces16 %>% calc_fun(CC16_301f, "Defense")
ttt7 <- cces16 %>% calc_fun(CC16_301g, "Soc. Sec.")
ttt8 <- cces16 %>% calc_fun(CC16_301h, "Environment")
ttt9 <- cces16 %>% calc_fun(CC16_301i, "Jobs")
ttt10 <- cces16 %>% calc_fun(CC16_301j, "Crime")
ttt11 <- cces16 %>% calc_fun(CC16_301k, "Natl. Sec.")
ttt12 <- cces16 %>% calc_fun(CC16_301l, "Race Rel.")
ttt13 <- cces16 %>% calc_fun(CC16_301m, "Health Care")
ttt14 <- cces16 %>% calc_fun(CC16_301n, "Gay Marriage")
ttt15 <- cces16 %>% calc_fun(CC16_301o, "Corruption")
all <- bind_df("ttt") %>%
arrange(age2, -mean) %>%
mutate(rank = rep(1:15, times = 6))
rank <- all %>%
mutate(ag = case_when(age2 == "18-35" ~ 1,
age2 == "36-44" ~ 2,
age2 == "45-54" ~ 3,
age2 == "55-64" ~ 4,
age2 == "65-74" ~ 5,
age2 == "75 and Older" ~ 6))
ggplot(rank, aes(ag, rank, color = issue)) +
geom_point(size=5, color="white") +
geom_point(size=4, shape=1) +
geom_point(size=3, shape=19) +
geom_bump(aes(smooth = 8), size = 2) +
scale_y_reverse(breaks = c(1,2,3,4,5,6,7,8,9,10,11,12,13,14, 15)) +
scale_x_continuous(breaks = c(1,2,3,4,5,6), labels = c("18-35", "36-44", "45-54", "55-64", "65-74", "75 and Older"), limits = c(-.8, 8)) +
theme_gg("Abel") +
geom_text(data = rank %>% filter(ag == min(ag)),
aes(x = ag - .1, label = issue), size = 5, hjust = 1, family = "font") +
geom_text(data = rank %>% filter(ag == max(ag)),
aes(x = ag + .1, label = issue), size = 5, hjust = 0, family = "font") +
scale_colour_tableau(palette = "Tableau 20") +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
labs(x = "Age Group", y = "Order of Importance", title = "Issue Ranks Among White Evangelicals", caption = "@ryanburge\nData: CCES 2016") +
ggsave("E://bump_rank.png", type = "cairo-png", width = 7.75)
imm_fun <- function(df, var, name){
df %>%
filter(pid7 == 5 | pid7 == 6 | pid7 == 7) %>%
filter(evangelical == 1 & race == 1) %>%
mutate(app = CC18_app_dtrmp_post) %>%
mutate(app = case_when(app == 1 | app == 2 ~ 1, TRUE ~ 0)) %>%
mutate(qq = {{var}}) %>%
mutate(qq = frcode(qq == 1 ~ "Support",
qq == 2 ~ "Oppose")) %>%
group_by(qq) %>%
mean_ci(app, ci = .84, wt = commonweight) %>%
mutate(type = name) %>%
na.omit()
}
aaa1 <- cces18 %>% imm_fun(CC18_322a, "Increase Border\nSecurity by $25 Billion")
aaa2 <- cces18 %>% imm_fun(CC18_322b, "DACA")
aaa3 <- cces18 %>% imm_fun(CC18_322c_new, "Eliminate Visa Lottery")
aaa4 <- cces18 %>% imm_fun(CC18_322c, "Withhold Fed. Funds\nfrom Sanctuary Cities")
aaa5 <- cces18 %>% imm_fun(CC18_322f, "Imprison Repeated\nBorder Crossers")
bbb1 <- cces18 %>% imm_fun(CC18_321a, "Allow Abortion\nas a Choice")
bbb2 <- cces18 %>% imm_fun(CC18_321b, "Permit Only In\nCase of Rape, \nIncest, Mother's Health")
bbb3 <- cces18 %>% imm_fun(CC18_321c, "Ban Abortion\nAfter 20 Weeks")
bbb4 <- cces18 %>% imm_fun(CC18_321d, "Allow Employers\nto Decline\nAbortion Coverage")
bbb5 <- cces18 %>% imm_fun(CC18_321f, "Make Illegal in\nAll Circumstances")
aaa <- bind_df("aaa")
aaa %>%
ggplot(., aes(x = qq, y = mean, fill = qq)) +
geom_col(color = "black") +
facet_wrap(~ type, nrow = 1) +
error_bar() +
theme_gg("Abel") +
y_pct() +
scale_fill_paletteer_d("awtools::mpalette") +
lab_bar(type = mean, pos = .025, sz = 4.25, top = FALSE) +
labs(x = "", y = "", title = "Trump Approval Among White Evangelical Republicans", subtitle = "By Position on Immigration Policy", caption = "@ryanburge\nData: CCES 2018") +
ggsave("E://immig_approval.png", type = "cairo-png", width = 8)
g2 <- bind_df("bbb")
g2 %>%
ggplot(., aes(x = qq, y = mean, fill = qq)) +
geom_col(color = "black") +
facet_wrap(~ type, nrow = 1) +
error_bar() +
theme_gg("Abel") +
y_pct() +
scale_fill_paletteer_d("awtools::mpalette") +
lab_bar(type = mean, pos = .025, sz = 4.25, top = FALSE) +
labs(x = "", y = "", title = "Trump Approval Among White Evangelical Republicans", subtitle = "By Position on Abortion Policy", caption = "@ryanburge\nData: CCES 2018") +
ggsave("E://ab_approval.png", type = "cairo-png", width = 8)
two_fun <- function(df, var, name){
df %>%
filter(pid7 == 5 | pid7 == 6 | pid7 == 7) %>%
filter(evangelical == 1 & race == 1) %>%
mutate(qq = {{var}}) %>%
mutate(qq = case_when(qq == 1 ~ 1,
qq == 2 ~ 0)) %>%
mean_ci(qq, ci = .84, wt = commonweight) %>%
mutate(type = name) %>%
na.omit()
}
bbb1 <- cces18 %>% two_fun(CC18_321a, "Allow Abortion\nas a Choice")
bbb2 <- cces18 %>% two_fun(CC18_321b, "Permit Only In\nCase of Rape, \nIncest, Mother's Health")
bbb3 <- cces18 %>% two_fun(CC18_321c, "Ban Abortion\nAfter 20 Weeks")
bbb4 <- cces18 %>% two_fun(CC18_321d, "Allow Employers\nto Decline\nAbortion Coverage")
bbb5 <- cces18 %>% two_fun(CC18_321f, "Make Illegal in\nAll Circumstances")
g3 <- bind_df("bbb")
g3 %>%
ggplot(., aes(x =type, y = mean, fill = mean)) +
geom_col(color = "black") +
error_bar() +
theme_gg("Abel") +
y_pct() +
coord_flip() +
lab_bar(type = mean, pos = .05, sz = 4.25, top = FALSE) +
labs(x = "", y = "", title = "Abortion Postions by White Evangelical Republicans", subtitle = "", caption = "@ryanburge\nData: CCES 2018") +
ggsave("E://ab_pos.png", type = "cairo-png", width = 8, height = 3.5)
evan <- cces18 %>%
filter(race == 1 & evangelical == 1) %>%
filter(pid7 == 5 | pid7 == 6 | pid7 == 7) %>%
mutate(age = 2018 - birthyr) %>%
mutate(attend = 7 - pew_churatd) %>%
mutate(male = case_when(gender == 1 ~ 1,
gender == 2 ~ 0)) %>%
mutate(ab = frcode(CC18_321a == 1 ~ "Support",
CC18_321a == 2 ~ "Oppose")) %>%
mutate(imm = frcode(CC18_322c_new == 1 ~ "Support",
CC18_322c_new == 2 ~ "Oppose")) %>%
mutate(income = car::recode(faminc_new, "97 = 0")) %>%
mutate(app = CC18_app_dtrmp_post) %>%
mutate(app = case_when(app == 1 | app == 2 ~ 1, TRUE ~ 0))
gg <- glm(app ~ ab*imm + attend + age + educ + male + income, data = evan, family = "binomial")
graph <- cat_plot(gg, pred = ab, modx = imm, interval = TRUE, int.width = .76, errorbar.width = .3, legend.main = "End Visa Lottery")
graph +
y_pct() +
theme_gg("Abel", legend = FALSE) +
# theme(legend.title = element_text(size = 12)) +
add_text(x=.77, y = .885, word = "Pro-Choice\nAnti-Immigration", sz = 2.5) +
add_text(x=1.22, y = .755, word = "Pro-Choice\nPro-Immigration", sz = 2.5) +
add_text(x=1.77, y = .9185, word = "Pro-Life\nAnti-Immigration", sz = 2.5) +
add_text(x=2.22, y = .8185, word = "Pro-Life\nPro-Immigration", sz = 2.5) +
labs(x = "Allow Abortion as a Choice", y = "Pr(Trump Approval)", title = "Trump Approval among White\nEvangelical Republicans", caption = "@ryanburge\nData: CCES 2018") +
ggsave("E://cat_plot_imm_abortion.png", type = "cairo-png", width = 4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.