In my view, I believe minorities are being unintentionally harmed by the new districts. Both political parties are simply trying to make sure they get as many of their voters in the right sports to hold the most house seats. In this case, we live in a very conservative state, so naturally minorities are going to be disproportionately misplaced due to their political affiliation.






## `geom_smooth()` using formula = 'y ~ x'


Code:

# ----------------------------------------------------------
# Load required libraries
# ----------------------------------------------------------

library(tidyverse)
library(tidycensus)
library(sf)
library(leaflet)
library(plotly)

# ----------------------------------------------------------
# Census API key (only needed once per session)
# ----------------------------------------------------------

census_api_key("904ea9b9c6b5f8f7d6b1d4846a59ee8cc3b48d1f", overwrite = TRUE)

# ----------------------------------------------------------
# Use your variable
# ----------------------------------------------------------

VariableList <- c(
  PercentWhite = "DP05_0037P"
)

# ----------------------------------------------------------
# Fetch ACS data (Congressional districts, TN, 2022)
# ----------------------------------------------------------

mydata <- get_acs(
  geography = "congressional district",
  state = "TN",
  variables = VariableList,
  year = 2022,
  survey = "acs1",
  output = "wide",
  geometry = TRUE
)

# ----------------------------------------------------------
# Clean data
# ----------------------------------------------------------

mydata <- mydata %>%
  rename(Area = NAME)

filtereddata <- mydata   # keep all districts

# ----------------------------------------------------------
# Prepare map data
# ----------------------------------------------------------

mapdata <- filtereddata %>%
  rename(
    Estimate = PercentWhiteE,
    Range = PercentWhiteM
  ) %>%
  st_as_sf()

mapdata <- st_transform(mapdata, 4326)

# ----------------------------------------------------------
# Color palette
# ----------------------------------------------------------

qs <- quantile(mapdata$Estimate, probs = seq(0, 1, length.out = 6), na.rm = TRUE)

pal <- colorBin(
  palette = "Blues",
  domain = mapdata$Estimate,
  bins = qs,
  pretty = FALSE
)

# ----------------------------------------------------------
# Dot plot with error bars
# ----------------------------------------------------------

plotdf <- filtereddata %>%
  st_drop_geometry() %>%
  mutate(
    point_color = pal(PercentWhiteE),
    y_ordered   = reorder(Area, PercentWhiteE),
    hover_text  = paste0("District: ", Area)
  )

mygraph <- plot_ly(
  data = plotdf,
  x = ~PercentWhiteE,
  y = ~as.character(y_ordered),
  type = "scatter",
  mode = "markers",
  showlegend = FALSE,
  marker = list(
    color = ~point_color,
    size  = 8
  ),
  error_x = list(
    type       = "data",
    array      = ~PercentWhiteM,
    arrayminus = ~PercentWhiteM
  ),
  text = ~hover_text,
  hovertemplate = "%{text}<br>%{x:.1f}%<extra></extra>"
) %>%
  layout(
    title = "Percent White Population by Congressional District (2022)",
    xaxis = list(title = "Percent White Population"),
    yaxis = list(title = "")
  )

mygraph

# ----------------------------------------------------------
# Map popup labels
# ----------------------------------------------------------

mapdata$popup <- paste0(
  "<strong>", mapdata$Area, "</strong><br/>",
  "<hr>",
  "Percent White: ", round(mapdata$Estimate, 1), "%<br/>",
  "Margin of Error: ±", round(mapdata$Range, 1)
)

# ----------------------------------------------------------
# Leaflet map
# ----------------------------------------------------------

DivisionMap <- leaflet(mapdata) %>%
  addProviderTiles(providers$CartoDB.Positron) %>%
  addPolygons(
    fillColor   = ~pal(Estimate),
    fillOpacity = 0.6,
    color       = "black",
    weight      = 1,
    popup       = ~popup
  ) %>%
  addLegend(
    pal = pal,
    values = ~Estimate,
    title = "% White Population"
  )

DivisionMap
#| echo: FALSE

