Here is the solution code for the Week 10 Lab assignment:

# 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")

library(tidyverse)
library(tidycensus)
library(sf)
library(mapview)

# Transmitting API key

# census_api_key("PasteYourAPIKeyBetweenTheseQuoteMarks")

# Fetching ACS codebooks

DetailedTables <- load_variables(2022, "acs5", cache = TRUE)
SubjectTables <- load_variables(2022, "acs5/subject", cache = TRUE)
ProfileTables <- load_variables(2022, "acs5/profile", cache = TRUE)

# Double checking target variables

ChosenVars <- filter(ProfileTables,name == "DP04_0063P"|
                       name == "DP02_0001")
print(ChosenVars$name)
print(ChosenVars$label)
print(ChosenVars$concept)

# Specifying target variables

VariableList = 
  c(PctGasHeat_ = "DP04_0063P",
    Households_ = "DP02_0001")

# Fetching data

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

# Reformatting data

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

# Filtering data

filtereddata <- mydata %>% 
  filter(County == "Davidson"|
           County == "Rutherford"|
           County == "Williamson"|
           County == "Cheatham"|
           County == "Robertson"|
           County == "Sumner"|
           County == "Wilson")

# Plotting data

ggplot(filtereddata, aes(x = PctGasHeat_E, y = reorder(County, PctGasHeat_E))) + 
  geom_errorbarh(aes(xmin = PctGasHeat_E - PctGasHeat_M, xmax = PctGasHeat_E + PctGasHeat_M)) + 
  geom_point(size = 3, color = "darkblue") + 
  theme_minimal(base_size = 12.5) + 
  labs(title = "Pct. households with gas heat", 
       subtitle = "Nashville-area counties. Brackets show error margins.", 
       x = "2018-2022 ACS estimate", 
       y = "")

# Mapping data

mapdata <- filtereddata %>% 
  rename(PctGasHeat = PctGasHeat_E,
         Households = Households_E)

mapdata <- st_as_sf(mapdata)

mapviewOptions(basemaps.color.shuffle = FALSE)
mapview(mapdata, zcol = "PctGasHeat",
        layer.name = "Pct. with gas heat",
        popup = TRUE)

# Better map

# Installing and activating some additional packages

if (!require("leaflet")) install.packages("leaflet")
if (!require("leaflet.extras2")) install.packages("leaflet.extras2")

library(leaflet)
library(leaflet.extras2)
library(leafpop)

# Defining a custome color palette for the map

mypalette = colorRampPalette(c('lightgray', 'darkorange'))

# Generating the map

mapview(mapdata, zcol = "PctGasHeat",
        col.regions = mypalette, at = seq(10, 60, 10),
        layer.name = "Pct. gas heat",
        popup = popupTable(mapdata,
                           feature.id = FALSE,
                           row.numbers = FALSE,
                           zcol = c("County",
                                    "State",
                                    "PctGasHeat",
                                    "Households")))

# Exporting data in .csv format

CSVdata <- st_drop_geometry(mapdata)
write.csv(CSVdata, "mydata.csv", row.names = FALSE)

And here is the code plus its output:

# 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")

library(tidyverse)
library(tidycensus)
library(sf)
library(mapview)

# Transmitting API key

# census_api_key("PasteYourAPIKeyBetweenTheseQuoteMarks")

# Fetching ACS codebooks

DetailedTables <- load_variables(2022, "acs5", cache = TRUE)
SubjectTables <- load_variables(2022, "acs5/subject", cache = TRUE)
ProfileTables <- load_variables(2022, "acs5/profile", cache = TRUE)

# Double checking target variables

ChosenVars <- filter(ProfileTables,name == "DP04_0063P"|
                       name == "DP02_0001")
print(ChosenVars$name)
## [1] "DP02_0001"  "DP04_0063P"
print(ChosenVars$label)
## [1] "Estimate!!HOUSEHOLDS BY TYPE!!Total households"                  
## [2] "Percent!!HOUSE HEATING FUEL!!Occupied housing units!!Utility gas"
print(ChosenVars$concept)
## [1] "Selected Social Characteristics in the United States"
## [2] "Selected Housing Characteristics"
# Specifying target variables

VariableList = 
  c(PctGasHeat_ = "DP04_0063P",
    Households_ = "DP02_0001")

# Fetching data

