title: “hawkelection” output: html_document date: “2026-04-17” —
VoteData_RuCo <- read.csv(
"https://github.com/drkblake/Data/raw/refs/heads/main/Rutherford_Vote_Data.csv"
)
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)
DataAndMapRuCo <- left_join(
VoteData_RuCo,
MapInfo_RuCo,
by = "Precinct"
) %>%
st_as_sf() %>%
st_transform(4326)
VoteData_Davidson <- read.csv(
"https://github.com/drkblake/Data/raw/refs/heads/main/Davidson_Vote_Data.csv"
)
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)
DataAndMapDavidson <- left_join(
VoteData_Davidson,
MapInfo_Davidson,
by = "Precinct"
) %>%
st_as_sf() %>%
st_transform(4326)
DataAndMap <- DataAndMapRuCo %>%
bind_rows(DataAndMapDavidson) %>%
st_as_sf() %>%
st_transform(4326)
dem_pal <- colorNumeric(
palette = c("#deebf7", "#08519c"),
domain = MarginData$Margin_Pct
)
rep_pal <- colorNumeric(
palette = c("#fee0d2", "#99000d"),
domain = MarginData$Margin_Pct
)
MarginData <- MarginData %>%
mutate(
FillColor = case_when(
Winner == "Democratic" ~ dem_pal(Margin_Pct),
Winner == "Republican" ~ rep_pal(Margin_Pct),
TRUE ~ "gray"
)
)
PopupData <- MarginData %>%
st_drop_geometry() %>%
select(
County,
Precinct,
Winner,
Trump,
Harris,
Other,
`Margin (percentage points)` = Margin_Pct
)
popup_content <- popupTable(
PopupData,
feature.id = FALSE,
row.numbers = FALSE
)
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