Introduction

This R Markdown document demonstrates how to map election data from the UF Election Lab Data Archive, focusing on Alachua County in Florida for the 2020 presidential election. We will load and process spatial data using the sf package, calculate the Trump vote share, and visualize the results using ggplot2.

In this document, we will: - Load a shapefile containing precinct-level election data for the state of Florida. - Calculate the percentage of Trump votes in the Trump + Biden vote total for the 2020 election. - Filter the data to include only Alachua County. - Create a map to visualize the Trump vote share by precinct using a red-to-blue gradient.

The embedded R code chunks below will perform each of these tasks.

fl_2020 <- st_read("/Users/anabella/Downloads/fl_2020 (3)/fl_2020.shp")
## Reading layer `fl_2020' from data source 
##   `/Users/anabella/Downloads/fl_2020 (3)/fl_2020.shp' using driver `ESRI Shapefile'
## Simple feature collection with 6162 features and 11 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
#Make Geometries Valid
fl_2020 <- st_make_valid(fl_2020)
# Calculate Trump vote percentage
fl_2020 <- fl_2020 %>%
  mutate(trump_pct = G20PRERTRU / (G20PRERTRU + G20PREDBID))
summary(fl_2020)
##     COUNTY            PRECINCT           PCT_STD            G20PRERTRU  
##  Length:6162        Length:6162        Length:6162        Min.   :   0  
##  Class :character   Class :character   Class :character   1st Qu.: 298  
##  Mode  :character   Mode  :character   Mode  :character   Median : 724  
##                                                           Mean   : 920  
##                                                           3rd Qu.:1309  
##                                                           Max.   :6699  
##                                                                         
##    G20PREDBID       G20PRELJOR       G20PREODEL        G20PRESLAR    
##  Min.   :   0.0   Min.   :  0.00   Min.   : 0.0000   Min.   : 0.000  
##  1st Qu.: 346.2   1st Qu.:  3.00   1st Qu.: 0.0000   1st Qu.: 0.000  
##  Median : 750.0   Median :  7.00   Median : 1.0000   Median : 0.000  
##  Mean   : 859.6   Mean   : 11.41   Mean   : 0.9682   Mean   : 0.927  
##  3rd Qu.:1207.8   3rd Qu.: 16.00   3rd Qu.: 2.0000   3rd Qu.: 1.000  
##  Max.   :5238.0   Max.   :204.00   Max.   :16.0000   Max.   :10.000  
##                                                                      
##    G20PREGHAW       G20PRECBLA        G20PREOWRI              geometry   
##  Min.   : 0.000   Min.   : 0.0000   Min.   : 0.000   MULTIPOLYGON :6162  
##  1st Qu.: 0.000   1st Qu.: 0.0000   1st Qu.: 1.000   epsg:4269    :   0  
##  Median : 2.000   Median : 0.0000   Median : 3.000   +proj=long...:   0  
##  Mean   : 2.389   Mean   : 0.6332   Mean   : 3.971                       
##  3rd Qu.: 4.000   3rd Qu.: 1.0000   3rd Qu.: 6.000                       
##  Max.   :20.000   Max.   :12.0000   Max.   :38.000                       
##                                                                          
##    trump_pct     
##  Min.   :0.0000  
##  1st Qu.:0.3870  
##  Median :0.5212  
##  Mean   :0.5025  
##  3rd Qu.:0.6327  
##  Max.   :1.0000  
##  NA's   :217

Including Plots

Here is a plot showing the Trump vote share by precinct in Alachua County:

library(ggplot2)

# Create a plot to visualize Trump vote share
ggplot(data = fl_2020) +
  geom_sf(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",  # Color for NAs if any
                       name = "Trump %") +  # Label for the legend
  labs(title = "Trump Vote Share by Precinct in Alachua County")

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.