R Spaital Lab Assignment #3

# load in RData file from previous assignments with processed datasets as sf objects
load("C:/Users/jdhoc/Desktop/Hunter/SPRING 2024/GEOG 39300 R/R-spatial/Session_7/data/assignment1_data.RData")
load('C:/Users/jdhoc/Desktop/Hunter/SPRING 2024/GEOG 39300 R/R-spatial/Session_8/week8_data.RData')

# read in covid acs data
acs_covid <- sf::st_read('C:/Users/jdhoc/Desktop/Hunter/SPRING 2024/GEOG 39300 R/R-spatial/Session_9/acsPopByZip.gpkg', layer="NYC_COVID_Pop_by_Zip")
## Reading layer `NYC_COVID_Pop_by_Zip' from data source 
##   `C:\Users\jdhoc\Desktop\Hunter\SPRING 2024\GEOG 39300 R\R-spatial\Session_9\acsPopByZip.gpkg' 
##   using driver `GPKG'
## Simple feature collection with 180 features and 13 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 913129 ymin: 120020.9 xmax: 1067113 ymax: 272710.9
## Projected CRS: NAD83 / New York Long Island (ftUS)

task 2: Use ggplot2 and other ggplot-compatible packages to create a multi-map figure illustrating the possible relationship between COVID-19 confirmed cases or rate and another factor (e.g., the number of nursing homes, number of food stores, neighborhood racial composition, elderly population, etc.). The maps should be put side by side on one single page. Add graticule to at least one of those maps and label some of the feature on the map where applicable and appropriate.

# calculate population density per sq mi
acs_covid$area <- st_area(acs_covid) %>% set_units(mi^2) %>% as.numeric()
acs_covid$pop_density <- acs_covid$POPULAT / acs_covid$area

# get breaks for density levels
breaks_lmh <- classIntervals(acs_covid$pop_density, n = 3, style = "quantile")
breaks_lmh$brks[1] %<>% magrittr::subtract(0.0001)
acs_covid %<>% mutate(density_lvl = cut(pop_density, breaks_lmh$brks, labels = c('Low','Medium','High')))

pop_dens_map <- ggplot(acs_covid) +
  geom_sf(aes(fill = density_lvl), color = "white") +
  scale_fill_brewer(palette = "Greens", name='Population Density') + 
  labs(title = "ZIP Population Density") +
  theme(legend.position = 'bottom',
        legend.direction = 'vertical')

# get breaks for covid positive levels
breaks_lmh <- classIntervals(acs_covid$Positiv, n = 3, style = "quantile")
breaks_lmh$brks[1] %<>% magrittr::subtract(0.0001)
acs_covid %<>% mutate(pos_lvl = cut(Positiv, breaks_lmh$brks, labels = c('Low','Medium','High')))

# edit covid pos map from previous task to fit with pop dens map next to it
covid_pos_map <- ggplot(acs_covid) +
  geom_sf(aes(fill = pos_lvl), color = "white") +
  scale_fill_brewer(palette = "YlOrRd", name='Positive Tests') +
  labs(title = "Positive COVID Tests by ZIP") +
  theme(legend.position = 'bottom',
        legend.direction = 'vertical')

ggarrange(covid_pos_map, pop_dens_map)

task 3: Create a web-based interactive map for COIVD-19 data using tmap, mapview, or leaflet package and save it as a HTML file.

acs_covid <- st_transform(acs_covid, 4326)

pal_fun <- colorQuantile("Oranges", NULL, n = 4)
p_tip <- paste0("ZIP Code: ", acs_covid$ZIPCODE)
p_popup <- paste0("<strong>COVID Tests with Positive Result: </strong>", 
                  (acs_covid$perc_pos * 100) %>% round(1), "%", "<br/>",
                  sep = "")
breaks_qt <- classIntervals(c(min(acs_covid$perc_pos) - .00001, acs_covid$perc_pos), n = 4, style = "quantile")

htmlMap <- leaflet() %>%
  addTiles() %>%
  addPolygons(
    data = acs_covid,
    stroke = FALSE,
    fillColor = ~pal_fun(perc_pos), # set fill color with function from above and value
    fillOpacity = 0.8, smoothFactor = 0.5, # make it nicer
    popup = p_popup,  # add popup
    label = p_tip) %>% 
  addLegend("bottomright",
            colors = brewer.pal(4, "Oranges"), 
            labels = paste0("up to ", format(breaks_qt$brks[-1], digits = 2)),
            title = "NYC Percent of COVID Tests that are Positive")
htmlMap
# save as html file
htmlwidgets::saveWidget(htmlMap, 'nyc_percCovidPos_leaflet.html')