R Spatial Lab Assignment# 3

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, neighborhood racial composition, elderly population, etc.).

library(ggplot2)
library(patchwork)
library(viridis)


plot_a <- ggplot(map_data_final) +
  geom_sf(aes(fill = Positive), color = "white", size = 0.05) +
  scale_fill_viridis_c(option = "magma", name = "Positive\nCases") +
  labs(title = "A: COVID-19 Confirmed Cases",
       subtitle = "NYC DOHMH April 2020") +
  theme_void() +
  theme(legend.position = "bottom")


plot_b <- ggplot(map_data_final) +
  geom_sf(aes(fill = pct_elderly), color = "white", size = 0.05) +
  scale_fill_viridis_c(option = "mako", name = "% Pop\n65+") +
  labs(title = "B: Elderly Population Concentration",
       subtitle = "ACS 5-Year Estimates (2018)") +
  theme_void() +
  theme(legend.position = "bottom")


task2_multi_map <- plot_a + plot_b + 
  plot_annotation(
    title = "Figure 1: Spatial Relationship Between COVID-19 and Age Demographics",
    subtitle = "Analysis of Case Counts vs. Vulnerable Populations by ZIP Code",
    caption = "Data Sources: NYC DOHMH & US Census Bureau",
    theme = theme(plot.title = element_text(size = 16, face = "bold", hjust = 0.5))
  )

task2_multi_map

ggsave(filename = "Task2_MultiMap_Elderly.png", 
       plot = task2_multi_map, 
       width = 14, 
       height = 8, 
       units = "in", 
       dpi = 300)

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.

library(leaflet)
library(htmlwidgets)
library(sf)
library(dplyr)

map_data_leaflet <- st_transform(map_data_final, 4326)

health_points <- map_data_leaflet %>%
  filter(!is.na(`Facility Longitude`) & !is.na(`Facility Latitude`)) %>%
  # Keep only the unique facility locations
  distinct(`Facility Name`, .keep_all = TRUE)


pal_covid <- colorNumeric(palette = "YlOrRd", domain = map_data_leaflet$Positive)
pal_elderly <- colorNumeric(palette = "YlGnBu", domain = map_data_leaflet$pct_elderly)

interactive_map_final <- leaflet() %>%
  addProviderTiles(providers$CartoDB.Positron) %>%
  

addPolygons(
    data = map_data_leaflet,
    group = "COVID-19 Cases",
    fillColor = ~pal_covid(Positive),
    weight = 1, opacity = 1, color = "white", fillOpacity = 0.7,
    highlightOptions = highlightOptions(weight = 3, color = "#666", bringToFront = TRUE),
    label = ~paste0("ZIP: ", ZIPCODE, " | Cases: ", Positive),
    popup = ~paste0("<b>ZIP Code: </b>", ZIPCODE, "<br><b>Positive Cases: </b>", Positive)
  ) %>%
  
 
  addPolygons(
    data = map_data_leaflet,
    group = "Elderly Population %",
    fillColor = ~pal_elderly(pct_elderly),
    weight = 1, opacity = 1, color = "white", fillOpacity = 0.7,
    highlightOptions = highlightOptions(weight = 3, color = "#666", bringToFront = TRUE),
    label = ~paste0("ZIP: ", ZIPCODE, " | Elderly: ", round(pct_elderly, 1), "%"),
    popup = ~paste0("<b>ZIP Code: </b>", ZIPCODE, "<br><b>Elderly Pop: </b>", round(pct_elderly, 1), "%")
  ) %>%
  
  
  addCircleMarkers(
    data = health_points,
    lng = ~`Facility Longitude`, lat = ~`Facility Latitude`,
    group = "Health Facilities",
    radius = 4, color = "#2c3e50", stroke = TRUE, weight = 1, fillOpacity = 0.8,
    label = ~`Facility Name`,
    popup = ~paste0("<b>Facility: </b>", `Facility Name`, "<br><b>Type: </b>", Description)
  ) %>%
  

  addLegend(pal = pal_covid, values = map_data_leaflet$Positive, title = "Positive Cases", 
            group = "COVID-19 Cases", position = "bottomright") %>%
  addLegend(pal = pal_elderly, values = map_data_leaflet$pct_elderly, title = "% Elderly", 
            group = "Elderly Population %", position = "bottomright") %>%
  
  addLayersControl(
    baseGroups = c("COVID-19 Cases", "Elderly Population %"),
    overlayGroups = c("Health Facilities"),
    options = layersControlOptions(collapsed = FALSE)
  ) %>%
  
  addEasyButton(easyButton(
    icon="fa-home", title="Reset View",
    onClick = JS("function(btn, map){ map.setView([40.7128, -74.0060], 10); }"))) %>%
  
  addScaleBar(position = "bottomleft")

saveWidget(interactive_map_final, file = "Task3_Interactive_COVID_Map.html")

interactive_map_final