R Spatial Lab Assignment# 3
Task 1: Plot at least two high-quality static maps at the zip code
level, with one using the COVID-19 testing data and one using a related
factor such as the number of nursing homes or the elderly population.
You can use either plot method for sf or ggplot method.
library(ggplot2)
library(sf)
## Linking to GEOS 3.13.0, GDAL 3.8.5, PROJ 9.5.1; sf_use_s2() is TRUE
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(ggpubr)
library(RColorBrewer)
library(classInt)
library(viridis)
## Loading required package: viridisLite
load("~/Desktop/SPRING 2026 HUNTER/Programming with R/Week 9/Section_09/map_data_final.RData")
map_covid_final <- ggplot(map_data_final) +
geom_sf(aes(fill = Positive), color = "white", size = 0.1) +
scale_fill_viridis_c(option = "magma", name = "Positive Cases") +
labs(title = "NYC COVID-19 Positive Cases",
subtitle = "By ZIP Code (April 2020 Data)",
caption = "Source: NYC DOHMH") +
theme_void()
print(map_covid_final)

ggsave("Map1_COVID_Positives.png", map_covid_final, width = 8, height = 8, dpi = 300)
map_elderly_final <- ggplot(map_data_final) +
geom_sf(aes(fill = pct_elderly), color = "white", size = 0.1) +
scale_fill_viridis_c(option = "mako", name = "% Pop 65+") +
labs(title = "Elderly Population Concentration",
subtitle = "Percentage of Total Population by ZIP Code",
caption = "Source: ACS 5-Year Estimates (2018)") +
theme_void()
print(map_elderly_final)

ggsave("Map2_Elderly_Concentration.png", map_elderly_final, width = 8, height = 8, 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