Instantly share code, notes, and snippets.

Embed
What would you like to do?
CR Influence and State Polarization
## Read in the CCES 2016
## This is calculating the weeklys and the nevers for each state
fff <- cces16 %>%
mutate(att = car::recode(pew_churatd, "1:2=1; else = 0")) %>%
group_by(inputstate) %>%
mean_ci(att) %>%
mutate(state = to_factor(inputstate)) %>%
select(state, week = mean)
fff2 <- cces16 %>%
mutate(att = car::recode(pew_churatd, "6=1; else = 0")) %>%
group_by(inputstate) %>%
mean_ci(att) %>%
mutate(state = to_factor(inputstate)) %>%
select(state, never = mean)
gg <- left_join(fff, fff2)
## Need to make a column of who won each state in 2016
votes <- read_csv("D://votes1216.csv")
win <- votes %>%
group_by(state_abbreviation) %>%
summarise(gop_sum = sum(votes_gop_2016), dem_sum = sum(votes_dem_2016)) %>%
mutate(total = gop_sum - dem_sum) %>%
mutate(winner = case_when(total > 0 ~ "GOP",
total < 0 ~ "Dem")) %>%
select(state_abbreviation, winner)
## Turn those states abb into full state names to join
# library(openintro)
win$state <- abbr2state(win$state_abbreviation)
win <- win %>%
select(state, winner)
gg <- left_join(gg, win)
## Alaska Got left out for some reason. Fix that.
gg[2,6] <- "GOP"
gg %>%
ggplot(., aes(x = reorder(state, -diff), y = diff, fill = winner)) +
geom_col(color = "black") +
coord_flip() +
theme_gg("Abel") +
scale_fill_manual(values = c("#1565C0", "#b92b27")) +
scale_y_continuous(labels = percent) +
theme(legend.position = c(.85,.45)) +
labs(y = "<-- More Never Than Weekly : More Weekly Than Never -->", x = "", title = "Which States Have More Regular Attenders Than Never Attenders?", caption = "Data: CCES 2016") +
annotate("text", y=.2, x = 27, label = "2016 Presidential", size = 12, family = "font", fontface = 2) +
annotate("text", y=.2, x = 26, label = "Election Result", size = 12, family = "font", fontface = 2) +
ggsave("D://cces/images/state_diff_att.png", height = 8, width =8)
## Making a HexMap
hex <- cces16 %>%
mutate(notimp = car::recode(pew_religimp, "4=1; else =0")) %>%
mutate(state = to_factor(inputstate)) %>%
group_by(state) %>%
mean_ci(notimp) %>%
select(state, mean)
## As a reminder to myself. You have to have the entire unzipped list of files for these shape files to read correctly. Not just the .shp one
US_shp <- rgdal::readOGR("D:\\cb_2017_us_state_5m.shp")
US_shp_cropped <- crop(US_shp, extent(-124.848974, -66.885444, 24.396308, 49.384358))
US_shp_cropped@data <- left_join(US_shp_cropped@data, hex, by = c("NAME" = "state"))
new_cells_hex <- calculate_grid(shape = US_shp_cropped, grid_type = "hexagonal", seed = 1)
result_hex <- assign_polygons(US_shp_cropped, new_cells_hex)
clean <- function(shape) {
shape@data$id = rownames(shape@data)
shape.points = fortify(shape, region="id")
shape.df = merge(shape.points, shape@data, by="id")
}
result_hex_df <- clean(result_hex)
font_add_google("Abel", "font")
showtext_auto()
ggplot(result_hex_df) +
geom_polygon(aes(x = long, y = lat, fill = mean, group = group), color = "black") +
geom_text(aes(V1, V2, label = STUSPS), size = 5, color = "white") +
scale_fill_gradient(low = "#108dc7", high = "#ef8e38", name = " Religion Not at All Important (%)", labels = percent) +
theme_void() +
coord_equal() +
theme(legend.text=element_text(size=34)) +
labs(title = "Which States Have Low Levels of Religious Importance?", caption = "Data: CCES 2016") +
theme(plot.title = element_text(family = "font", size = 40, vjust =2, face = "bold")) +
theme(plot.caption = element_text(family = "font", size = 36)) +
theme(legend.title = element_text(family = "font", size = 24, vjust =1.2, hjust = 112, face = "bold")) +
theme(legend.text=element_text(family = "font", face = "bold")) +
theme(legend.position="bottom") +
ggsave("D://cces/images/hexmap.png")
## Scatter of Nones and Evangelicals
ddd1 <- cces16 %>%
mutate(bagain = car::recode(pew_bornagain, "1=1; else =0")) %>%
group_by(inputstate) %>%
mean_ci(bagain) %>%
mutate(state = to_factor(inputstate)) %>%
dplyr::select(state, evanmean = mean)
ddd2 <- cces16 %>%
group_by(inputstate) %>%
mean_ci(none) %>%
mutate(state = to_factor(inputstate)) %>%
dplyr::select(state, nonemean = mean)
graph <- left_join(ddd1, ddd2)
tot <- cces16 %>%
group_by(inputstate) %>%
count() %>%
ungroup(inputstate) %>%
mutate(state = to_factor(inputstate)) %>%
dplyr::select(state, total = n)
graph <- left_join(graph, tot)
graph <- left_join(graph, win)
graph[2,5] <- "GOP"
graph %>%
ggplot(., aes(x=evanmean, y = nonemean, size = total, fill = winner)) +
geom_point(shape = 21, stroke = 2) +
geom_smooth(method =lm, aes(color = winner), alpha = .15, linetype = "dashed") +
theme_gg("Abel") +
geom_text_repel(aes(evanmean, y = nonemean, label = state), size = 10, nudge_y = .005) +
scale_fill_manual(values = c("dodgerblue3", "firebrick3")) +
scale_color_manual(values = c("dodgerblue3", "firebrick3")) +
scale_y_continuous(labels = percent) +
scale_x_continuous(labels = percent) +
labs(x = "Percent of Born-Again", y = "Percent of Nones", title = "Relationship between Born-Agains and Nones", caption = "Data: CCES 2016") +
ggsave("D://cces/images/scatter_none_evan.png", width = 8, height = 8)
## This is making the animated gif
library(haven)
library(labelled)
library(gganimate)
aaa1 <- cces08 %>%
mutate(bagain = car::recode(V215, "1=1; else =0")) %>%
group_by(V206) %>%
mean_ci(bagain) %>%
mutate(year = 2008) %>%
select(state = V206, bamean =mean, year)
aaa2 <- cces08 %>%
mutate(none = car::recode(V219, "9:11=1; else =0")) %>%
group_by(V206) %>%
mean_ci(none) %>%
mutate(year = 2008) %>%
select(state = V206, nonemean =mean, year)
aaa3 <- cces08 %>%
group_by(V206) %>%
count() %>%
select(state = V206, total =n)
add08 <- left_join(aaa1, aaa2)
add08 <- left_join(add08, aaa3)
aaa4 <- cces08 %>%
filter(CC307a < 8) %>%
group_by(V206) %>%
mean_ci(CC307a) %>%
select(state = V206, pid = mean)
add08 <- left_join(add08, aaa4)
aaa1 <- cces10 %>%
mutate(bagain = car::recode(V215, "1=1; else =0")) %>%
group_by(V206) %>%
mean_ci(bagain) %>%
mutate(year = 2010) %>%
select(state = V206, bamean =mean, year)
aaa2 <- cces10 %>%
mutate(none = car::recode(V219, "9:11=1; else =0")) %>%
group_by(V206) %>%
mean_ci(none) %>%
mutate(year = 2010) %>%
select(state = V206, nonemean =mean, year)
aaa3 <- cces08 %>%
group_by(V206) %>%
count() %>%
select(state = V206, total =n)
add10 <- left_join(aaa1, aaa2)
add10 <- left_join(add10, aaa3)
aaa4 <- cces10 %>%
filter(V212d < 8) %>%
group_by(V206) %>%
mean_ci(V212d) %>%
select(state = V206, pid = mean)
add10 <- left_join(add10, aaa4)
aaa1 <- cces12 %>%
mutate(bagain = car::recode(pew_bornagain, "1=1; else =0")) %>%
group_by(inputstate) %>%
mean_ci(bagain) %>%
mutate(year = 2012) %>%
select(state = inputstate, bamean = mean, year)
aaa2 <- cces12 %>%
mutate(none = car::recode(religpew, "9:11=1; else =0")) %>%
group_by(inputstate) %>%
mean_ci(none) %>%
mutate(year = 2012) %>%
select(state = inputstate, nonemean = mean, year)
aaa3 <- cces12 %>%
group_by(inputstate) %>%
count() %>%
select(state = inputstate, total = n)
add12 <- left_join(aaa1, aaa2)
add12 <- left_join(add12, aaa3)
aaa4 <- cces12 %>%
filter(pid7 < 8) %>%
group_by(inputstate) %>%
mean_ci(pid7) %>%
select(state = inputstate, pid = mean)
add12 <- left_join(add12, aaa4)
aaa1 <- cces14 %>%
mutate(bagain = car::recode(pew_bornagain, "1=1; else =0")) %>%
group_by(inputstate) %>%
mean_ci(bagain) %>%
mutate(year = 2014) %>%
select(state = inputstate, bamean = mean, year)
aaa2 <- cces14 %>%
mutate(none = car::recode(religpew, "9:11=1; else =0")) %>%
group_by(inputstate) %>%
mean_ci(none) %>%
mutate(year = 2014) %>%
select(state = inputstate, nonemean = mean, year)
aaa3 <- cces14 %>%
group_by(inputstate) %>%
count() %>%
select(state = inputstate, total = n)
add14 <- left_join(aaa1, aaa2)
add14 <- left_join(add14, aaa3)
aaa4 <- cces14 %>%
filter(pid7 < 8) %>%
group_by(inputstate) %>%
mean_ci(pid7) %>%
select(state = inputstate, pid = mean)
add14 <- left_join(add14, aaa4)
aaa1 <- cces16 %>%
mutate(bagain = car::recode(pew_bornagain, "1=1; else =0")) %>%
group_by(inputstate) %>%
mean_ci(bagain) %>%
mutate(year = 2016) %>%
select(state = inputstate, bamean = mean, year)
aaa2 <- cces16 %>%
mutate(none = car::recode(religpew, "9:11=1; else =0")) %>%
group_by(inputstate) %>%
mean_ci(none) %>%
mutate(year = 2016) %>%
select(state = inputstate, nonemean = mean, year)
aaa3 <- cces16 %>%
group_by(inputstate) %>%
count() %>%
select(state = inputstate, total = n)
add16 <- left_join(aaa1, aaa2)
add16 <- left_join(add16, aaa3)
aaa4 <- cces16 %>%
filter(pid7 < 8) %>%
group_by(inputstate) %>%
mean_ci(pid7) %>%
select(state = inputstate, pid = mean)
add16 <- left_join(add16, aaa4)
all <- bind_df("add")
cc <- read_dta("D://cces/data/cces16.dta")
tt <- cc %>%
mutate(stname = to_factor(inputstate)) %>%
mutate(state = as.numeric(inputstate)) %>%
select(state, stname) %>%
distinct(state, stname)
# rm(cc)
graph <- merge(all, tt) %>% as.tibble()
pid_check <- graph %>%
group_by(stname) %>%
mean_ci(pid) %>% arrange(-mean)
pid_check$mark <- rep(1:3, each = 17)
pid_check <- pid_check %>% select(stname, mark )
graph <- merge(graph, pid_check)
p <- graph %>%
ggplot(., aes(x= bamean, y = nonemean))+
geom_point(aes(x = bamean, y = nonemean, size = total, fill = factor(mark)), show.legend = FALSE, shape = 21, stroke = 2) +
geom_text(aes(x = bamean, y = nonemean, label = stname), size=4, family = "Product Sans", nudge_y = .0075) +
theme_minimal() +
# scale_fill_gradient2(low = "dodgerblue3", mid = "azure4", high = "firebrick3") +
scale_fill_manual(values= c("firebrick3", "azure4", "dodgerblue3")) +
geom_smooth(se = FALSE) +
theme(text=element_text(size=22, family="Product Sans")) +
scale_y_continuous(labels = scales::percent) +
scale_x_continuous(labels = scales::percent) +
theme(legend.position = "none") +
labs(title = 'Relationship Between Born Agains and Nones', subtitle = 'Year: {round(frame_time,0)}', x = 'Percent Born-Again', y = 'Percent of Nones', caption = "Data: CCES (2008-2016)") +
transition_time(year) +
ease_aes('linear')
animate(p, width = 1000, height = 600, fps = 25, nframes = 200, end_pause = 15)
conger <- read_dta("D://conger.dta")
conger <- conger %>%
dplyr::select(state = var1, CRInfluenceIndex)
graph <- left_join(graph, conger)
graph %>%
ggplot(., aes(x=evanmean, CRInfluenceIndex)) +
geom_point() +
geom_smooth(method = lm)
gg <- lm(CRInfluenceIndex ~ evanmean*nonemean, data = graph)
gg2 <- interact_plot(gg, pred= evanmean, modx = nonemean, interval = T, int.width = .76, modx.values = "plus-minus")
gg2 +
labs(x = "Born Again Share", y = "Christian Right Influence", title = "Interaction of Nones and Born-Agains on Christian Right Influence") +
theme_gg("Abel") +
scale_x_continuous(labels = percent) +
scale_fill_npg() +
scale_color_npg() +
theme(legend.position = c(.75,.25)) +
theme(plot.title = element_text(size = 38)) +
annotate("text", y=2, x = .465, label = "% of Nones", size = 12, family = "font", fontface = 2) +
annotate("text", y=1.8, x =.49, label = "In State Population", size = 12, family = "font", fontface = 2) +
ggsave("D://cces/images/jtools_interact.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment