If you’re running this for the first time, make sure to uncomment package installation and API key setting.

#install.packages(c("tidycensus", "sf", "tigris", "ggplot2", "viridis"))

# Load the packages
library(tidycensus)
library(sf)
## Linking to GEOS 3.12.2, GDAL 3.9.3, PROJ 9.4.1; sf_use_s2() is TRUE
library(tigris)
## To enable caching of data, set `options(tigris_use_cache = TRUE)`
## in your R script or .Rprofile.
library(ggplot2)
library(viridis)
## Loading required package: viridisLite
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyr)
library(leaflet)


#census_api_key("8d2dec7d58676dfb242321e6386d6b8431cf8899",install=TRUE)

R Markdown

IMPORTANT: Change the Variable

# county-level overcrowding data 
overcrowding_data <- get_acs(
  geography = "county",
  variables = c("B25001_001","B25014_005","B25014_006","B25014_007","B25014_011","B25014_012","B25014_013"), # Example variable for overcrowded households (you may need a different variable)
  year = 2020,
  survey = "acs5"
)
## Getting data from the 2016-2020 5-year ACS
overcrowding_data <- overcrowding_data %>% select(-moe) %>% spread(key = variable,value=estimate)
overcrowding_data <-  overcrowding_data %>% mutate(crowded = rowSums(select(.,4:9),na.rm=TRUE))
overcrowding_data <- overcrowding_data %>% select(1:3,10) 
overcrowding_data <- overcrowding_data %>% mutate(crowding_rate = crowded/B25001_001)

# https://github.com/GeoDaCenter/covid/blob/master/public/csv/covid_wk_pos_cdc.csv
coviddata <- read.csv("C:/Users/MBA/OneDrive - Emory University/MBA Program/Semester_24Fall/EH584 Public Health and Built Environment/Final/covid_wk_pos_cdc-2024-12-04.csv",colClasses = c("fips_code"="character"))
coviddata$fips_code<-stringr::str_pad(coviddata$fips_code,width=5, side = "left", pad="0")
coviddata_selected <- coviddata[,c(1,200:350)]
coviddata_selected <- coviddata_selected %>%
  mutate(positivity_avg = rowMeans(select(.,2:152),na.rm=TRUE))
coviddata_selected <- coviddata_selected[,c(1,153)]
# Load shapefiles for us counties
counties_sf <- counties(cb = TRUE)
## Retrieving data for the year 2022
##   |                                                                              |                                                                      |   0%  |                                                                              |                                                                      |   1%  |                                                                              |=                                                                     |   1%  |                                                                              |=                                                                     |   2%  |                                                                              |==                                                                    |   3%  |                                                                              |===                                                                   |   4%  |                                                                              |===                                                                   |   5%  |                                                                              |====                                                                  |   6%  |                                                                              |=====                                                                 |   7%  |                                                                              |=====                                                                 |   8%  |                                                                              |======                                                                |   8%  |                                                                              |======                                                                |   9%  |                                                                              |=======                                                               |  10%  |                                                                              |========                                                              |  11%  |                                                                              |=========                                                             |  12%  |                                                                              |==========                                                            |  14%  |                                                                              |==========                                                            |  15%  |                                                                              |===========                                                           |  16%  |                                                                              |============                                                          |  17%  |                                                                              |=============                                                         |  19%  |                                                                              |==============                                                        |  20%  |                                                                              |===============                                                       |  21%  |                                                                              |================                                                      |  22%  |                                                                              |================                                                      |  24%  |                                                                              |=================                                                     |  25%  |                                                                              |==================                                                    |  26%  |                                                                              |===================                                                   |  27%  |                                                                              |====================                                                  |  29%  |                                                                              |=====================                                                 |  30%  |                                                                              |======================                                                |  31%  |                                                                              |=======================                                               |  32%  |                                                                              |=======================                                               |  33%  |                                                                              |========================                                              |  35%  |                                                                              |=========================                                             |  36%  |                                                                              |==========================                                            |  37%  |                                                                              |===========================                                           |  38%  |                                                                              |============================                                          |  40%  |                                                                              |=============================                                         |  41%  |                                                                              |=============================                                         |  42%  |                                                                              |==============================                                        |  43%  |                                                                              |===============================                                       |  45%  |                                                                              |================================                                      |  46%  |                                                                              |=================================                                     |  47%  |                                                                              |==================================                                    |  48%  |                                                                              |===================================                                   |  49%  |                                                                              |====================================                                  |  51%  |                                                                              |====================================                                  |  52%  |                                                                              |=====================================                                 |  53%  |                                                                              |======================================                                |  54%  |                                                                              |=======================================                               |  56%  |                                                                              |========================================                              |  57%  |                                                                              |=========================================                             |  58%  |                                                                              |==========================================                            |  59%  |                                                                              |==========================================                            |  61%  |                                                                              |===========================================                           |  62%  |                                                                              |============================================                          |  63%  |                                                                              |=============================================                         |  64%  |                                                                              |==============================================                        |  66%  |                                                                              |===============================================                       |  67%  |                                                                              |================================================                      |  68%  |                                                                              |================================================                      |  69%  |                                                                              |=================================================                     |  70%  |                                                                              |==================================================                    |  72%  |                                                                              |===================================================                   |  73%  |                                                                              |====================================================                  |  74%  |                                                                              |=====================================================                 |  75%  |                                                                              |======================================================                |  77%  |                                                                              |=======================================================               |  78%  |                                                                              |=======================================================               |  79%  |                                                                              |========================================================              |  80%  |                                                                              |=========================================================             |  82%  |                                                                              |==========================================================            |  83%  |                                                                              |===========================================================           |  84%  |                                                                              |============================================================          |  85%  |                                                                              |=============================================================         |  87%  |                                                                              |=============================================================         |  88%  |                                                                              |==============================================================        |  89%  |                                                                              |===============================================================       |  90%  |                                                                              |================================================================      |  91%  |                                                                              |=================================================================     |  93%  |                                                                              |==================================================================    |  94%  |                                                                              |===================================================================   |  95%  |                                                                              |===================================================================   |  96%  |                                                                              |====================================================================  |  98%  |                                                                              |===================================================================== |  99%  |                                                                              |======================================================================| 100%
# Merge the overcrowding data with the county shapefile by GEOID (FIPS code)
overcrowding_sf1 <- left_join(counties_sf, overcrowding_data, by = c("GEOID" = "GEOID"))
overcrowding_sf <- left_join(overcrowding_sf1, coviddata_selected, by = c("GEOID" = "fips_code"))
# Reproject the shapefile to WGS84 (EPSG:4326)
overcrowding_sf <- st_transform(overcrowding_sf, crs = 4326)
# Create a color palette for the overcrowding rate
pal <- colorNumeric(palette = "YlGnBu", domain = overcrowding_sf$positivity_avg)


# Create the interactive map using leaflet
leaflet(data = overcrowding_sf) %>%
  addTiles() %>%  
  addPolygons(
    fillColor = "white",  
    fillOpacity = .8,
    color = ~pal(positivity_avg),  
    weight = ~crowding_rate/0.005,  
    popup = ~paste(
      "County: ", NAME.x, "<br>",
      "Overcrowding Rate: ", crowding_rate, "<br>","pos ",positivity_avg
    ),opacity=1
  ) %>%
  addLegend(
    pal = pal, 
    values = ~positivity_avg, 
    title = "Rate",
    position = "bottomright"
  )