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","paletteer"))
# 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)
library(paletteer)
#census_api_key("8d2dec7d58676dfb242321e6386d6b8431cf8899",install=TRUE)
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_selected1 <- coviddata[,c(1,286:336)]
coviddata_selected <- coviddata_selected1 %>%
mutate(positivity_avg = rowMeans(select(.,2:52),na.rm=TRUE))
coviddata_selected <- coviddata_selected[,c(1,53)]
# Load shapefiles for us counties
counties_sf <- counties(cb = TRUE)
## Retrieving data for the year 2022
## | | | 0% | | | 1% | |= | 1% | |= | 2% | |== | 2% | |== | 3% | |=== | 4% | |==== | 5% | |==== | 6% | |===== | 7% | |===== | 8% | |====== | 9% | |======= | 10% | |======== | 12% | |========= | 13% | |========== | 14% | |=========== | 15% | |============ | 17% | |============ | 18% | |============= | 19% | |============== | 20% | |=============== | 21% | |================ | 23% | |================= | 24% | |================== | 25% | |================== | 26% | |=================== | 28% | |==================== | 29% | |===================== | 30% | |====================== | 31% | |======================= | 33% | |======================== | 34% | |========================= | 35% | |========================= | 36% | |========================== | 37% | |=========================== | 39% | |============================ | 40% | |============================= | 41% | |============================== | 42% | |=============================== | 44% | |=============================== | 45% | |================================ | 46% | |================================= | 47% | |================================== | 49% | |=================================== | 50% | |==================================== | 51% | |===================================== | 52% | |===================================== | 54% | |====================================== | 55% | |======================================= | 56% | |======================================== | 57% | |========================================= | 58% | |========================================== | 60% | |=========================================== | 61% | |============================================ | 62% | |============================================ | 63% | |============================================= | 65% | |============================================== | 66% | |=============================================== | 67% | |================================================ | 68% | |================================================= | 70% | |================================================== | 71% | |================================================== | 72% | |=================================================== | 73% | |==================================================== | 75% | |===================================================== | 76% | |====================================================== | 77% | |======================================================= | 78% | |======================================================== | 79% | |======================================================== | 81% | |========================================================= | 82% | |========================================================== | 83% | |=========================================================== | 84% | |============================================================ | 86% | |============================================================= | 87% | |============================================================== | 88% | |=============================================================== | 89% | |=============================================================== | 91% | |================================================================ | 92% | |================================================================= | 93% | |================================================================== | 94% | |=================================================================== | 95% | |==================================================================== | 97% | |===================================================================== | 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
n_colors<-100
pal <- colorNumeric(palette = "viridis", domain = overcrowding_sf$positivity_avg)
# Create the interactive map using leaflet
leaflet(data = overcrowding_sf) %>%
addProviderTiles("CartoDB.Positron") %>%
addPolygons(
fillColor = "white",
fillOpacity = ~ifelse(is.na(positivity_avg),0,0.8),
color = ~ifelse(is.na(positivity_avg),"white",pal(positivity_avg)),
weight = ~crowding_rate/0.005,
popup = ~paste(
"County: ", NAME.x, "<br>",
"Overcrowding Rate: ", crowding_rate, "<br>","pos ",positivity_avg
),opacity=~ifelse(is.na(positivity_avg),0,1)
) %>%
addLegend(
pal = pal,
values = ~positivity_avg,
title = "Rate",
position = "bottomright"
)