ElectProject / Early-Vote-2020G

Tracker for 2020 general election early vote (mail and in-person) activity
109 stars 20 forks source link

WV data pull and mapping #602

Open engineerchange opened 3 years ago

engineerchange commented 3 years ago

Here's some minimally viable code to help with mapping West Virginia. I'm not entirely sure of the dataframe you use on the backend, so I'm not sure a PR would be helpful; but it looks like you can easily pull metrics on registration count and requested/returned ballots.

library(tidyverse)
library(RCurl)
library(tabulizer)
library(highcharter)

# example of how data pulled in for Wisconsin (WI):
   #WI_stats <- read_csv("D:/DropBox/Dropbox/Mail_Ballots_2020/markdown/2020G_Early_Vote_WI.csv")

# pull in registration data via PDF
WV_registration <- tabulizer::extract_tables('https://sos.wv.gov/elections/Documents/VoterRegistrationTotals/2020/Oct2020.pdf') %>% .[[1]] %>%
  as_tibble()
colnames(WV_registration) <- WV_registration[1,]
WV_registration <- WV_registration[-1, ] 

# pull in current requested/returned statistics
WV_stats <- read.csv(textConnection(RCurl::getURL('http://services.sos.wv.gov/Elections/AbsenteeBallotStats/Home/CountyData')))
WV_map_data <- WV_stats %>%
  rename("County"="X",
         "Mail.Req.Tot"="Requested",
         "Mail.Ret.Tot"="Returned") %>% select(County,Mail.Req.Tot,Mail.Ret.Tot)
# merge in fips and connect statistics with registration data
WV_map_data <- WV_map_data %>%
  left_join(get_data_from_map(download_map_data("countries/us/us-wv-all")) %>% select(name,fips) %>%
              rename("county"="name") %>% mutate(county=toupper(county)),by=c("County"="county")) %>%
  left_join(WV_registration,by="County") %>%
  rename("Reg.Voters"="Total") %>%
  mutate(Reg.Voters=as.numeric(Reg.Voters))

# prepare for highcharter
WV_map_data <- mutate(WV_map_data, percent = round(100*(Mail.Req.Tot/Reg.Voters), digits = 1))
WV_map_data <- mutate(WV_map_data, fips = as.character(fips))

# highcharter
hcmap(map = "countries/us/us-wv-all", data = WV_map_data,
      value = "percent", name = "Percent Requested", joinBy = "fips") %>%
  hc_title(text ="Mail Ballot Request Rates") %>%
  hc_subtitle(text = "County plots may not be shaded using the same scale")
engineerchange commented 3 years ago

Just a heads up; the data pull didn't work yesterday for some reason, but I worked with the WV Dept of Elections and fixed the issue on their end. Let me know if there is any rework I can do to assist with your process.