The USAboundaries package provides both historical and contemporary boundaries of the United States. This vignette gives an example of using the package to create maps of Maryland Congressional elections in 2016 (to the 115th Congress) and in 1792 (to the 3rd Congress). We will use the USAboundaries package data for the spatial data. See the documentation for the functions used for citations to the underlying datasets. For the 2016 Congressional votes, we will use data available on the website of the U.S. House of Representatives. For the 1792 elections we will use data from the Mapping Early American Elections project.

We begin by loading the packages that we need.

We can load the voting data for the 115th Congress into md_votes. We use this package to get the contemporary (2016) boundaries for Congressional districts in Maryland. They are returned as an sf object from the sf package.

In order to map the data, we have to join the voting data to the spatial data.

if (avail) {
  md_115 <- md_districts %>% 
   left_join(md_votes, by = c("cd115fp" = "district")) 

The data has the percentage of votes for Democrats and Republicans. We are going to map the margin that Republicans had over an even vote, so that we can see both the Democrat and Republican results. For a more careful analysis we would have to take account of third parties and write-in votes, but this will serve our purpose.

We create a color palette to turn the margin of victory into red and blue colors.

if (avail) {
  palette <- colorBin("RdBu", domain = c(-0.3, 0.3), bins = 7, reverse = TRUE)

We can use the leaflet package to make the map.

We can do the same analysis for the 3rd Congress, thanks to the historical data included in the package. In this case, we will have to map counties rather than Congressional districts. The parties of interest are the Federalists and the Democratic-Republicans.

We start by loading and joining the data.

Then we create a new palette function and make the map.