Lab 7

This code generates a code of ages 19-64 that is in labor force and employed but has no health insurance coverage. It displays includes subdivison in Davidson County, as well as Nashville.

Code:

# 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("5ef79a6f4380ae0dfefc4c256e98d623316f159a")

# 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