Workshop 6

Select a state/election from the UF Election Lab Data Archive to map using R: Florida 2018

library(sf)
## Warning: package 'sf' was built under R version 4.3.3
## Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.4.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
fl_2018 <-st_read("fl_2018/fl_2018.shp")
## Reading layer `fl_2018' from data source `/Users/sanjanad/fl_2018/fl_2018.shp' using driver `ESRI Shapefile'
## Simple feature collection with 6152 features and 21 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -87.6349 ymin: 24.39631 xmax: -79.97431 ymax: 31.00097
## Geodetic CRS:  NAD83

Using mutate, create a vote share to fill precincts with (such as the Trump shae of the Trump + Biden vote)

govrace <- fl_2018 |>
  mutate(gillum = G18GOVDGIL / (G18GOVRDES + G18GOVDGIL))
fl_2018 <- st_make_valid(fl_2018)
govrace_hillsb <- govrace |>
  filter(COUNTY == "HIL")
ggplot() + 
  geom_sf(data = govrace_hillsb, aes(fill = gillum), color = "black", alpha = 0.7) + 
  theme_minimal() + 
  scale_fill_gradient2(low = "red", mid = "white", high = "blue", 
                        midpoint = 0.5, 
                        limits = c(0,1), 
                        na.value = "grey50", 
                        name = "2018 Andrew Gillum %") + 
  labs(title = "Hillsborough County")