Created
October 17, 2018 13:40
-
-
Save ryanburge/35ce6d3bdd219f48945389fa68a72cd6 to your computer and use it in GitHub Desktop.
An animated gif from tweenr. State evangelicals and vote choice 2000-2016.
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(fst) | |
library(building) | |
library(car) | |
library(tidycensus) | |
library(extrafont) | |
library(viridis) | |
library(urbnmapr) | |
library(urbnthemes) | |
library(gganimate) | |
library(tweenr) | |
census <- read.fst("D://census.fst") | |
census00 <- read.fst("D://census00.fst") | |
vote <- read_csv("D://election0016.csv") | |
vote <- vote %>% | |
group_by(FIPS, year) %>% | |
summarise(sum = sum(candidatevotes)) %>% | |
left_join(vote) %>% | |
mutate(pct = candidatevotes/sum) %>% | |
ungroup(FIPS) | |
stupid_fun <- function(yr){ | |
yr <- enquo(yr) | |
dem <- vote %>% | |
filter(year == !! yr) %>% | |
filter(party == "democrat") | |
rep <- vote %>% | |
filter(year == !! yr) %>% | |
filter(party == "republican") | |
rep$diff <- rep$pct - dem$pct | |
rep <- rep %>% | |
rename(rep_adv = diff) | |
} | |
vote00 <- stupid_fun(2000) | |
vote04 <- stupid_fun(2004) | |
vote08 <- stupid_fun(2008) | |
vote12 <- stupid_fun(2012) | |
vote16 <- stupid_fun(2016) | |
vote00 %>% | |
group_by(state) %>% | |
summarise(total = sum(totalvotes, na.rm = TRUE), can = sum(candidatevotes, na.rm = TRUE)) %>% | |
mutate(pct = can/total) | |
agg_fun <- function(df, yr){ | |
yr <- enquo(yr) | |
df %>% | |
group_by(state) %>% | |
summarise(total = sum(totalvotes, na.rm = TRUE), can = sum(candidatevotes, na.rm = TRUE)) %>% | |
mutate(pct = can/total) %>% | |
mutate(year = !! yr) | |
} | |
vvv00 <- agg_fun(vote00, 2000) | |
vvv04 <- agg_fun(vote04, 2004) | |
vvv08 <- agg_fun(vote08, 2008) | |
vvv12 <- agg_fun(vote12, 2012) | |
vvv16 <- agg_fun(vote16, 2016) | |
agg1 <- bind_rows(vvv00, vvv04, vvv08) | |
agg2 <- bind_rows(vvv12, vvv16) | |
c1 <- census00 %>% | |
select(state, POP200, evanrt) %>% | |
rename(pop = POP200, evanrate = evanrt) %>% | |
group_by(state) %>% | |
summarise(evan = mean(evanrate, na.rm = TRUE), pop = sum(pop, na.rm= TRUE)) %>% | |
ungroup(state) | |
merge1 <- left_join(agg1, c1) %>% | |
select(state, pct, evan, pop, year) | |
c2 <- census %>% | |
select(stname, POP2010, evanrate) %>% | |
group_by(stname) %>% | |
summarise(evan = mean(evanrate, na.rm = TRUE), pop = sum(POP2010, na.rm= TRUE)) %>% | |
ungroup(stname) %>% | |
rename(state = stname) | |
merge2 <- left_join(agg2, c2) %>% | |
select(state, pct, evan, pop, year) | |
merge_all <- bind_rows(merge1, merge2) | |
chart <- merge_all %>% | |
ggplot(., aes(x= evan, y = pct, frame = year)) + | |
geom_point(aes(size = pop)) + | |
geom_smooth(aes(group = year), method = lm) | |
gganimate(chart) | |
tw_data <- merge_all %>% | |
rename(x = evan, y = pct, time=year, id = state) %>% | |
mutate(ease = "linear") | |
tween <- tween_elements(tw_data, "time", "id", "ease", nframes = 150) | |
tween <- tween %>% | |
mutate(year = round(time), | |
state = .group) | |
tt <- inner_join(tween, merge) %>% as.tibble() | |
font_add_google("Oswald", "font") | |
showtext_auto() | |
new_g <- tween %>% | |
ggplot(., aes(x=x, y=y, frame = .frame)) + | |
geom_point(alpha = .2) + | |
scale_size_continuous(range = c(2,25)) + | |
theme_minimal() + | |
theme(text=element_text(size=22, family="font")) + | |
geom_text(aes(label=state), size=4, nudge_y = .0, family = "font") + | |
geom_smooth(aes(group = .frame), se = FALSE) + | |
labs(x = "Rate of Evangelicals per Thousand", y = "Percent of vote for Republican", title = "State's Rate of Evangelicals and Vote for the GOP") + | |
scale_y_continuous(labels = scales::percent, limits = c(.0, .85)) + | |
scale_x_continuous(limits = c(-50, 500)) | |
gganimate(new_g, filename = "first_sec_final.gif", title_frame = FALSE, interval = .05, ani.width = 1000, ani.height = 1000) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment