R Spatial Lab Assignment # 3

Task 1:

Task 2:

Create a multi-map figure illustrating the possible relationship between COVID-19 confirmed cases or rate and another factor.

Use ggplot2 and other ggplot-compatible packages. The maps should be put side-by-side on one single page. Add graticule to at least one of those maps and label come of the features on the map where applicable and appropriate.
# Create quantile breaks for proper choropleth mapping
breaks_jenks <- classIntervals(c(min(allVariables_sf$COVID_CASE_COUNT) - 1,
                              allVariables_sf$COVID_CASE_COUNT), n=5, style="jenks")

# Add column case_bin to assign zipcodes into bins
allVariables_sf <- mutate(allVariables_sf,
                               case_bin = cut(COVID_CASE_COUNT, breaks_jenks$brks,
                                              labels = paste(
                                                format(breaks_jenks$brks[-length(breaks_jenks$brks)], big.mark = ","),
                                                format(breaks_jenks$brks[-1], big.mark = ","),
                                                sep = " -" # It originally gave me labels in scientific notation, so I used chatGPT to format the labels
                                              )))

# Extracting zipcodes with highest case count for labelling
highest_zips <- allVariables_sf %>%
  arrange(desc(COVID_CASE_COUNT)) %>%
  slice(1:5)
# COVID case map
g1 <- ggplot(allVariables_sf) +
  geom_sf(aes(fill = case_bin)) +
  scale_fill_brewer(palette = "RdPu", name = 'by Zipcode') +
  labs(x = 'Longitude', y = 'Latitude', title = "NYC COVID Case Count") +
  geom_sf_label(data = highest_zips,
                aes(label = ZIPCODE),
                label.size = .09,
                size = 3)
# Health facilities map
g2 <- ggplot(allVariables_sf) +
  geom_sf(aes(fill = factor(HealthFacilityNum))) +
  scale_fill_brewer(palette = "RdPu", name = 'Number of Facilities') +
  labs(x = 'Longitude', y = 'Latitude', title = "NYC Health Facilities by Zipcode")
ggarrange(g1, g2, nrow=2, ncol=1)

Task 3:

Create a web-based interactive map for COVID-19 data

Use tmap, mapview, or leaflet package and save it as a HTML file.
pal_fun <- colorNumeric("RdPu", NULL, n = 5);
p_tip <- paste0("ZIPCODE: ", allVariables_sf$ZIPCODE);

interactive_covid <- leaflet(allVariables_sf %>% st_transform(4326)) %>%
  addPolygons(
    stroke = FALSE,
    fillColor = ~pal_fun(PERCENT_POSITIVE),
    fillOpacity = 0.8, smoothFactor = 0.5,
    label = p_tip) %>%
  addTiles() %>%
  addLegend("bottomright", 
            pal = pal_fun,
            values = ~allVariables_sf$PERCENT_POSITIVE,
            title = "Percent of Positive COVID Tests by Zipcode")

interactive_covid
htmlwidgets::saveWidget(interactive_covid, 'interactive_covid.html', 
                        selfcontained = FALSE)
## Warning in dir.create(target_dir):
## 'interactive_covid_files\htmltools-fill-0.5.8.1' already exists
## Warning in dir.create(target_dir): 'interactive_covid_files\htmlwidgets-1.6.4'
## already exists
## Warning in dir.create(target_dir): 'interactive_covid_files\jquery-3.6.0'
## already exists
## Warning in dir.create(target_dir): 'interactive_covid_files\leaflet-1.3.1'
## already exists
## Warning in dir.create(target_dir): 'interactive_covid_files\leafletfix-1.0.0'
## already exists
## Warning in dir.create(target_dir): 'interactive_covid_files\proj4-2.6.2'
## already exists
## Warning in dir.create(target_dir): 'interactive_covid_files\Proj4Leaflet-1.0.1'
## already exists
## Warning in dir.create(target_dir):
## 'interactive_covid_files\rstudio_leaflet-1.3.1' already exists
## Warning in dir.create(target_dir):
## 'interactive_covid_files\leaflet-binding-2.2.2' already exists