Workshop 6: Using Shapefiles and Visualizing Precinct-Level Results

In this assignment, I visualize the percentage of Trump voters in 2016 within Palm Beach County.

Necessary Packages for the Assignment

library(tidyverse)
library(sf)

Accessing the Shapefile

fl_2016 <- st_read("/Users/dalia/Downloads/fl_2016/fl_2016.shp")
## Reading layer `fl_2016' from data source 
##   `/Users/dalia/Downloads/fl_2016/fl_2016.shp' using driver `ESRI Shapefile'
## Simple feature collection with 6116 features and 18 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY, XYZ
## Bounding box:  xmin: -87.6349 ymin: 24.39631 xmax: -79.97431 ymax: 31.00097
## z_range:       zmin: 0 zmax: 0
## Geodetic CRS:  NAD83

Using Merge to Create a Vote Share: Trump share of the Trump and Clinton vote

fl_2016 <- fl_2016|> 
  mutate(trump_pct = G16PRERTRU/(G16PRERTRU + G16PREDCLI))

Making the Plot: filtering through desired counties and making the shape “valid” to work with

fl_2016 <- st_make_valid(fl_2016) 

fl_2016_PalmBeach <- fl_2016 |>
  filter(COUNTY == "PAL")

Creating the Plot

ggplot(data = fl_2016_PalmBeach) +
  geom_sf(data = fl_2016_PalmBeach, aes(fill = trump_pct), color = "black", alpha = 0.7) +
  theme_minimal() +
  scale_fill_gradient2(low = "blue", mid = "white", high = "red",
                      midpoint = 0.5,
                      limits = c(0, 1), 
                      na.value = "grey50",  
                      name = "2016 Trump %") +  
  labs(title = "Palm Beach County")