Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Iowa Caucus
graph <- cces %>%
filter(state == "Iowa") %>%
mutate(trad = frcode(evangelical == 1 ~ "Evangelical",
mainline == 1 ~ "Mainline",
catholic == 1 ~ "Catholic",
none == 1 ~ "None",
TRUE ~ "All Others")) %>%
group_by(year) %>%
ct(trad, wt = weight, show_na = FALSE)
graph %>%
filter(year > 2006) %>%
ggplot(., aes(x = 1, y = pct, fill = trad)) +
geom_col(color = "black") +
coord_flip() +
facet_wrap(~ year, ncol =1, strip.position = "left") +
theme_gg("Abel") +
theme(legend.position = "bottom") +
scale_y_continuous(labels = percent) +
theme(strip.text.y = element_text(angle = 180)) +
guides(fill = guide_legend(reverse=T, nrow =1)) +
scale_fill_tableau() +
theme(axis.title.y=element_blank(), axis.text.y=element_blank(), axis.ticks.y=element_blank()) +
theme(panel.grid.minor.y=element_blank(), panel.grid.major.y=element_blank()) +
geom_text(aes(label = ifelse(pct >.05, paste0(pct*100, '%'), '')), position = position_stack(vjust = 0.5), size = 4, family = "font", color = "black") +
labs(x = "", y = "", title = "Religious Distribution in Iowa", subtitle = "", caption = "@ryanburge\nData: CCES 2008-2018") +
ggsave("E://iowa_reltrad.png")
graph1 <- cces %>%
filter(year == 2018) %>%
mutate(trad = frcode(evangelical == 1 ~ "Evangelical",
mainline == 1 ~ "Mainline",
catholic == 1 ~ "Catholic",
none == 1 ~ "None",
TRUE ~ "All Others")) %>%
group_by(year) %>%
ct(trad, wt = weight, show_na = FALSE) %>%
mutate(group = "Entire Country")
graph2 <- graph %>%
filter(year == 2018) %>%
mutate(group = "Iowa")
gg <- bind_rows(graph1, graph2)
gg %>%
ggplot(., aes(x = 1, y = pct, fill = trad)) +
geom_col(color = "black") +
coord_flip() +
facet_wrap(~ group, ncol =1, strip.position = "top") +
theme_gg("Abel") +
theme(legend.position = "bottom") +
scale_y_continuous(labels = percent) +
theme(strip.text.y = element_text(angle = 180)) +
guides(fill = guide_legend(reverse=T, nrow =1)) +
scale_fill_tableau() +
theme(axis.title.y=element_blank(), axis.text.y=element_blank(), axis.ticks.y=element_blank()) +
theme(panel.grid.minor.y=element_blank(), panel.grid.major.y=element_blank()) +
geom_text(aes(label = ifelse(pct >.05, paste0(pct*100, '%'), '')), position = position_stack(vjust = 0.5), size = 4, family = "font", color = "black") +
labs(x = "", y = "", title = "Comparing Iowa to the Entire Country", subtitle = "", caption = "@ryanburge\nData: CCES 2008-2018") +
ggsave("E://iowa_compare2.png", height = 4)
ggg <- cces %>%
filter(race == 1) %>%
filter(year >= 2008) %>%
filter(pid7 <= 7) %>%
mutate(rep = case_when(pid7 == 5 | pid7 == 6 | pid7 == 7 ~ 1, TRUE ~ 0)) %>%
mutate(trad = frcode(evangelical == 1 ~ "Evangelical",
mainline == 1 ~ "Mainline",
catholic == 1 ~ "Catholic",
none == 1 ~ "None",
TRUE ~ "All Others")) %>%
mutate(group = frcode(state == "Iowa" ~ "Iowa",
TRUE ~ "Rest of the Country")) %>%
group_by(trad, group, year) %>%
mean_ci(rep, wt = weight)
ggg %>%
ggplot(., aes(x = year, y = mean, color = group, group = group)) +
geom_point() +
geom_line() +
facet_wrap(~ trad) +
theme_gg("Abel") +
theme(legend.position = c(.8, .25)) +
y_pct() +
scale_color_d3() +
scale_x_continuous(breaks = c(2008, 2010, 2012, 2014, 2016, 2018)) +
labs(x = "", y = "", title = "Percent Identifying as Republican", subtitle = "Among White Respondents", caption = "@ryanburge\nData: CCES 2008-2018") +
ggsave("E://iowa_time_series.png", type = "cairo-png")
fun <- function(df, var, issue){
var <- enquo(var)
aa1 <- df %>%
filter(race == 1) %>%
filter(religpew == 1) %>%
mutate(vvv = car::recode(!! var, "1=1; 2=0; else = NA")) %>%
mean_ci(vvv, wt = commonweight)
aa2 <- df %>%
filter(inputstate == 18) %>%
filter(race == 1) %>%
filter(religpew == 1) %>%
mutate(vvv = car::recode(!! var, "1=1; 2=0; else = NA")) %>%
mean_ci(vvv, wt = commonweight)
bind_cols(aa1, aa2) %>%
select(`Entire Country` = mean,
`Iowa Only` = mean1) %>%
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(race == 1) %>%
filter(religpew == 1) %>%
mutate(vvv = car::recode(!! var, "2=1; 1=0; else = NA")) %>%
mean_ci(vvv, wt = commonweight)
aa2 <- df %>%
filter(inputstate == 18) %>%
filter(race == 1) %>%
filter(religpew == 1) %>%
mutate(vvv = car::recode(!! var, "2=1; 1=0; else = NA")) %>%
mean_ci(vvv, wt = commonweight)
bind_cols(aa1, aa2) %>%
select(`Entire Country` = mean,
`Iowa Only` = mean1) %>%
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, "Oppose Allowing 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", "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", subtitle = "Among White Protestants", caption = "@ryanburge\nData: CCES 2018") +
ggsave("E://threepoint_dumbs_iowa.png", type = "cairo-png", width = 10)
fun <- function(df, var, issue){
var <- enquo(var)
aa1 <- df %>%
filter(race == 1) %>%
filter(religpew == 2) %>%
mutate(vvv = car::recode(!! var, "1=1; 2=0; else = NA")) %>%
mean_ci(vvv, wt = commonweight)
aa2 <- df %>%
filter(inputstate == 18) %>%
filter(race == 1) %>%
filter(religpew == 2) %>%
mutate(vvv = car::recode(!! var, "1=1; 2=0; else = NA")) %>%
mean_ci(vvv, wt = commonweight)
bind_cols(aa1, aa2) %>%
select(`Entire Country` = mean,
`Iowa Only` = mean1) %>%
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(race == 1) %>%
filter(religpew == 2) %>%
mutate(vvv = car::recode(!! var, "2=1; 1=0; else = NA")) %>%
mean_ci(vvv, wt = commonweight)
aa2 <- df %>%
filter(inputstate == 18) %>%
filter(race == 1) %>%
filter(religpew == 2) %>%
mutate(vvv = car::recode(!! var, "2=1; 1=0; else = NA")) %>%
mean_ci(vvv, wt = commonweight)
bind_cols(aa1, aa2) %>%
select(`Entire Country` = mean,
`Iowa Only` = mean1) %>%
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, "Oppose Allowing 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", "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", subtitle = "Among White Catholics in Iowa", caption = "@ryanburge\nData: CCES 2018") +
ggsave("E://threepoint_dumbs_iowa_cath.png", type = "cairo-png", width = 10)
regg <- cces18 %>%
mutate(st = frcode(inputstate == 18 ~ "Iowa", TRUE ~ "Rest of the Country")) %>%
mutate(white = case_when(race == 1 ~ 1, TRUE ~ 0)) %>%
mutate(male = case_when(gender == 1 ~ 1, TRUE ~ 0)) %>%
mutate(age = 2018 - birthyr) %>%
mutate(att = car::recode(pew_churatd, "6=1; 5=2; 4=3; 3=4; 2=5; 1=6; else = NA")) %>%
mutate(tr = case_when(CC18_308a == 1 | CC18_308a == 2 ~ 1, TRUE~ 0)) %>%
mutate(trad = frcode(evangelical == 1 ~ "Evangelical",
mainline == 1 ~ "Mainline",
catholic == 1 ~ "Catholic",
none == 1 ~ "None")) %>%
select(st, white, male, age, att, tr, educ, trad)
reg1 <- glm(tr ~ att*st*trad + white + male + age + educ, data = regg, family = "binomial")
gg2 <- interact_plot(reg1, pred= att, modx = st, mod2 = trad, int.width = .76, interval = TRUE, mod2.labels = c("Evangelical", "Mainline", "Catholic", "None"))
gg2 +
labs(x = "Church Attendance", y = "Predicted Trump Approval", title = "Religious Iowans Like Trump as Much as the Rest of the Country", caption = "@ryanburge\nData: CCES 2018") +
theme_gg("Abel", legend = TRUE) +
scale_x_continuous(limits = c(1,6.1), breaks = c(1,2,3,4,5,6), labels = c("Never", "Seldom", "Yearly", "Monthly", "Weekly", "Weekly+")) +
scale_color_d3() +
scale_fill_d3() +
y_pct() +
ggsave("E://iowa_interact4way.png")
graph <- cces %>%
mutate(pid2 = frcode(pid7 == 1 | pid7 == 2 | pid7 == 3 ~ "Democrats",
pid7 == 5 | pid7 == 6 | pid7 == 7 ~ "Republicans")) %>%
filter(state == "Iowa") %>%
mutate(trad = frcode(evangelical == 1 ~ "Evangelical",
mainline == 1 ~ "Mainline",
catholic == 1 ~ "Catholic",
none == 1 ~ "None",
TRUE ~ "All Others")) %>%
group_by(pid2) %>%
ct(trad, wt = weight) %>%
na.omit()
graph %>%
ggplot(., aes(x = 1, y = pct, fill = trad)) +
geom_col(color = "black") +
coord_flip() +
facet_wrap(~ pid2, ncol =1, strip.position = "top") +
theme_gg("Abel") +
theme(legend.position = "bottom") +
scale_y_continuous(labels = percent) +
theme(strip.text.y = element_text(angle = 180)) +
guides(fill = guide_legend(reverse=T, nrow =1)) +
scale_fill_tableau() +
theme(axis.title.y=element_blank(), axis.text.y=element_blank(), axis.ticks.y=element_blank()) +
theme(panel.grid.minor.y=element_blank(), panel.grid.major.y=element_blank()) +
geom_text(aes(label = ifelse(pct >.05, paste0(pct*100, '%'), '')), position = position_stack(vjust = 0.5), size = 4, family = "font", color = "black") +
labs(x = "", y = "", title = "Religious Composition of the Two Parties", subtitle = "", caption = "@ryanburge\nData: CCES 2008-2018") +
ggsave("E://iowa_compare_pid2.png", height = 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.