Last active
January 13, 2019 17:46
-
-
Save ryanburge/2293b4e8ab53416f7a44d720bab006c4 to your computer and use it in GitHub Desktop.
Clinton vs Obama vs Evanrate Mapping
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(socsci) | |
library(urbnmapr) | |
library(urbnthemes) | |
library(viridis) | |
library(fst) | |
library(showtext) | |
### Data from here: https://www.kaggle.com/joelwilson/2012-2016-presidential-elections/ | |
votes <- read_csv("D://votes1216.csv") | |
## Making Clinton vs Obama Share Map #### | |
vote <- votes %>% | |
select(fips, dem_diff = Clinton_Obama) %>% | |
arrange(-dem_diff) | |
territories_counties <- get_urbn_map(map = "territories_counties") | |
territories_counties <- territories_counties %>% | |
mutate(fips = as.numeric(county_fips)) %>% | |
select(-county_fips) | |
map <- left_join(territories_counties, vote) | |
font_add_google("Abel", "font") | |
showtext_auto() | |
map %>% | |
ggplot(aes(long, lat, group = group, fill = dem_diff)) + | |
geom_polygon(color = "black", size = 0.05) + | |
coord_map(projection = "albers", lat0 = 39, lat1 = 45) + | |
scale_fill_gradient(low = "firebrick1", high = "dodgerblue1", labels = scales::percent) + | |
# scale_fill_viridis(labels = scales::percent) + | |
theme(legend.title = element_text(), legend.key.width = unit(.5, "in")) + | |
labs(title = "Clinton's Performance vs Obama in 2012", caption = "", fill = "") + | |
urbnthemes::theme_urbn_map() + | |
theme(plot.title = element_text(size=75, family= "font", face= "bold")) + | |
theme(plot.caption = element_text(size = 36, family="font")) + | |
theme(text=element_text(size=45, family="font")) + | |
theme(legend.position = c(0.8, 0.2)) + | |
ggsave("D://clinton_v_obama.png") | |
## Making Evangelical Map #### | |
set_urbn_defaults(style = "map") | |
census <- read.fst("D://census.fst") | |
census <- census %>% | |
mutate(county_fips = as.numeric(fips)) %>% | |
select(county_fips, evanrate) | |
territories_counties <- get_urbn_map(map = "territories_counties") | |
territories_counties <- territories_counties %>% | |
mutate(county_fips = as.numeric(county_fips)) | |
joined <- left_join(territories_counties, census) | |
rm(territories_counties) | |
joined %>% | |
mutate(evanrate = recode(evanrate, "1000:2000 = 1000")) %>% | |
ggplot(aes(long, lat, group = group, fill = evanrate/1000)) + | |
geom_polygon(color = "black", size = 0.05) + | |
coord_map(projection = "albers", lat0 = 39, lat1 = 45) + | |
labs(title = "Percent Evangelical Per County", caption = "", fill = "") + | |
scale_fill_viridis(labels = percent) + | |
theme(legend.title = element_text(), legend.key.width = unit(.5, "in")) + | |
urbnthemes::theme_urbn_map() + | |
theme(plot.title = element_text(size=75, family= "font", face= "bold")) + | |
theme(plot.caption = element_text(size = 36, family="font")) + | |
theme(text=element_text(size=45, family="font")) + | |
theme(legend.position = c(0.8, 0.2)) + | |
ggsave("D://evan_map.png") | |
votes <- read_csv("D://votes1216.csv") | |
vote1 <- votes %>% | |
select(share = Obama, county_fips = fips) %>% | |
mutate(cand = "Obama") | |
vote2 <- votes %>% | |
select(share = Clinton, county_fips = fips) %>% | |
mutate(cand = "Clinton") | |
votes <- bind_rows(vote1, vote2) | |
votej <- left_join(census, votes) %>% as.tibble() | |
votej %>% | |
mutate(evanrate = recode(evanrate, "1000:2000 = 1000")) %>% | |
na.omit() %>% | |
ggplot(., aes(x= evanrate, y = share, color = cand)) + | |
geom_point(alpha = .2) + | |
geom_smooth(method = lm) + | |
theme_gg("Abel") + | |
scale_y_continuous(labels = percent) + | |
theme(legend.position = c(0.8, 0.7)) + | |
theme(plot.title = element_text(size = 32)) + | |
guides(color=guide_legend(override.aes=list(fill=NA))) + | |
labs(x = "Rate of Evangelicals per 1000", y = "Vote Share", title = "The Relationship between Evangelicals and Vote Choice in 2012 and 2016") + | |
ggsave("D://clinton_obama_scatter.png") | |
write_csv(votej, "D://djupe_data.csv") | |
## Attendance ScatterPlot #### | |
vote1 <- votes %>% | |
group_by(state_fips) %>% | |
mean_ci(Obama) %>% | |
select(inputstate = state_fips, share = mean) %>% | |
mutate(cand = "Obama") | |
cces16 <- read.fst("C://cces16.fst") | |
att16 <- cces16 %>% | |
mutate(att = recode(pew_churatd, "1:2=1; else =0")) %>% | |
group_by(inputstate) %>% | |
mean_ci(att) %>% | |
select(inputstate, att = mean) | |
v16 <- left_join(vote1, att16) | |
cces12 <- read.fst("C://cces12.fst") | |
att12 <- cces12 %>% | |
mutate(att = recode(pew_churatd, "1:2=1; else =0")) %>% | |
group_by(inputstate) %>% | |
mean_ci(att) %>% | |
select(inputstate, att = mean) | |
vote2 <- votes %>% | |
group_by(state_fips) %>% | |
mean_ci(Clinton) %>% | |
select(inputstate = state_fips, share = mean) %>% | |
mutate(cand = "Clinton") | |
v12 <- left_join(vote2, att12) | |
graph <- bind_rows(v12, v16) %>% | |
rename(state = inputstate) | |
library(haven) | |
library(labelled) | |
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) | |
graph <- merge(graph, tt) %>% as.tibble() | |
graph %>% | |
na.omit() %>% | |
ggplot(., aes(x= att, y = share, color = cand)) + | |
geom_point(alpha = .2, show.legend = FALSE) + | |
geom_smooth(method = lm, alpha = .2) + | |
theme_gg("Abel") + | |
geom_text_repel(data = graph, aes(att,share,label=stname), size = 6, show.legend = FALSE) + | |
scale_y_continuous(labels = percent) + | |
scale_x_continuous(labels = percent) + | |
theme(legend.position = c(0.8, 0.7)) + | |
theme(plot.title = element_text(size = 46)) + | |
guides(color=guide_legend(override.aes=list(fill=NA))) + | |
labs(x = "Percent Who Attend Weekly", y = "Vote Share", title = "The Relationship between Church Attendance and Vote Choice in 2012 and 2016") + | |
ggsave("D://clinton_obama_scatter_weekly.png") | |
## Field Offices #### | |
darr <- read_csv("D://darr.csv") %>% | |
rename(stname = state) | |
obama <- darr %>% | |
select(stname, num =obama) %>% | |
mutate(cand = "Obama") | |
clinton <- darr %>% | |
select(stname, num = clinton) %>% | |
mutate(cand = "Clinton") | |
gg1 <- graph %>% | |
filter(cand == "Clinton") %>% | |
select(cand, stname, att) %>% | |
left_join(clinton) | |
gg2 <- graph %>% | |
filter(cand == "Obama") %>% | |
select(cand, stname, att) %>% | |
left_join(obama) | |
graph <- bind_rows(gg1, gg2) | |
graph %>% | |
na.omit() %>% | |
filter(num > 0) %>% | |
ggplot(., aes(x= att, y = num, color = cand)) + | |
geom_point(alpha = .2, show.legend = FALSE) + | |
geom_smooth(alpha = .2) + | |
theme_gg("Abel") + | |
geom_text_repel(data = graph, aes(att,num,label=stname), size = 6, show.legend = FALSE) + | |
# scale_y_continuous(labels = percent) + | |
scale_x_continuous(labels = percent) + | |
theme(legend.position = c(0.8, 0.7)) + | |
theme(plot.title = element_text(size = 46)) + | |
guides(color=guide_legend(override.aes=list(fill=NA))) + | |
labs(x = "Percent Who Attend Weekly", y = "Number of Field Offices", title = "The Relationship between Church Attendance and Field Offices in 2012 and 2016") + | |
ggsave("D://clinton_obama_scatter_offices.png") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment