R Spatial Lab Assignment # 3

Task 0 Setting Up Lab:

To set up for the lab, I changed my working directory to where I stored the folder setwd(“C:/Users/Max/Desktop/Section_08/R-Spatial_I_Lab”)

Task 1 COVID-19 Data and Elderly Population Map:

nycZipSf <- nycZipSf %>% filter(!is.na(COVID_CASE_COUNT)) 

#this plots the COVID map using the raw data and total counts, I used distiller instead of brewer because brewer cause my maps not to generate even after restarting console
mapCovid <- ggplot(nycZipSf) + geom_sf(aes(fill = COVID_CASE_COUNT), color = "white", size = 0.1) + scale_fill_distiller(palette = "Reds", direction = 1, name = "Total Cases") + labs(title = "NYC Total COVID-19 Cases")

#this plots the Elderly Population map using the raw data and total counts, I used distiller instead of brewer because brewer cause my maps not to generate even after restarting console
mapElderly <- ggplot(nycZipSf) + geom_sf(aes(fill = ElderlyPopulation), color = "white", size = 0.1) + scale_fill_distiller(palette = "Blues", direction = 1, name = "Total Elderly") + labs(title = "NYC Total Elderly Population")

#this prints the maps one by one, side by side will be the next task
mapCovid

mapElderly

Task 2 Multi-Map Figure:

#this combines the mpas for COVID and Elderly data
covidElderlyCombined <- ggarrange(mapCovid, mapElderly, ncol = 2, nrow = 1) #ggarrange was shown in the lab, I felt like this sufficed rather than using "+" method which seemed a little overcomplicated 

#this outputs said combination of maps
covidElderlyCombined

Task 3 Web-Based Interactive Map for COVID-19:

#sets it to NYC coordinate system
mapCoordinates <- st_transform(nycZipSf, 4326)

#this section is important to create color patterns for each layer
#one of my favorite aspects of R is using fun color schemes so I played round with some sources of colors blocking from https://stackoverflow.com/questions/57153428/r-plot-color-combinations-that-are-colorblind-accessible (I tried to choose colors that fit to the basic descriptions of the maps like red for covid cases)
palCovidData <- colorNumeric("Reds", domain = mapCoordinates$COVID_CASE_COUNT)
palElderlyData <- colorNumeric("BuPu", domain = mapCoordinates$ElderlyPopulation)
palNursingData <- colorNumeric("YlGn", domain = mapCoordinates$healthFacilityNumber)

#this portion creates the interactive map itself without any layers or data, I also stuck with DarkMatter over Positron because night mode is easier on my eyes 
interactiveMap <- leaflet(mapCoordinates) %>% addProviderTiles("CartoDB.DarkMatter", group = "Basemap")%>%
  
#COVID Data Layer, Layer 1
addPolygons(fillColor = ~palCovidData(COVID_CASE_COUNT), weight = 1, color = "grey", fillOpacity = 0.8, group = "Total COVID Cases", popup = ~paste("Zip:", ZIPCODE, "<br>Total Cases:", COVID_CASE_COUNT) ) %>%
  
#Elderly Pop Layer, Layer 2
addPolygons(fillColor = ~palElderlyData(ElderlyPopulation), weight = 1, color = "grey", fillOpacity = 0.8, group = "Elderly Population", popup = ~paste("Zip:", ZIPCODE, "<br>Elderly Population:", ElderlyPopulation)) %>%
  
#Nursing Home Layer, Layer 3
addPolygons(fillColor = ~palNursingData(healthFacilityNumber), weight = 1, color = "grey", fillOpacity = 0.8, group = "Nursing Homes", popup = ~paste("Zip:", ZIPCODE, "<br>Nursing Homes:", healthFacilityNumber)) %>%
  
#Adds the legend to the top left for the COVID Layer
addLegend("topleft", pal = palCovidData, values = ~COVID_CASE_COUNT, title = "Total COVID Cases", group = "Total COVID Cases") %>% addLayersControl(baseGroups = c("Basemap"), overlayGroups = c("Total COVID Cases", "Elderly Population", "Nursing Homes"), options = layersControlOptions(collapsed = FALSE) )

#Prints out the interactive map
interactiveMap
#NOTE: I tried adding the scale bar and north arrow to my maps but for some reason the package would not load onto my RStudio no matter if I updated, restarted, or downgraded to a earlier version so I left it out :(