data2022 <- get_acs(
  geography = "congressional district",
  state = "TN",
  variables = VariableList,
  year = 2022,
  survey = "acs1",
  output = "wide",
  geometry = TRUE
) %>%
  rename(Area = NAME)

mapdata2022 <- data2022 %>%
  rename(Estimate = PercentWhiteE, Range = PercentWhiteM) %>%
  st_transform(4326)

pal2022 <- colorBin(
  "Blues",
  domain = mapdata2022$Estimate,
  bins = quantile(mapdata2022$Estimate, probs = seq(0,1,length.out=6), na.rm=TRUE),
  pretty = FALSE
)

plotdf2022 <- data2022 %>%
  st_drop_geometry() %>%
  mutate(y_ordered = reorder(Area, PercentWhiteE))

graph2022 <- plot_ly(
  data = plotdf2022,
  x = ~PercentWhiteE,
  y = ~as.character(y_ordered),
  type = "scatter",
  mode = "markers",
  marker = list(size = 10),
  error_x = list(
    type = "data",
    array = ~PercentWhiteM,
    arrayminus = ~PercentWhiteM
  )
) %>%
  layout(
    title = "Percent White Population by Congressional District (2022)",
    xaxis = list(title = "Percent White Population", ticksuffix = "%"),
    yaxis = list(title = "", categoryarray = levels(plotdf2022$y_ordered)),
    margin = list(l = 200)
  )

mapdata2022$popup <- paste0(
  "<strong>", mapdata2022$Area, "</strong><br/>",
  "Percent White: ", round(mapdata2022$Estimate,1), "%"
)

map2022 <- leaflet(mapdata2022) %>%
  addProviderTiles(providers$CartoDB.Positron) %>%
  addPolygons(
    fillColor = ~pal2022(Estimate),
    fillOpacity = 0.6,
    color = "black",
    weight = 1,
    popup = ~popup
  )
# ----------------------------------------------------------
# Load required libraries
# ----------------------------------------------------------

library(tidyverse)
library(tidycensus)
library(sf)
library(leaflet)
library(plotly)

# ----------------------------------------------------------
# Census API key (only needed once per session)
# ----------------------------------------------------------

census_api_key("904ea9b9c6b5f8f7d6b1d4846a59ee8cc3b48d1f", overwrite = TRUE)

# ----------------------------------------------------------
# Use your variable
# ----------------------------------------------------------

VariableList <- c(
  PercentWhite = "DP05_0037P"
)

# ----------------------------------------------------------
# Fetch ACS data (Congressional districts, TN, 2021)
# ----------------------------------------------------------

mydata <- get_acs(
  geography = "congressional district",
  state = "TN",
  variables = VariableList,
  year = 2021,
  survey = "acs1",
  output = "wide",
  geometry = TRUE
)

# ----------------------------------------------------------
# Clean data
# ----------------------------------------------------------

mydata <- mydata %>%
  rename(Area = NAME)

filtereddata <- mydata   # keep all districts

# ----------------------------------------------------------
# Prepare map data
# ----------------------------------------------------------

mapdata <- filtereddata %>%
  rename(
    Estimate = PercentWhiteE,
    Range = PercentWhiteM
  ) %>%
  st_as_sf()

mapdata <- st_transform(mapdata, 4326)

# ----------------------------------------------------------
# Color palette
# ----------------------------------------------------------

qs <- quantile(mapdata$Estimate, probs = seq(0, 1, length.out = 6), na.rm = TRUE)

pal <- colorBin(
  palette = "Blues",
  domain = mapdata$Estimate,
  bins = qs,
  pretty = FALSE
)

# ----------------------------------------------------------
# Dot plot with error bars
# ----------------------------------------------------------

plotdf <- filtereddata %>%
  st_drop_geometry() %>%
  mutate(
    point_color = pal(PercentWhiteE),
    y_ordered   = reorder(Area, PercentWhiteE),
    hover_text  = paste0("District: ", Area)
  )

