Last active
November 7, 2020 03:41
-
-
Save MattSandy/3054f559b9066bd43659e227cebe0b77 to your computer and use it in GitHub Desktop.
Voter Shift
This file contains hidden or 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
name,registered_voters_2020,registered_voters_2016 | |
Aitkin,"10,840","10,090" | |
Anoka,"228,495","206,600" | |
Becker,"21,396","18,829" | |
Beltrami,"27,518","24,213" | |
Benton,"24,688","22,106" | |
Big Stone,"3,212","3,090" | |
Blue Earth,"39,978","36,184" | |
Brown,"16,209","15,052" | |
Carlton,"22,253","20,424" | |
Carver,"70,539","61,884" | |
Cass,"20,559","18,421" | |
Chippewa,"7,321","6,858" | |
Chisago,"36,747","32,855" | |
Clay,"36,387","32,465" | |
Clearwater,"5,030","4,605" | |
Cook,"4,039","3,653" | |
Cottonwood,"6,531","6,297" | |
Crow Wing,"44,215","39,427" | |
Dakota,"283,710","257,792" | |
Dodge,"12,812","11,776" | |
Douglas,"26,440","24,302" | |
Faribault,"8,475","8,262" | |
Fillmore,"13,042","12,221" | |
Freeborn,"18,629","17,920" | |
Goodhue,"30,732","28,320" | |
Grant,"3,985","3,852" | |
Hennepin,"835,366","759,075" | |
Houston,"12,415","12,026" | |
Hubbard,"14,147","12,620" | |
Isanti,"25,822","22,926" | |
Itasca,"29,104","27,110" | |
Jackson,"6,266","5,985" | |
Kanabec,"10,060","9,264" | |
Kandiyohi,"25,592","23,719" | |
Kittson,"2,854","2,730" | |
Koochiching,"7,614","7,169" | |
Lac Qui Parle,"4,392","4,226" | |
Lake,"7,595","7,206" | |
Lake of the Woods,"2,641","2,443" | |
Le Sueur,"18,040","16,499" | |
Lincoln,"3,465","3,392" | |
Lyon,"14,322","13,559" | |
McLeod,"22,605","20,667" | |
Mahnomen,"2,883","2,483" | |
Marshall,"5,602","5,306" | |
Martin,"12,115","11,805" | |
Meeker,"14,321","13,493" | |
Mille Lacs,"15,884","14,442" | |
Morrison,"21,104","19,085" | |
Mower,"21,677","20,042" | |
Murray,"5,381","5,085" | |
Nicollet,"21,025","19,988" | |
Nobles,"10,066","9,536" | |
Norman,"3,824","3,615" | |
Olmsted,"101,371","90,259" | |
Otter Tail,"39,096","35,742" | |
Pennington,"7,777","7,400" | |
Pine,"16,959","15,526" | |
Pipestone,"5,482","5,370" | |
Polk,"17,628","16,516" | |
Pope,"7,375","6,897" | |
Ramsey,"334,758","305,584" | |
Red Lake,"2,331","2,157" | |
Redwood,"8,974","9,355" | |
Renville,"8,784","8,508" | |
Rice,"39,508","35,912" | |
Rock,"5,549","5,244" | |
Roseau,"9,260","8,543" | |
St. Louis,"131,653","123,666" | |
Scott,"94,317","82,598" | |
Sherburne,"60,396","53,667" | |
Sibley,"9,290","8,522" | |
Stearns,"93,373","86,457" | |
Steele,"22,912","21,026" | |
Stevens,"5,537","5,753" | |
Swift,"5,638","5,567" | |
Todd,"13,982","13,007" | |
Traverse,"2,003","2,002" | |
Wabasha,"14,248","13,277" | |
Wadena,"8,198","7,539" | |
Waseca,"11,222","10,580" | |
Washington,"178,499","159,431" | |
Watonwan,"5,682","5,866" | |
Wilkin,"3,799","3,583" | |
Winona,"29,674","28,825" | |
Wright,"87,354","75,963" | |
Yellow Medicine,"5,970","5,924" |
This file contains hidden or 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(tidyverse) | |
library(jsonlite) | |
library(rvest) | |
library(plotly) | |
library(glue) | |
# 2020 Data --------------------------------------------------------------- | |
nyt <- "https://static01.nyt.com/elections-assets/2020/data/api/2020-11-03/state-page" | |
df <- fromJSON(glue("{nyt}/minnesota.json"))$data$races$counties[[1]] | |
# Voter Registration 2016/2020 ------------------------------------------- | |
registered <- read_csv("registered-voters.csv") | |
webpage <- read_html("https://www.nytimes.com/elections/2016/results/minnesota") | |
tbls <- html_nodes(webpage, "table") | |
# Shows all tables where Walz is matched | |
df_2016 <- tbls %>% | |
grep("Hennepin",.) %>% # Find table with a county | |
tbls[.] %>% | |
html_table(fill=T) %>% .[[1]] %>% # Convert table to DF | |
rename(name = `Vote by county`) %>% | |
filter(!name %>% is.na) %>% # Drop NA | |
arrange(name) %>% # Get to line up with NYT 2020 data | |
mutate(Clinton = Clinton %>% str_replace('[^0-9]','') %>% as.numeric, | |
Trump = Trump %>% str_replace('[^0-9]','') %>% as.numeric) | |
# Contains the county names, 2016 registered voters, and 2020 registered voters | |
results <- df %>% left_join(registered) %>% arrange(name) # Ordered by County | |
# Build Votes DF ---------------------------------------------------------- | |
# DF for plot | |
votes <- data.frame( | |
county = results$name, | |
dem_2020 = results$results$bidenj, | |
rep_2020 = results$results$trumpd, | |
dem_2016 = df_2016$Clinton, | |
rep_2016 = df_2016$Trump, | |
voters_2020 = results$registered_voters_2020, | |
voters_2016 = results$registered_voters_2016, | |
rep_2016_percent = df_2016$Trump / results$registered_voters_2016 * 100, | |
dem_2016_percent = df_2016$Clinton / results$registered_voters_2016 * 100, | |
rep_2020_percent = results$results$trumpd / results$registered_voters_2020 * 100, | |
dem_2020_percent = results$results$bidenj / results$registered_voters_2020 * 100 | |
) | |
votes$dem_2016_offset <- votes$dem_2016_percent - mean(votes$rep_2016_percent %>% append(votes$dem_2016_percent)) | |
votes$rep_2016_offset <- votes$rep_2016_percent - mean(votes$rep_2016_percent %>% append(votes$dem_2016_percent)) | |
votes$dem_2020_offset <- votes$dem_2020_percent - mean(votes$rep_2016_percent %>% append(votes$dem_2016_percent)) | |
votes$rep_2020_offset <- votes$rep_2020_percent - mean(votes$rep_2016_percent %>% append(votes$dem_2016_percent)) | |
# Plot Data --------------------------------------------------------------- | |
p1 <- ggplot(votes) + | |
geom_segment(aes(x = dem_2016_offset, y = rep_2016_offset, | |
xend = dem_2020_offset, yend = rep_2020_offset, | |
colour = county, | |
size = voters_2020), | |
arrow = arrow(length = unit(0.03, "npc"))) + | |
geom_abline(slope=1, intercept=0) + | |
scale_x_continuous(limits = c(-25,30)) + | |
scale_y_continuous(limits = c(-25,30)) + | |
theme(legend.position = "none") + | |
xlab("Democratic") + | |
ylab("Republican") | |
ggsave(p1,filename = "plot.png",width=10,height=10) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment