Skip to content

Instantly share code, notes, and snippets.

@ryanburge
Last active January 6, 2020 14:14
Embed
What would you like to do?
Which One is Closer? Nones or White Evangelicals
fun <- function(df, var, issue){
var <- enquo(var)
aa1 <- df %>%
filter(evangelical == 1 & race == 1) %>%
mutate(vvv = car::recode(!! var, "1=1; 2=0; else = NA")) %>%
mean_ci(vvv, wt = commonweight)
aa2 <- df %>%
mutate(none = case_when(religpew == 9 | religpew == 10 ~ 1, TRUE ~ 0)) %>%
filter(pid3 == 1) %>%
filter(none == 0) %>%
mutate(vvv = car::recode(!! var, "1=1; 2=0; else = NA")) %>%
mean_ci(vvv, wt = commonweight)
aa3 <- df %>%
mutate(wep = case_when(evangelical == 1 & race == 1 ~ 1, TRUE ~ 0)) %>%
filter(pid3 == 2) %>%
filter(wep == 0) %>%
mutate(vvv = car::recode(!! var, "1=1; 2=0; else = NA")) %>%
mean_ci(vvv, wt = commonweight)
aa4 <- df %>%
filter(religpew == 9 | religpew == 10) %>%
mutate(vvv = car::recode(!! var, "1=1; 2=0; else = NA")) %>%
mean_ci(vvv, wt = commonweight)
bind_cols(aa1, aa2, aa3, aa4) %>%
select(`White Evangelicals` = mean,
`Democrats` = mean1,
`Republicans` = mean2,
`Atheists/Agnostics` = mean3) %>%
mutate(type = issue)
}
aaa3 <- cces18 %>% fun(CC18_320d, "Make it Easier to get CCL")
aaa5 <- cces18 %>% fun(CC18_322a, "Increase Funding for Border by $25B")
aaa7 <- cces18 %>% fun(CC18_322c_new, "Eliminate Visa Lottery")
aaa8 <- cces18 %>% fun(CC18_325a, "Cut Corporate Tax Rate to 21%")
aaa9 <- cces18 %>% fun(CC18_325f_new, "Reduce Taxes on the Rich")
aaa11 <- cces18 %>% fun(CC18_327c, "Repeal the ACA")
aaa12 <- cces18 %>% fun(CC18_331a, "Support Tariffs on Chinese Goods")
aaa13 <- cces18 %>% fun(CC18_332a, "Support Moving Capital to Jerusalem")
aaa14 <- cces18 %>% fun(CC18_332b, "Allow Construction of Keystone XL Pipeline")
aaa15 <- cces18 %>% fun(CC18_332c, "Withdraw from Paris Climate")
aaa16 <- cces18 %>% fun(CC18_332e, "Withdraw from TPP")
rev_fun <- function(df, var, issue){
var <- enquo(var)
aa1 <- df %>%
filter(evangelical == 1 & race == 1) %>%
mutate(vvv = car::recode(!! var, "2=1; 1=0; else = NA")) %>%
mean_ci(vvv, wt = commonweight)
aa2 <- df %>%
mutate(none = case_when(religpew == 9 | religpew == 10 ~ 1, TRUE ~ 0)) %>%
filter(pid3 == 1) %>%
filter(none == 0) %>%
mutate(vvv = car::recode(!! var, "2=1; 1=0; else = NA")) %>%
mean_ci(vvv, wt = commonweight)
aa3 <- df %>%
mutate(wep = case_when(evangelical == 1 & race == 1 ~ 1, TRUE ~ 0)) %>%
filter(pid3 == 2) %>%
filter(wep == 0) %>%
mutate(vvv = car::recode(!! var, "2=1; 1=0; else = NA")) %>%
mean_ci(vvv, wt = commonweight)
aa4 <- df %>%
filter(religpew == 9 | religpew == 10) %>%
mutate(vvv = car::recode(!! var, "2=1; 1=0; else = NA")) %>%
mean_ci(vvv, wt = commonweight)
bind_cols(aa1, aa2, aa3, aa4) %>%
select(`White Evangelicals` = mean,
`Democrats` = mean1,
`Republicans` = mean2,
`Atheists/Agnostics` = mean3) %>%
mutate(type = issue)
}
aaa1 <- cces18 %>% rev_fun(CC18_320a, "Oppose Background Checks")
aaa2 <- cces18 %>% rev_fun(CC18_320c, "Oppose Assault Weapons Ban")
aaa6 <- cces18 %>% rev_fun(CC18_322b, "Oppose DACA")
aaa10 <- cces18 %>% rev_fun(CC18_327a, "Oppose Medicare for All")
aaa4 <- cces18 %>% rev_fun(CC18_321a, "Always Allow Abortion as a Choice")
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("purple", "dodgerblue3", "firebrick3", "forestgreen")) +
scale_x_continuous(labels = scales::percent, limits = c(0, 1)) +
theme_gg("Abel") +
theme(legend.position = "bottom") +
labs(x = "Percent Who Agree", y = "", title = "Ideological Positioning on Salient Topics in 2018", caption = "@ryanburge\nData: CCES 2018") +
ggsave("E://threepoint_dumbs_bunch.png", type = "cairo-png", width = 10)
graph <- graph %>%
as_tibble() %>%
mutate(right_diff = `White Evangelicals` - Republicans) %>%
mutate(left_diff = Democrats - `Atheists/Agnostics`) %>%
mutate(rght = case_when(right_diff > 0 ~ 1, TRUE ~ 0)) %>%
mutate(lft = case_when(left_diff > 0 ~ 1, TRUE ~ 0))
graph %>%
ggplot(., aes(x = type, y = right_diff, fill = factor(rght))) +
geom_col(color = "black") +
coord_flip() +
scale_fill_manual(values = c("firebrick3", "forestgreen")) +
scale_y_continuous(labels = scales::percent, limits = c(-.15, .15)) +
theme_gg("Abel") +
labs(y = "<-- Republicans More Conservative::WEPs More Conservative -->", x= "", title = "Where Is There Daylight Between the Groups?", caption = "@ryanburge\nData: CCES 2018") +
# add_text(x = 8, y = -.05, word = "Republicans More Conservative", sz = 4) +
ggsave("E://rightsidegraph.png", type = "cairo-png")
graph %>%
ggplot(., aes(x = type, y = left_diff, fill = factor(lft))) +
geom_col(color = "black") +
coord_flip() +
scale_fill_manual(values = c("dodgerblue3", "purple")) +
scale_y_continuous(labels = scales::percent, limits = c(-.15, .15)) +
theme_gg("Abel") +
labs(y = "<-- Democrats More Liberal::Nones More Liberal -->", x= "", title = "Where Is There Daylight Between the Groups?", caption = "@ryanburge\nData: CCES 2018") +
# add_text(x = 8, y = -.05, word = "Republicans More Conservative", sz = 4) +
ggsave("E://leftsidegraph.png", type = "cairo-png")
fun <- function(df, var, issue){
var <- enquo(var)
aa1 <- df %>%
filter(evangelical == 1 & race == 1) %>%
mutate(vvv = car::recode(!! var, "1=1; 2=0; else = NA")) %>%
mean_ci(vvv, wt = commonweight)
aa2 <- df %>%
filter(religpew == 9 | religpew == 10) %>%
mutate(vvv = car::recode(!! var, "1=1; 2=0; else = NA")) %>%
mean_ci(vvv, wt = commonweight)
aa3 <- df %>%
mutate(none = case_when(religpew == 9 | religpew == 10 ~ 1, TRUE ~ 0)) %>%
mutate(wep = case_when(evangelical == 1 & race == 1 ~ 1, TRUE ~ 0)) %>%
filter(none == 0) %>%
filter(wep == 0) %>%
mutate(vvv = car::recode(!! var, "1=1; 2=0; else = NA")) %>%
mean_ci(vvv, wt = commonweight)
bind_cols(aa1, aa2, aa3) %>%
select(`White Evangelicals` = mean,
`Atheists/Agnostics` = mean1,
`Rest of the Sample` = mean2) %>%
mutate(type = issue)
}
aaa3 <- cces18 %>% fun(CC18_320d, "Make it Easier to get CCL")
aaa5 <- cces18 %>% fun(CC18_322a, "Increase Funding for Border by $25B")
aaa7 <- cces18 %>% fun(CC18_322c_new, "Eliminate Visa Lottery")
aaa8 <- cces18 %>% fun(CC18_325a, "Cut Corporate Tax Rate to 21%")
aaa9 <- cces18 %>% fun(CC18_325f_new, "Reduce Taxes on the Rich")
aaa11 <- cces18 %>% fun(CC18_327c, "Repeal the ACA")
aaa12 <- cces18 %>% fun(CC18_331a, "Support Tariffs on Chinese Goods")
aaa13 <- cces18 %>% fun(CC18_332a, "Support Moving Capital to Jerusalem")
aaa14 <- cces18 %>% fun(CC18_332b, "Allow Construction of Keystone XL Pipeline")
aaa15 <- cces18 %>% fun(CC18_332c, "Withdraw from Paris Climate")
aaa16 <- cces18 %>% fun(CC18_332e, "Withdraw from TPP")
rev_fun <- function(df, var, issue){
var <- enquo(var)
aa1 <- df %>%
filter(evangelical == 1 & race == 1) %>%
mutate(vvv = car::recode(!! var, "2=1; 1=0; else = NA")) %>%
mean_ci(vvv, wt = commonweight)
aa2 <- df %>%
filter(religpew == 9 | religpew == 10) %>%
mutate(vvv = car::recode(!! var, "2=1; 1=0; else = NA")) %>%
mean_ci(vvv, wt = commonweight)
aa3 <- df %>%
mutate(none = case_when(religpew == 9 | religpew == 10 ~ 1, TRUE ~ 0)) %>%
mutate(wep = case_when(evangelical == 1 & race == 1 ~ 1, TRUE ~ 0)) %>%
filter(none == 0) %>%
filter(wep == 0) %>%
mutate(vvv = car::recode(!! var, "2=1; 1=0; else = NA")) %>%
mean_ci(vvv, wt = commonweight)
bind_cols(aa1, aa2, aa3) %>%
select(`White Evangelicals` = mean,
`Atheists/Agnostics` = mean1,
`Rest of the Sample` = mean2) %>%
mutate(type = issue)
}
aaa1 <- cces18 %>% rev_fun(CC18_320a, "Oppose Background Checks")
aaa2 <- cces18 %>% rev_fun(CC18_320c, "Oppose Assault Weapons Ban")
aaa6 <- cces18 %>% rev_fun(CC18_322b, "Oppose DACA")
aaa10 <- cces18 %>% rev_fun(CC18_327a, "Oppose Medicare for All")
aaa4 <- cces18 %>% rev_fun(CC18_321a, "Always Allow Abortion as a Choice")
graph <- bind_df("aaa")
graph <- graph %>%
as_tibble() %>%
mutate(left_diff = `Atheists/Agnostics` - `Rest of the Sample`) %>%
mutate(right_diff = `White Evangelicals` - `Rest of the Sample`) %>%
mutate(left = left_diff*-1) %>%
mutate(left_label = paste0((left*100), '%')) %>%
mutate(right_label = paste0((right_diff*100), '%'))
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
)
graph <- graph %>%
as_tibble() %>%
mutate(left_diff = `Atheists/Agnostics` - `Rest of the Sample`) %>%
mutate(right_diff = `White Evangelicals` - `Rest of the Sample`) %>%
mutate(left = left_diff*-1) %>%
mutate(left_label = paste0((left*100), '%')) %>%
mutate(right_label = paste0((right_diff*100), '%')) %>%
mutate(diff = right_diff - left) %>%
mutate(winner = frcode(diff > .02 ~ "Atheists/Agnostics",
diff < .02 ~ "White Evangelical Protestants",
TRUE ~ "No Difference")) %>%
mutate(diff2 = abs(diff)) %>%
mutate(win2 = frcode(diff2 <= .02 ~ "No Difference",
winner == "Atheists/Agnostics" ~ "Atheists/Agnostics",
winner == "White Evangelical Protestants" ~ "White Evangelicals"))
g1 +
geom_rect(data=graph, aes(xmin=-.025, xmax=.04, ymin=-Inf, ymax=Inf), fill="gray") +
geom_rect(data=graph, aes(xmin=.845, xmax=.905, ymin=-Inf, ymax=Inf), fill="gray") +
geom_text(data=graph, aes(label=left_label, y=type, x=.01), fontface="bold", size=3, family="font") +
geom_text(data=graph, aes(label=right_label, y=type, x=.875), fontface="bold", size=3, family="font") +
scale_color_manual(values = c("purple", "black", "forestgreen")) +
scale_x_continuous(labels = scales::percent, limits = c(-.05, 1.1), breaks = c(.25, .5, .75)) +
geom_text(data=graph, aes(label= win2, y=type, x= 1), fontface="bold", size=3, family="font") +
theme_gg("Abel") +
annotate("text", x=1, y = 16.7, label = "Closer to the Mean", size = 3.5, family = "font", color = "firebrick3", fontface = "bold") +
theme(legend.position = "bottom") +
scale_y_discrete(expand = expand_scale(mult = c(.05, .075))) +
labs(x = "Percent Who Agree", y = "", title = "Which Group Better Represents the Average American?", caption = "@ryanburge\nData: CCES 2018") +
ggsave("E://threepoint_dumbs_bunch_v2.png", type = "cairo-png", width = 10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment