In this lab I explored the spatial data assembled in Labs 1 and
2.
For Lab 1, I produced a final dataset of NYC ZIP code areas with
COVID‑19 data (see RPubs
1284677) and for Lab 2, I aggregated additional demographic data
from ACS (see RPubs
1287481).
In this assignment, I used the final aggregated dataset
(nyc_zip_final) to:
load("D:/Session_7/R-Spatial_I_Lab/nyc_zip_final.RData")
p_covid <- ggplot(nyc_zip_final) +
geom_sf(aes(fill = Positive)) +
scale_fill_viridis_c(option = "plasma", direction = -1) +
labs(
title = "NYC COVID-19 Positive Cases by ZIP Code",
fill = "Positive Cases"
) +
theme_minimal() +
theme(plot.title = element_text(hjust = 0.5)) +
coord_sf(clip = "off") +
ggspatial::annotation_north_arrow(
location = "tr",
which_north = "true",
style = ggspatial::north_arrow_fancy_orienteering
)
p_elderly <- ggplot(nyc_zip_final) +
geom_sf(aes(fill = elderly_population)) +
scale_fill_viridis_c(option = "viridis", direction = -1) +
labs(
title = "NYC Elderly Population (65+)",
fill = "Elderly Population"
) +
theme_minimal() +
theme(plot.title = element_text(hjust = 0.5)) +
coord_sf(clip = "off") +
ggspatial::annotation_north_arrow(
location = "tr",
which_north = "true",
style = ggspatial::north_arrow_fancy_orienteering
)
print(p_covid)
print(p_elderly)
p1 <- ggplot(nyc_zip_final) +
geom_sf(aes(fill = Positive)) +
scale_fill_viridis_c(option = "plasma", direction = -1) +
labs(title = "COVID-19 Positive Cases", fill = "Positive") +
theme_minimal() +
theme(plot.title = element_text(hjust = 0.5),
axis.text.x = element_text(angle = 45, hjust = 1)) +
coord_sf(clip = "off") +
ggspatial::annotation_north_arrow(location = "tr", which_north = "true",
style = ggspatial::north_arrow_fancy_orienteering)
p2 <- ggplot(nyc_zip_final) +
geom_sf(aes(fill = elderly_population)) +
scale_fill_viridis_c(option = "viridis", direction = -1) +
labs(title = "Elderly Population (65+)", fill = "Elderly") +
theme_minimal() +
theme(plot.title = element_text(hjust = 0.5),
axis.text.x = element_text(angle = 45, hjust = 1)) +
coord_sf(clip = "off") +
ggspatial::annotation_north_arrow(location = "tr", which_north = "true",
style = ggspatial::north_arrow_fancy_orienteering)
combined <- ggarrange(p1, p2, ncol = 2, nrow = 1)
ggsave("D:/Spring2025Hunter/Session_8/combined_map.png", plot = combined, width = 12, height = 8, dpi = 300)
nyc_zip_final_no_na <- nyc_zip_final[!is.na(nyc_zip_final$Positive), ]
nyc_zip_final_no_na$Positive <- as.numeric(nyc_zip_final_no_na$Positive)
pal <- colorNumeric(
palette = viridisLite::plasma(256, direction = -1),
domain = nyc_zip_final_no_na$Positive
)
leaflet(nyc_zip_final_no_na %>% st_transform(4326)) %>%
addProviderTiles("CartoDB.Voyager") %>%
addPolygons(
fillColor = ~pal(Positive),
fillOpacity = 0.7,
color = "grey",
weight = 1,
popup = ~paste("<strong>ZIP Code:</strong>", ZIPCODE, "<br>",
"<strong>Positive Cases:</strong>", Positive)
) %>%
addLegend(
pal = pal,
values = nyc_zip_final_no_na$Positive,
title = "Positive COVID-19 Cases",
position = "bottomright"
) %>%
addScaleBar(
position = "bottomleft",
options = scaleBarOptions(imperial = FALSE)
)