Davidson County election analysis. Below I will show you a chart and graph showing how each precint in Davidson County, Tennessee voted in this past election. Vice President Kamala Harris ended up winning Davidson county by a wide margin,but the map shows that the race seemed closer than it actually was between Harris and Trump. I say that because their seems to be a lot more of the blue and red mixing together to make it seem like there was not that wide of a margin. The graph on the other hand shows that Harris won the county pretty handly.I say that because the colors do not mix together, and shows the discrepency of votes better. Harris won the county by a staggering 79,000 plus votes. 2024 Davidson County Election Data
# Required packages
if (!require("tidyverse"))
install.packages("tidyverse")
if (!require("mapview"))
install.packages("mapview")
if (!require("sf"))
install.packages("sf")
if (!require("leaflet"))
install.packages("leaflet")
if (!require("leaflet.extras2"))
install.packages("leaflet.extras2")
if (!require("plotly"))
install.packages("plotly")
library(tidyverse)
library(mapview)
library(sf)
library(leaflet)
library(leafpop)
library(readxl)
library(plotly)
# Getting the election data
VoteData <- read.csv("https://github.com/drkblake/Data/raw/refs/heads/main/Davidson_Vote_Data.csv")
# Getting the map
download.file("https://github.com/drkblake/Data/raw/refs/heads/main/Davidson_Precincts.zip","DavidsonPrecinctMap.zip")
unzip("DavidsonPrecinctMap.zip")
MapInfo <- read_sf("Davidson_Precincts.shp")
# Enhancing the data
VoteData2 <- VoteData %>%
mutate(Total = Trump + Harris + Other,
Pct_Trump = round((Trump / Total)*100,1),
Pct_Harris = round((Harris / Total)*100,1),
Pct_Other = round((Other / Total)*100,1),
Margin_Trump = Trump - Harris,
Margin_Harris = Harris - Trump,
Winner = case_when(Trump > Harris ~ "Trump",
Harris > Trump ~ "Harris",
Other > (Trump + Harris) ~ "Other",
.default = "Tie"))
# Merging the data and the map
DataAndMap <- left_join(VoteData2,MapInfo)
# Converting the merged data and map into an sf object
DataAndMap <- st_as_sf(DataAndMap)
# Trump percentage map
mypalette = colorRampPalette(c('blue', 'red'))
PctTrumpMap <- mapview(
DataAndMap,
zcol = "Pct_Trump",
col.regions = mypalette,
map.types = ("OpenStreetMap"),
layer.name = "Pct. Trump",
popup = popupTable(
DataAndMap,
feature.id = FALSE,
row.numbers = FALSE,
zcol = c(
"Precinct",
"Trump",
"Harris",
"Other",
"Total",
"Pct_Trump",
"Pct_Harris",
"Pct_Other",
"Margin_Trump",
"Margin_Harris",
"Winner"
)
)
)
PctTrumpMap
# Harris percentage map
mypalette = colorRampPalette(c('red', 'blue'))
PctHarrisMap <- mapview(
DataAndMap,
zcol = "Pct_Harris",
col.regions = mypalette,
map.types = ("OpenStreetMap"),
layer.name = "Pct. Harris",
popup = popupTable(
DataAndMap,
feature.id = FALSE,
row.numbers = FALSE,
zcol = c(
"Precinct",
"Trump",
"Harris",
"Other",
"Total",
"Pct_Trump",
"Pct_Harris",
"Pct_Other",
"Margin_Trump",
"Margin_Harris",
"Winner"
)
)
)
PctHarrisMap
# Sort the data by the Total variable
# and make it stay sorted in the chart
ChartData <- DataAndMap %>%
arrange(Total) %>%
mutate(Precinct = factor(Precinct, levels = Precinct))
# Create and format the chart
Chart <- plot_ly(data = ChartData, orientation = 'h') %>%
add_trace(
x = ~Trump,
y = ~Precinct,
name = 'Trump',
type = 'bar',
marker = list(color = 'red') # We have made the red bars
) %>%
add_trace(
x = ~Harris,
y = ~Precinct,
name = 'Harris',
type = 'bar',
marker = list(color = 'darkblue') # We have added the blue bars
) %>%
add_trace(
x = ~Other,
y = ~Precinct,
name = 'Other',
type = 'bar',
marker = list(color = 'gray') #We have added the gray bars
) %>%
add_trace(
x = ~Other,
y = ~Precinct,
type = 'bar',
name = '',
marker = list(color = 'rgba(0,0,0,0)'),
text = ~Winner,
textposition = 'outside',
showlegend = FALSE # We have added the "winner" labels
) %>%
layout(
barmode = 'stack',
xaxis = list(title = 'Number of Votes'),
yaxis = list(title = 'Precinct',
tickfont = list(size = 10),
automargin = TRUE)) # We have made it a stacked bar chart
# Show the plot
Chart
# Getting the election data
VoteData <- read.csv("https://github.com/drkblake/Data/raw/refs/heads/main/Davidson_Vote_Data.csv")
# Getting the map
download.file("https://github.com/drkblake/Data/raw/refs/heads/main/Davidson_Precincts.zip","DavidsonPrecinctMap.zip")
unzip("DavidsonPrecinctMap.zip")
MapInfo <- read_sf("Davidson_Precincts.shp")