title: “hawkelection” output: html_document date: “2026-04-17” —


🗳️ Load Rutherford County Vote Data

VoteData_RuCo <- read.csv(
  "https://github.com/drkblake/Data/raw/refs/heads/main/Rutherford_Vote_Data.csv"
)

🗺️ Load Rutherford County Shapefile

download.file(
  "https://github.com/drkblake/Data/raw/refs/heads/main/Rutherford_Precincts.zip",
  "RutherfordPrecinctMap.zip",
  mode = "wb"
)

unzip("RutherfordPrecinctMap.zip")

MapInfo_RuCo <- read_sf("Rutherford_Precincts.shp") %>%
  select(Precinct, geometry)

🔗 Join Rutherford Data + Map

DataAndMapRuCo <- left_join(
  VoteData_RuCo,
  MapInfo_RuCo,
  by = "Precinct"
) %>%
  st_as_sf() %>%
  st_transform(4326)

🗳️ Load Davidson County Vote Data

VoteData_Davidson <- read.csv(
  "https://github.com/drkblake/Data/raw/refs/heads/main/Davidson_Vote_Data.csv"
)

🗺️ Load Davidson County Shapefile

download.file(
  "https://github.com/drkblake/Data/raw/refs/heads/main/Davidson_Precincts.zip",
  "DavidsonPrecinctMap.zip",
  mode = "wb"
)

unzip("DavidsonPrecinctMap.zip")

MapInfo_Davidson <- read_sf("Davidson_Precincts.shp") %>%
  select(Precinct, geometry)

🔗 Join Davidson Data + Map

DataAndMapDavidson <- left_join(
  VoteData_Davidson,
  MapInfo_Davidson,
  by = "Precinct"
) %>%
  st_as_sf() %>%
  st_transform(4326)

🧩 Combine Both Counties

DataAndMap <- DataAndMapRuCo %>%
  bind_rows(DataAndMapDavidson) %>%
  st_as_sf() %>%
  st_transform(4326)

📊 Compute Vote Totals, Shares, Winner, Margin

MarginData <- DataAndMap %>%
  mutate(
    Total = Trump + Harris + Other,
    Pct_Trump  = (Trump  / Total) * 100,
    Pct_Harris = (Harris / Total) * 100
  ) %>%
  mutate(
    Winner = case_when(
      Trump > Harris ~ "Republican",
      Harris > Trump ~ "Democratic",
      TRUE           ~ "Tie"
    ),
    Margin_Pct = round(abs(Pct_Trump - Pct_Harris), 1)
  )

🎨 Color Palettes

dem_pal <- colorNumeric(
  palette = c("#deebf7", "#08519c"),
  domain  = MarginData$Margin_Pct
)

rep_pal <- colorNumeric(
  palette = c("#fee0d2", "#99000d"),
  domain  = MarginData$Margin_Pct
)

🎨 Assign Fill Colors

MarginData <- MarginData %>%
  mutate(
    FillColor = case_when(
      Winner == "Democratic" ~ dem_pal(Margin_Pct),
      Winner == "Republican" ~ rep_pal(Margin_Pct),
      TRUE                   ~ "gray"
    )
  )

🗺️ TRUE Margin Choropleth Map

TrueMarginMap <- leaflet(MarginData) %>%
  addProviderTiles("CartoDB.Positron") %>%
  addPolygons(
    fillColor   = ~FillColor,
    fillOpacity = 0.7,
    color       = "black",
    weight      = 0.4,
    popup       = popup_content
  ) %>%
  addLegend(
    colors = c("#08519c", "#99000d"),
    labels = c("Democratic winner", "Republican winner"),
    title  = "Winner (by margin)",
    opacity = 1
  )

TrueMarginMap