Preparing R

The two packages needed for this project are “sf” and “tidyverse”. Loading these packages is shown below.

library(sf)
library(tidyverse)

Loading in the data.

For this project, we created a map of the 2016 General Election, specifically in Alamance County, NC. This information was gathered from the North Carolina State Board of Elections.

The second line of code is a fail safe in case there are any errors in reading the boundaries of the counties being loaded in.

nc_2016 <- st_read("/Users/ajcoots/Downloads/nc_2016/nc_2016.shp")
nc_2016 <- st_make_valid(nc_2016)

Filtering the data.

To filter down to Alamance County, we created a separate data set named “nc_fil_2016”. This data set will only contain the data and precinct boundaries within Alamance County.

nc_fil_2016 <- nc_2016 |> 
  filter(COUNTY_NAM == "ALAMANCE")

Mutating the data.

In the first section of code, we added a column onto our newly created “shapefile” data set that found the percentage of votes that Clinton won compared to the entire electorate. This is represented by the variable “clinton_pct”.

The second section of code is declaring two variables, 0 and 1. This will make sense once we go to plot the data, but in a nutshell, these two variables (“min_clinton_pct” and “max_clinton_pct”), were created in order to scale the data when plotting.

nc_shapefile <- nc_fil_2016 |> 
  mutate(clinton_pct = G16PREDCLI/(G16PREDCLI + G16PRERTRU))

min_clinton_pct <- 0
max_clinton_pct <- 1

Plotting the data.

This section of code utilizes “ggplot” to plot our nc_shapefile. The code fills the precinct boundaries with the percentage of voter share that Clinton won, showing a deeper blue for a higher vote share, and deeper red for lower vote share (therefore a higher vote share for Trump).

This code also uses those two variables, min & max clinton_pct, as limits for our data set and to better represent our legends on the right side of our data.

The final step was cleaning up our plot by utilizing the “labs” function, creating a tidy legend and title.

ggplot(data = nc_shapefile) +
  geom_sf(aes(fill = clinton_pct)) +
  scale_fill_gradient2(
    low = "red", 
    mid = "white", 
    high = "blue",
    midpoint = 0.5, 
    limits = c(min_clinton_pct, max_clinton_pct),
    name = "Vote Share (%)",
    breaks = c(min_clinton_pct, 0.5, max_clinton_pct),  
    labels = c("Trump %", "50%", "Clinton %") 
  ) +
  theme_minimal() +
  labs(title = "Clinton vs Trump Vote Share in Alamance County, NC") +
  theme(legend.position = "right")