mygraph <- plot_ly(
  data = plotdf,
  x = ~PercentWhiteE,
  y = ~as.character(y_ordered),
  type = "scatter",
  mode = "markers",
  showlegend = FALSE,
  marker = list(
    color = ~point_color,
    size  = 8
  ),
  error_x = list(
    type       = "data",
    array      = ~PercentWhiteM,
    arrayminus = ~PercentWhiteM
  ),
  text = ~hover_text,
  hovertemplate = "%{text}<br>%{x:.1f}%<extra></extra>"
) %>%
  layout(
    title = "Percent White Population by Congressional District (2022)",
    xaxis = list(title = "Percent White Population"),
    yaxis = list(title = "")
  )

mygraph

# ----------------------------------------------------------
# Map popup labels
# ----------------------------------------------------------

mapdata$popup <- paste0(
  "<strong>", mapdata$Area, "</strong><br/>",
  "<hr>",
  "Percent White: ", round(mapdata$Estimate, 1), "%<br/>",
  "Margin of Error: ±", round(mapdata$Range, 1)
)

# ----------------------------------------------------------
# Leaflet map
# ----------------------------------------------------------

DivisionMap <- leaflet(mapdata) %>%
  addProviderTiles(providers$CartoDB.Positron) %>%
  addPolygons(
    fillColor   = ~pal(Estimate),
    fillOpacity = 0.6,
    color       = "black",
    weight      = 1,
    popup       = ~popup
  ) %>%
  addLegend(
    pal = pal,
    values = ~Estimate,
    title = "% White Population"
  )

DivisionMap
#| echo: FALSE

library(tidyverse)
library(tidycensus)
library(sf)
library(leaflet)
library(plotly)

census_api_key("904ea9b9c6b5f8f7d6b1d4846a59ee8cc3b48d1f", overwrite = TRUE)

VariableList <- c(PercentWhite = "DP05_0037P")

data2021 <- get_acs(
  geography = "congressional district",
  state = "TN",
  variables = VariableList,
  year = 2021,
  survey = "acs1",
  output = "wide",
  geometry = TRUE
) %>%
  rename(Area = NAME)

mapdata2021 <- data2021 %>%
  rename(Estimate = PercentWhiteE, Range = PercentWhiteM) %>%
  st_transform(4326)

pal2021 <- colorBin(
  "Blues",
  domain = mapdata2021$Estimate,
  bins = quantile(mapdata2021$Estimate, probs = seq(0,1,length.out=6), na.rm=TRUE),
  pretty = FALSE
)

plotdf2021 <- data2021 %>%
  st_drop_geometry() %>%
  mutate(y_ordered = reorder(Area, PercentWhiteE))

graph2021 <- plot_ly(
  data = plotdf2021,
  x = ~PercentWhiteE,
  y = ~as.character(y_ordered),
  type = "scatter",
  mode = "markers",
  marker = list(size = 10),
  error_x = list(
    type = "data",
    array = ~PercentWhiteM,
    arrayminus = ~PercentWhiteM
  )
) %>%
  layout(
    title = "Percent White Population by Congressional District (2021)",
    xaxis = list(title = "Percent White Population", ticksuffix = "%"),
    yaxis = list(title = "", categoryarray = levels(plotdf2021$y_ordered)),
    margin = list(l = 200)
  )

mapdata2021$popup <- paste0(
  "<strong>", mapdata2021$Area, "</strong><br/>",
  "Percent White: ", round(mapdata2021$Estimate,1), "%"
)

map2021 <- leaflet(mapdata2021) %>%
  addProviderTiles(providers$CartoDB.Positron) %>%
  addPolygons(
    fillColor = ~pal2021(Estimate),
    fillOpacity = 0.6,
    color = "black",
    weight = 1,
    popup = ~popup
  )
#| echo: FALSE
#| message: FALSE
#| warning: FALSE

library(tidyverse)

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

scatterplot <- ggplot(scatter_data, aes(x = Pct_Nonwhite, y = Pct_Harris)) +
  geom_point(color = "#4C78A8", alpha = 0.8) +
  geom_smooth(method = "lm", se = FALSE, color = "black") +
  labs(
    title = "Pct. for Harris by Pct. Nonwhite",
    x = "Pct. Nonwhite",
    y = "Pct. for Harris"
  ) +
  theme_minimal()