This interactive map and graphic illustrate the percentage of the civilian noninstitutionalized population, ages 19 to 64, who are employed and in the labor force but lack health insurance coverage in Davidson County, TN.
# Installing and loading required packages
if (!require("tidyverse")) install.packages("tidyverse")
if (!require("tidycensus")) install.packages("tidycensus")
if (!require("sf")) install.packages("sf")
if (!require("mapview")) install.packages("mapview")
if (!require("leaflet")) install.packages("leaflet")
if (!require("leaflet.extras2")) install.packages("leaflet.extras2")
library(tidyverse)
library(tidycensus)
library(sf)
library(mapview)
library(leaflet)
library(leafpop)
# Transmitting API key
census_api_key("4718a693b0b1bcb7e4b57832f923f7e48a0323a1")
# Fetching ACS codebooks
ProfileTables <- load_variables(2023, "acs5/profile", cache = TRUE)
# Specifying target variables
VariableList =
c(Estimate_ = "DP03_0108P")
# Fetching data
mydata <- get_acs(
geography = "county subdivision",
state = "TN",
variables = VariableList,
year = 2023,
survey = "acs5",
output = "wide",
geometry = TRUE)
# Reformatting data
mydata <-
separate_wider_delim(mydata,
NAME,
delim = ", ",
names = c("Division", "County", "State"))
# Filtering data
filtereddata <- mydata %>%
filter(County %in% c("Davidson County"))
# Plotting data
mygraph <- ggplot(filtereddata, aes(x = Estimate_E, y = reorder(Division, Estimate_E))) +
geom_errorbarh(aes(xmin = Estimate_E - Estimate_M, xmax = Estimate_E + Estimate_M)) +
geom_point(size = 3, color = "#099d91") +
theme_minimal(base_size = 12.5) +
labs(title = "Estimates by area",
subtitle = "County subdivisions. Brackets show error margins.",
x = "2019-2023 ACS estimate",
y = "")
mygraph
# Mapping data
mapdata <- filtereddata %>%
rename(Estimate = Estimate_E,
Range = Estimate_M)
mapdata <- st_as_sf(mapdata)
mapviewOptions(basemaps.color.shuffle = FALSE)
DivisionMap <- mapview(mapdata,
zcol = "Estimate",
layer.name = "Estimate",
popup = popupTable(
mapdata,
feature.id = FALSE,
row.numbers = FALSE,
zcol = c(
"State",
"County",
"Division",
"Estimate",
"Range")))
DivisionMap