An interesting discussion on Twitter today brought up the relationship between race, voting, and legislative boundaries in Texas. This can be explored interactively with the Racial Dot Map from the University of Virginia, the tigris R package by myself and Bob Rudis, and RStudio’s Leaflet package with just a few lines of R.
To get started, let’s set up a map object for Texas. The map will use the Racial Dot Map as a base layer, and state legislative districts for the Texas House as a polygon overlay, to be displayed with black dashed lines.
# install.packages('leaflet')
# install.packages('tigris')
library(tigris)
library(leaflet)
txlege <- state_legislative_districts("Texas", house = "lower", cb = TRUE)
tiles <- "http://demographics.coopercenter.org/DotMap/tiles4/{z}/{x}/{y}.png"
attribution <- 'Tiles © 2013, Weldon Cooper Center for Public Service, Rector and Visitors of the University of Virginia (Dustin A. Cable, creator) | Map labels by <a href="http://stamen.com/">Stamen Design</a>'
stamen_labels <- "http://tile.stamen.com/toner-labels/{z}/{x}/{y}.png"
popup <- paste0("District: ", as.character(txlege$NAME))
map <- leaflet() %>%
addTiles(urlTemplate = tiles, attribution = attribution) %>%
addPolygons(data = txlege, color = "black", dashArray = '3', fillColor = "transparent", weight = 1, popup = popup) %>%
addTiles(urlTemplate = stamen_labels, group = "Map labels") %>%
addLegend(position = "bottomright",
colors = c("#33CCFF", "#66FF33", "#FF0000", "#FF9900", "#663300"),
labels = c("White", "Black", "Asian", "Hispanic", "Other"),
opacity = 1) %>%
addLayersControl(overlayGroups = "Map labels", options = layersControlOptions(collapsed = FALSE)) %>%
hideGroup("Map labels")
Now, let’s take a look at some major cities in Texas.
map %>% setView(lng = -97.002, lat = 32.833, zoom = 10)
map %>% setView(lng = -95.378, lat = 29.868, zoom = 10)
map %>% setView(lng = -97.811, lat = 30.307, zoom = 10)
map %>% setView(lng = -98.451, lat = 29.459, zoom = 10)