## [1] "494018a70f114d4f76b10537730ccc9c7dbfe36b"
##   |                                                                              |                                                                      |   0%  |                                                                              |=                                                                     |   1%  |                                                                              |=                                                                     |   2%  |                                                                              |==                                                                    |   2%  |                                                                              |===                                                                   |   4%  |                                                                              |====                                                                  |   5%  |                                                                              |====                                                                  |   6%  |                                                                              |=====                                                                 |   7%  |                                                                              |=====                                                                 |   8%  |                                                                              |======                                                                |   8%  |                                                                              |=======                                                               |   9%  |                                                                              |=======                                                               |  10%  |                                                                              |========                                                              |  11%  |                                                                              |========                                                              |  12%  |                                                                              |=========                                                             |  13%  |                                                                              |==========                                                            |  14%  |                                                                              |==========                                                            |  15%  |                                                                              |===========                                                           |  15%  |                                                                              |============                                                          |  17%  |                                                                              |============                                                          |  18%  |                                                                              |===============                                                       |  21%  |                                                                              |===============                                                       |  22%  |                                                                              |==================                                                    |  25%  |                                                                              |===================                                                   |  28%  |                                                                              |====================                                                  |  29%  |                                                                              |=====================                                                 |  29%  |                                                                              |=====================                                                 |  30%  |                                                                              |======================                                                |  32%  |                                                                              |========================                                              |  34%  |                                                                              |========================                                              |  35%  |                                                                              |==========================                                            |  37%  |                                                                              |==========================                                            |  38%  |                                                                              |===========================                                           |  39%  |                                                                              |=============================                                         |  42%  |                                                                              |===============================                                       |  45%  |                                                                              |=================================                                     |  47%  |                                                                              |====================================                                  |  52%  |                                                                              |=====================================                                 |  53%  |                                                                              |=======================================                               |  56%  |                                                                              |=========================================                             |  58%  |                                                                              |=========================================                             |  59%  |                                                                              |============================================                          |  63%  |                                                                              |==============================================                        |  66%  |                                                                              |==================================================                    |  71%  |                                                                              |=======================================================               |  78%  |                                                                              |==========================================================            |  83%  |                                                                              |===========================================================           |  84%  |                                                                              |=================================================================     |  93%  |                                                                              |======================================================================| 100%
Pct. no insurance
DIVISION ESTIMATE_E
District 21 41.0
District 19 39.7
District 28 39.1
District 30 34.0
District 27 25.0

r echo=FALSE, message=FALSE, warning=FALSE
# 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")
if (!require("gt")) install.packages("gt")

library(tidyverse)
library(tidycensus)
library(sf)
library(mapview)
library(leaflet)
library(leafpop)
library(gt)

# Transmitting API key

census_api_key("494018a70f114d4f76b10537730ccc9c7dbfe36b", install = TRUE, overwrite = TRUE)

# Specifying target variables

VariableList = 
  c(Estimate_ = "DP03_0121P")  # Variable for employed 19-64 without health insurance

# Fetching data

mydata <- get_acs(
  geography = "county subdivision",
  state = "TN",
  county = "Davidson County",
  variables = VariableList,
  year = 2023,
  survey = "acs5",
  output = "wide",
  geometry = TRUE)

# Reformatting data

mydata <-
  separate_wider_delim(mydata,
                       NAME,
                       delim = ", ",
                       names = c("Division", "County", "State"))

filtereddata <- mydata %>% 
  filter(County %in% c("Davidson County"))

# Creating a Table

table_output <- filtereddata %>%
  select(Division, Estimate_E) %>%
  arrange(desc(Estimate_E)) %>%
  head(5) %>%
  gt() %>%
  cols_label(
    Division = "DIVISION",
    Estimate_E = "ESTIMATE_E"
  ) %>%
  tab_header(
    title = md("**Pct. no insurance**")
  )

table_output

# 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 = "Percentage of Employed Adults (19-64) Without Health Insurance", 
       subtitle = "Davidson County, TN - ACS 2023 Data", 
       x = "Percentage", 
       y = "Subdivision")

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