Last active
November 7, 2018 21:06
-
-
Save MattSandy/c76f0704c4d94a95bc68157fca8563d3 to your computer and use it in GitHub Desktop.
2018 Election Results for Minnesota
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("rvest") | |
library("openxlsx") | |
library("ggthemes") | |
# Election Results -------------------------------------------------------- | |
column_names <- c("State", | |
"County_ID", | |
"Precinct_name", | |
"Office_ID", | |
"Office_Name", | |
"District", | |
"Candidate_Order_Code", | |
"Candidate_Name", | |
"Suffix", | |
"Incumbent_Code", | |
"Party_Abbreviation", | |
"Number_of_Precincts_reporting", | |
"Total_number_of_precincts_voting_for_the_office", | |
"Votes_for_Candidate", | |
"Percentage_of_Votes_for_Candidate_out_of_Total_Votes_for_Office", | |
"Total_number_of_votes_for_Office_in_area") | |
urls <- list() | |
urls$results <- "https://electionresults.sos.state.mn.us/Results/MediaResult/115?mediafileid=38" | |
urls$county <- "http://w20.education.state.mn.us/MdeOrgView/reference/county" | |
urls$wiki <- "https://en.wikipedia.org/wiki/List_of_counties_in_Minnesota" | |
urls$xlsx <- "https://mn.gov/admin/assets/mn_county_estimates_sdc_2017v2_tcm36-353258.xlsx" | |
results <- read.csv(urls$results, | |
sep = ";", | |
stringsAsFactors = F, | |
header = F) | |
names(results) <- column_names | |
# County Information ------------------------------------------------------ | |
webpage <- read_html(urls$county) | |
tbls <- html_nodes(webpage, "table") | |
# Shows all tables | |
tbls | |
# Select Table | |
counties <- html_table(tbls[1],fill = T)[[1]] | |
names(counties) <- c("County_ID","County_Name") | |
# County Area Information ------------------------------------------- | |
webpage <- read_html(urls$wiki) | |
tbls <- html_nodes(webpage, "table") | |
# Shows all tables | |
tbls | |
# Select Table | |
wiki <- html_table(tbls[1],fill = T)[[1]] | |
names(wiki) <- gsub("\\[.*$","",names(wiki)) | |
wiki$Population <- wiki$Population %>% | |
gsub("^.*♠","",.) %>% | |
gsub("[^0-9\\.]","",.) %>% | |
as.numeric | |
wiki$Area <- wiki$Area %>% | |
gsub("^[0-9]+♠","",.) %>% | |
gsub("sq.*$","",.) %>% | |
gsub("[^0-9\\.]","",.) %>% | |
as.numeric | |
wiki$Map <- NULL | |
wiki$County <- gsub(" County","",wiki$County) | |
names(wiki)[1] <- "County_Name" | |
# Population Estimates for 2017 ------------------------------------------- | |
estimates <- read.xlsx(urls$xlsx) | |
# Fix Names (comma + period OR period get replaced with underscore) | |
names(estimates) <- gsub("(\\,?\\.)","_",names(estimates)) | |
# County IDs are not state codes for the county? | |
names(estimates)[1] <- "County_ID" | |
estimates$County_ID <- NULL | |
# Append County Information to Election Results --------------------------- | |
counties <- merge(counties, estimates, by = "County_Name", all.x = T) %>% | |
merge(wiki, by = "County_Name", all.x = T) | |
results <- merge(results, counties, by = "County_ID", all.x = T) | |
results$Density <- results$Total_Population_2017 / results$Area | |
fix <- which(is.na(results$Density)) | |
results$Density[fix] <- results$Population[fix] / results$Area[fix] | |
results$Percent_Area <- round((results$Votes_for_Candidate / results$Total_number_of_votes_for_Office_in_area) * 100,digits = 2) | |
# Senate Results ---------------------------------------------------------- | |
senate <- filter(results, Office_Name == "U.S. Senator" & Party_Abbreviation %in% c("DFL","R")) | |
senate.hennepin <- filter(senate, County_Name == "Hennepin") | |
sample <- senate[which(is.na(senate$Density)),] | |
# Plot Results ------------------------------------------------------------ | |
p1 <- ggplot(senate, aes(x = Density, | |
y = Percent_Area, | |
color = Party_Abbreviation, | |
group = Party_Abbreviation, | |
text = County_Name)) + | |
geom_point() + theme_fivethirtyeight() + | |
scale_y_continuous(limits = c(20,80)) + | |
scale_color_manual(values = c("#2f99ea","#ea2f2f")) + | |
scale_x_log10() + | |
geom_density_2d() + facet_wrap(~Party_Abbreviation) + | |
theme(axis.title = element_text(), | |
legend.position = "right", | |
legend.justification = "top", | |
legend.box = "vertical", | |
legend.direction = "vertical") + | |
labs(x = "\nPeople Per Square Mile (base-10 log scale)", | |
caption = "@appupio", | |
y = "Percent of Vote\n", | |
color = "Party") + | |
ggtitle("2018 Election Results for Minnesota") | |
p1 + geom_smooth() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment