Mapping Voters

R
tidyverse
Maps
geocoding
Author

Robert W. Walker

Published

April 20, 2022

The Data

Robert Husseman (RHusseman?) on Twitter is running to represent Oregon District 21 in the Oregon House of Representatives. He has a platform that focuses on housing and livability in the state by making sensible allocations of public resources to public problems. He has an MBA; he was my student and his head and heart are very much in the right place to serve Oregon well.

His campaign has a voter list for the Democratic voters in Oregon 21. He shared access to this. Let me load them up from a saved Excel spreadsheet derived from a Google sheet.

Code
library(readxl)
RHCamp <- read_excel("data/RHCamp.xlsx", 
    col_types = c("numeric", "text", "text", 
        "text", "text", "text", "text", "date", 
        "text", "text", "text", "text", "text", 
        "text", "text", "text", "text", "text", 
        "text", "text", "text", "text", "text", 
        "text", "text", "text", "text", "text", 
        "text", "text", "numeric", "numeric", 
        "text", "text", "numeric", "numeric", 
        "numeric", "numeric", "numeric", 
        "numeric"))

Geocoding

I ran across a package called tidygeocoder to do the heavy lifting. Here is what I have to work with.

Code
library(tidyverse)
library(tidygeocoder)
names(RHCamp)
 [1] "VOTER_ID"          "FIRST_NAME"        "MIDDLE_NAME"      
 [4] "LAST_NAME"         "NAME_SUFFIX"       "BIRTH_DATE"       
 [7] "CONFIDENTIAL"      "EFF_REGN_DATE"     "STATUS"           
[10] "PARTY_CODE"        "PHONE_NUM"         "UNLISTED"         
[13] "COUNTY"            "RES_ADDRESS_1"     "RES_ADDRESS_2"    
[16] "HOUSE_NUM"         "HOUSE_SUFFIX"      "PRE_DIRECTION"    
[19] "STREET_NAME"       "STREET_TYPE"       "POST_DIRECTION"   
[22] "UNIT_TYPE"         "UNIT_NUM"          "ADDR_NON_STD"     
[25] "CITY"              "STATE"             "ZIP_CODE"         
[28] "ZIP_PLUS_FOUR"     "EFF_ADDRESS_1"     "EFF_ADDRESS_2"    
[31] "EFF_ADDRESS_3"     "EFF_ADDRESS_4"     "EFF_CITY"         
[34] "EFF_STATE"         "EFF_ZIP_CODE"      "EFF_ZIP_PLUS_FOUR"
[37] "ABSENTEE_TYPE"     "PRECINCT_NAME"     "PRECINCT"         
[40] "SPLIT"            

A Great Thing about R

There is a vignette explaining the usage.

My use case is pretty easy though there is a lot of data. I want to try this with just the addresses. I have loaded the tidyverse and now I want to peel off just the data that I need in order to geocode it, and then process it with the Census geocoder. This results in 9443 queries.

Code
GeoMe <- RHCamp %>% select(VOTER_ID, RES_ADDRESS_1, CITY, STATE) %>% geocode(street = RES_ADDRESS_1, city=CITY, state=STATE, method="census")
NewData <- GeoMe %>% left_join(., RHCamp)
save(NewData, file="~/Desktop/NewDataRH.RData")

This takes quite a long time to run – 667.1 seconds.

When it is complete, I will want to figure out how to map them in a way that is best fit for assisting a campaign. This seems like a good use for leaflet. Rather than taking 10 minutes or so to compile each time, I saved them off. I am loading them from a local file in the first line.

Leaflet Mapping

Code
load("data/NewDataRH.RData")

The leaflet package uses very powerful mapping tools that will be perfect for this use case because the maps have easy zoom features.

Code
library(leaflet)
NewData %>% mutate(labMy = paste0(FIRST_NAME,LAST_NAME,RES_ADDRESS_1,sep="\n")) %>%
  leaflet(data = .) %>% 
  addTiles() %>%
  addCircleMarkers(~long, ~lat, label = ~labMy, fillOpacity = 0.1, stroke=FALSE, radius=1.6)

That works but it isn’t quite right. Turns out that I will have to mess with html to get the labels quite right.

So, of course, I went to stackoverflow.

Building Better Labels

I found a post on how to do this.

The key piece of code seems to be:

Code
labs <- lapply(seq(nrow(NewData)), function(i) {
  paste0( '<p>', NewData[i,"RES_ADDRESS_1"],'</p>' )
})
NewData %>%
  leaflet(data = .) %>% 
  addTiles() %>%
  addCircleMarkers(~long, ~lat, label = lapply(labs, htmltools::HTML), fillOpacity = 0.15, stroke=FALSE, radius=1.6)

References

Code
knitr::write_bib(names(sessionInfo()$otherPkgs), file="bibliography.bib")

References

Cambon, Jesse, Diego Hernangómez, Christopher Belanger, and Daniel Possenriede. 2021. Tidygeocoder: Geocoding Made Easy. https://CRAN.R-project.org/package=tidygeocoder.
Cheng, Joe, Bhaskar Karambelkar, and Yihui Xie. 2022. Leaflet: Create Interactive Web Maps with the JavaScript Leaflet Library. https://rstudio.github.io/leaflet/.
Müller, Kirill, and Hadley Wickham. 2022. Tibble: Simple Data Frames. https://CRAN.R-project.org/package=tibble.
Wickham, Hadley. 2016. Ggplot2: Elegant Graphics for Data Analysis. Springer-Verlag New York. https://ggplot2.tidyverse.org.
———. 2022a. Stringr: Simple, Consistent Wrappers for Common String Operations. https://CRAN.R-project.org/package=stringr.
———. 2022b. Tidyverse: Easily Install and Load the Tidyverse. https://CRAN.R-project.org/package=tidyverse.
———. 2023. Forcats: Tools for Working with Categorical Variables (Factors). https://CRAN.R-project.org/package=forcats.
Wickham, Hadley, Mara Averick, Jennifer Bryan, Winston Chang, Lucy D’Agostino McGowan, Romain François, Garrett Grolemund, et al. 2019. “Welcome to the tidyverse.” Journal of Open Source Software 4 (43): 1686. https://doi.org/10.21105/joss.01686.
Wickham, Hadley, and Jennifer Bryan. 2023. Readxl: Read Excel Files. https://CRAN.R-project.org/package=readxl.
Wickham, Hadley, Winston Chang, Lionel Henry, Thomas Lin Pedersen, Kohske Takahashi, Claus Wilke, Kara Woo, Hiroaki Yutani, and Dewey Dunnington. 2023. Ggplot2: Create Elegant Data Visualisations Using the Grammar of Graphics. https://CRAN.R-project.org/package=ggplot2.
Wickham, Hadley, Romain François, Lionel Henry, Kirill Müller, and Davis Vaughan. 2023. Dplyr: A Grammar of Data Manipulation. https://CRAN.R-project.org/package=dplyr.
Wickham, Hadley, and Lionel Henry. 2023. Purrr: Functional Programming Tools. https://CRAN.R-project.org/package=purrr.
Wickham, Hadley, Jim Hester, and Jennifer Bryan. 2023. Readr: Read Rectangular Text Data. https://CRAN.R-project.org/package=readr.
Wickham, Hadley, Davis Vaughan, and Maximilian Girlich. 2023. Tidyr: Tidy Messy Data. https://CRAN.R-project.org/package=tidyr.