mydata <- get_acs(
  geography = "county",
  state = "TN",
  variables = VariableList,
  year = 2022,
  survey = "acs5",
  output = "wide",
  geometry = TRUE)
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |                                                                      |   1%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |==                                                                    |   2%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |===                                                                   |   5%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |========                                                              |  12%
  |                                                                            
  |=========                                                             |  12%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |==========                                                            |  14%
  |                                                                            
  |==========                                                            |  15%
  |                                                                            
  |===========                                                           |  15%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |=============                                                         |  18%
  |                                                                            
  |==============                                                        |  21%
  |                                                                            
  |===============                                                       |  22%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |=================                                                     |  24%
  |                                                                            
  |==================                                                    |  25%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |======================                                                |  31%
  |                                                                            
  |=========================                                             |  36%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |=============================                                         |  41%
  |                                                                            
  |===================================                                   |  49%
  |                                                                            
  |====================================                                  |  51%
  |                                                                            
  |=====================================                                 |  53%
  |                                                                            
  |========================================                              |  57%
  |                                                                            
  |=========================================                             |  59%
  |                                                                            
  |============================================                          |  63%
  |                                                                            
  |===============================================                       |  68%
  |                                                                            
  |=================================================                     |  69%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |==================================================                    |  72%
  |                                                                            
  |====================================================                  |  74%
  |                                                                            
  |======================================================                |  78%
  |                                                                            
  |=======================================================               |  78%
  |                                                                            
  |=========================================================             |  81%
  |                                                                            
  |===========================================================           |  84%
  |                                                                            
  |============================================================          |  85%
  |                                                                            
  |=============================================================         |  87%
  |                                                                            
  |=============================================================         |  88%
  |                                                                            
  |==============================================================        |  89%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |================================================================      |  91%
  |                                                                            
  |=================================================================     |  92%
  |                                                                            
  |=================================================================     |  93%
  |                                                                            
  |==================================================================    |  94%
  |                                                                            
  |===================================================================   |  95%
  |                                                                            
  |====================================================================  |  98%
  |                                                                            
  |===================================================================== |  99%
  |                                                                            
  |======================================================================| 100%
# Reformatting data

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

# Filtering data

filtereddata <- mydata %>% 
  filter(County == "Davidson"|
           County == "Rutherford"|
           County == "Williamson"|
           County == "Cheatham"|
           County == "Robertson"|
           County == "Sumner"|
           County == "Wilson")

# Plotting data

ggplot(filtereddata, aes(x = PctGasHeat_E, y = reorder(County, PctGasHeat_E))) + 
  geom_errorbarh(aes(xmin = PctGasHeat_E - PctGasHeat_M, xmax = PctGasHeat_E + PctGasHeat_M)) + 
  geom_point(size = 3, color = "darkblue") + 
  theme_minimal(base_size = 12.5) + 
  labs(title = "Pct. households with gas heat", 
       subtitle = "Nashville-area counties. Brackets show error margins.", 
       x = "2018-2022 ACS estimate", 
       y = "")

# Mapping data

mapdata <- filtereddata %>% 
  rename(PctGasHeat = PctGasHeat_E,
         Households = Households_E)

mapdata <- st_as_sf(mapdata)

mapviewOptions(basemaps.color.shuffle = FALSE)
mapview(mapdata, zcol = "PctGasHeat",
        layer.name = "Pct. with gas heat",
        popup = TRUE)
# Better map

# Installing and activating some additional packages

if (!require("leaflet")) install.packages("leaflet")
if (!require("leaflet.extras2")) install.packages("leaflet.extras2")

library(leaflet)
library(leaflet.extras2)
library(leafpop)

# Defining a custome color palette for the map

mypalette = colorRampPalette(c('lightgray', 'darkorange'))

# Generating the map

mapview(mapdata, zcol = "PctGasHeat",
        col.regions = mypalette, at = seq(10, 60, 10),
        layer.name = "Pct. gas heat",
        popup = popupTable(mapdata,
                           feature.id = FALSE,
                           row.numbers = FALSE,
                           zcol = c("County",
                                    "State",
                                    "PctGasHeat",
                                    "Households")))
# Exporting data in .csv format

CSVdata <- st_drop_geometry(mapdata)
write.csv(CSVdata, "mydata.csv", row.names = FALSE)