Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Immigration and Abortion Three Dumbbells
## Immigration ####
fun <- function(df, var, issue){
var <- enquo(var)
aa1 <- df %>%
mutate(trad = frcode(evangelical == 1 & race == 1 ~ "White Evangelical",
evangelical == 1 & race != 1 ~ "Non-White Evangelical",
bprot == 1 ~ "Black Protestant",
mainline == 1 ~ "Mainline",
religpew == 2 & race == 1 ~ "White Catholic",
religpew == 2 & race != 1 ~ "Non-White Catholic",
religpew == 3 ~ "Mormon",
religpew == 4 ~ "Orthodox",
religpew == 5 ~ "Jewish",
religpew == 6 ~ "Muslim",
religpew == 7 ~ "Buddhist",
religpew == 8 ~ "Hindu",
religpew == 9 ~ "Atheist",
religpew == 10 ~ "Agnostic",
religpew == 11 ~ "Nothing in Particular",
religpew == 12 ~ "Something Else")) %>%
mutate(imm = car::recode(!! var, "1=1; 2=0; else = NA")) %>%
group_by(trad) %>%
mean_ci(imm, wt = commonweight)
aa2 <- aa1 %>%
filter(trad == "White Evangelical")
aa3 <- aa1 %>%
filter(trad != "White Evangelical") %>%
filter(n > 500) %>%
arrange(-mean) %>%
top_n(1) %>%
mutate(trad = "Closest Religious Group")
aa4 <- bind_cols(aa2, aa3)
aa5 <- df %>%
mutate(imm = car::recode(!! var, "1=1; 2=0; else = NA")) %>%
mean_ci(imm, wt = commonweight) %>%
mutate(trad = "Entire Sample")
bind_cols(aa4, aa5) %>%
select(`White Evangelical` = mean,
`Next Most Conservative Religious Group` = mean1,
`Entire Sample` = mean2) %>%
mutate(type = issue)
}
ggg1 <- cces18 %>% fun(CC18_322a, "Increase Border Funding by $25 Billion")
# ggg2 <- cces18 %>% rev_fun(CC18_322b, "Support DACA")
ggg3 <- cces18 %>% fun(CC18_322c_new, "Eliminate Visa Lottery/Family-Based Migration")
ggg4 <- cces18 %>% fun(CC18_322c, "Withhold Federal Funds from Sanctuary Police Depts.")
ggg5 <- cces18 %>% fun(CC18_322f, "Imprison Repeated Border Crossers")
aa1 <- cces18 %>%
mutate(trad = frcode(evangelical == 1 & race == 1 ~ "White Evangelical",
evangelical == 1 & race != 1 ~ "Non-White Evangelical",
bprot == 1 ~ "Black Protestant",
mainline == 1 ~ "Mainline",
religpew == 2 & race == 1 ~ "White Catholic",
religpew == 2 & race != 1 ~ "Non-White Catholic",
religpew == 3 ~ "Mormon",
religpew == 4 ~ "Orthodox",
religpew == 5 ~ "Jewish",
religpew == 6 ~ "Muslim",
religpew == 7 ~ "Buddhist",
religpew == 8 ~ "Hindu",
religpew == 9 ~ "Atheist",
religpew == 10 ~ "Agnostic",
religpew == 11 ~ "Nothing in Particular",
religpew == 12 ~ "Something Else")) %>%
mutate(imm = car::recode(CC18_322b, "1=1; 2=0; else = NA")) %>%
group_by(trad) %>%
mean_ci(imm, wt = commonweight)
aa2 <- aa1 %>%
filter(trad == "White Evangelical")
aa3 <- aa1 %>%
filter(trad == "White Catholic") %>%
mutate(trad = "Closest Religious Group")
aa4 <- bind_cols(aa2, aa3)
aa5 <- cces18 %>%
mutate(imm = car::recode(CC18_322b, "1=1; 2=0; else = NA")) %>%
mean_ci(imm, wt = commonweight) %>%
mutate(trad = "Entire Sample")
ggg2 <- bind_cols(aa4, aa5) %>%
select(`White Evangelical` = mean,
`Next Most Conservative Religious Group` = mean1,
`Entire Sample` = mean2) %>%
mutate(type = "Support DACA")
graph <- bind_df("ggg")
g1 <- ggplot() +
# reshape the data frame & get min value so you can draw an eye-tracking line (this is one geom)
geom_segment(
data = gather(graph, measure, val, -type) %>%
group_by(type) %>%
top_n(-1) %>%
slice(1) %>%
ungroup(),
aes(x = 0, xend = val, y = type, yend = type),
linetype = "dotted", size = 0.5, color = "gray80"
) +
# reshape the data frame & get min/max category values so you can draw the segment (this is another geom)
geom_segment(
data = gather(graph, measure, val, -type) %>%
group_by(type) %>%
summarise(start = range(val)[1], end = range(val)[2]) %>%
ungroup(),
aes(x = start, xend = end, y = type, yend = type),
color = "gray80", size = 2
) +
# reshape the data frame & plot the points
geom_point(
data = gather(graph, measure, value, -type),
aes(value, type, color = measure), shape = 21, stroke =2, fill = "white", size = 3
)
g1 +
scale_color_manual(values = c("#56A8CBFF", "#53A567FF", "#DA291CFF")) +
scale_x_continuous(labels = scales::percent, limits = c(0, 1)) +
theme_gg("Abel") +
theme(legend.position = "bottom") +
labs(x = "Percent Who Agree", y = "", title = "White Evangelicals Are Extreme on Immigration", caption = "@ryanburge\nData: CCES 2018") +
ggsave("E://threepoint_dumbs_immigration.png", type = "cairo-png", width = 10)
## Abortion ####
fun <- function(df, var, issue){
var <- enquo(var)
aa1 <- df %>%
mutate(trad = frcode(evangelical == 1 & race == 1 ~ "White Evangelical",
evangelical == 1 & race != 1 ~ "Non-White Evangelical",
bprot == 1 ~ "Black Protestant",
mainline == 1 ~ "Mainline",
religpew == 2 & race == 1 ~ "White Catholic",
religpew == 2 & race != 1 ~ "Non-White Catholic",
religpew == 3 ~ "Mormon",
religpew == 4 ~ "Orthodox",
religpew == 5 ~ "Jewish",
religpew == 6 ~ "Muslim",
religpew == 7 ~ "Buddhist",
religpew == 8 ~ "Hindu",
religpew == 9 ~ "Atheist",
religpew == 10 ~ "Agnostic",
religpew == 11 ~ "Nothing in Particular",
religpew == 12 ~ "Something Else")) %>%
mutate(imm = car::recode(!! var, "1=1; 2=0; else = NA")) %>%
group_by(trad) %>%
mean_ci(imm, wt = commonweight)
aa2 <- aa1 %>%
filter(trad == "White Evangelical")
aa3 <- aa1 %>%
filter(trad != "White Evangelical") %>%
filter(n > 500) %>%
arrange(mean) %>%
top_n(-1) %>%
mutate(trad = "Closest Religious Group")
aa4 <- bind_cols(aa2, aa3)
aa5 <- df %>%
mutate(imm = car::recode(!! var, "1=1; 2=0; else = NA")) %>%
mean_ci(imm, wt = commonweight) %>%
mutate(trad = "Entire Sample")
bind_cols(aa4, aa5) %>%
select(`White Evangelical` = mean,
`Next Most Conservative Religious Group` = mean1,
`Entire Sample` = mean2) %>%
mutate(type = issue)
}
rev_fun <- function(df, var, issue){
var <- enquo(var)
aa1 <- df %>%
mutate(trad = frcode(evangelical == 1 & race == 1 ~ "White Evangelical",
evangelical == 1 & race != 1 ~ "Non-White Evangelical",
bprot == 1 ~ "Black Protestant",
mainline == 1 ~ "Mainline",
religpew == 2 & race == 1 ~ "White Catholic",
religpew == 2 & race != 1 ~ "Non-White Catholic",
religpew == 3 ~ "Mormon",
religpew == 4 ~ "Orthodox",
religpew == 5 ~ "Jewish",
religpew == 6 ~ "Muslim",
religpew == 7 ~ "Buddhist",
religpew == 8 ~ "Hindu",
religpew == 9 ~ "Atheist",
religpew == 10 ~ "Agnostic",
religpew == 11 ~ "Nothing in Particular",
religpew == 12 ~ "Something Else")) %>%
mutate(imm = car::recode(!! var, "1=1; 2=0; else = NA")) %>%
group_by(trad) %>%
mean_ci(imm, wt = commonweight)
aa2 <- aa1 %>%
filter(trad == "White Evangelical")
aa3 <- aa1 %>%
filter(trad != "White Evangelical") %>%
filter(n > 500) %>%
arrange(mean) %>%
top_n(1) %>%
mutate(trad = "Closest Religious Group")
aa4 <- bind_cols(aa2, aa3)
aa5 <- df %>%
mutate(imm = car::recode(!! var, "1=1; 2=0; else = NA")) %>%
mean_ci(imm, wt = commonweight) %>%
mutate(trad = "Entire Sample")
bind_cols(aa4, aa5) %>%
select(`White Evangelical` = mean,
`Next Most Conservative Religious Group` = mean1,
`Entire Sample` = mean2) %>%
mutate(type = issue)
}
aaa1 <- cces18 %>% fun(CC18_321a, "Always Allow as a\nMatter of Choice")
aaa3 <- cces18 %>% rev_fun(CC18_321c, "Ban After 20 Weeks")
aaa4 <- cces18 %>% rev_fun(CC18_321d, "Allow Employers to Decline\nAbortion Coverage in Insurance")
aaa5 <- cces18 %>% rev_fun(CC18_321e, "Prohibit Federal\nFunds for Abortion")
aaa6 <- cces18 %>% rev_fun(CC18_321f, "Make Abortion Illegal\nin All Circumstances")
graph <- bind_df("aaa")
g1 <- ggplot() +
# reshape the data frame & get min value so you can draw an eye-tracking line (this is one geom)
geom_segment(
data = gather(graph, measure, val, -type) %>%
group_by(type) %>%
top_n(-1) %>%
slice(1) %>%
ungroup(),
aes(x = 0, xend = val, y = type, yend = type),
linetype = "dotted", size = 0.5, color = "gray80"
) +
# reshape the data frame & get min/max category values so you can draw the segment (this is another geom)
geom_segment(
data = gather(graph, measure, val, -type) %>%
group_by(type) %>%
summarise(start = range(val)[1], end = range(val)[2]) %>%
ungroup(),
aes(x = start, xend = end, y = type, yend = type),
color = "gray80", size = 2
) +
# reshape the data frame & plot the points
geom_point(
data = gather(graph, measure, value, -type),
aes(value, type, color = measure), shape = 21, stroke =2, fill = "white", size = 3
)
g1 +
scale_color_manual(values = c("#56A8CBFF", "#53A567FF", "#DA291CFF")) +
scale_x_continuous(labels = scales::percent, limits = c(0, 1)) +
theme_gg("Abel") +
theme(legend.position = "bottom") +
labs(x = "Percent Who Agree", y = "", title = "White Evangelicals Are Also Pretty Extreme on Abortion", caption = "@ryanburge\nData: CCES 2018") +
ggsave("E://threepoint_dumbs_abortion.png", type = "cairo-png", width = 10)
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.