Week 9 Assignment

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

The maps should be put side by side on one single page.

Add graticule to at least one of those maps and label some of the feature on the map where applicable and appropriate.

load("/Users/kaitlynmaritan/Desktop/R-spatial/Data/nycNursingHomes.RData")
load("/Users/kaitlynmaritan/Desktop/R-spatial/Data/popData.RData")
load("/Users/kaitlynmaritan/Desktop/R-spatial/Data/food_store_map.RData")

ggplot(zip_covid19) + 
  geom_sf(aes(fill=zcta_cum.perc_pos))

require(classInt)

breaks_qt <- classIntervals(c(min(zip_covid19$zcta_cum.perc_pos) - .00001,
      zip_covid19$zcta_cum.perc_pos), n = 7, style = "quantile")
breaks_qt
## style: quantile
## [23.25999,43.7)     [43.7,48.9)    [48.9,55.28)   [55.28,58.69)    [58.69,61.8) 
##              27              26              28              27              27 
##    [61.8,64.65)   [64.65,79.01] 
##              27              28
zip_covid19 <- mutate(zip_covid19, 
    positive_rate_cat = cut(zcta_cum.perc_pos, breaks_qt$brks,dig.lab = 4, digits=1)) 


#Map 1
ggplot(zip_covid19) + 
  geom_sf(aes(fill = positive_rate_cat)) +
  scale_fill_brewer(palette = "GnBu", name = 'Per SQKM') +
  coord_sf(
    xlim = c(-74.25, -73.72),
    ylim = c(40.5, 40.91),
    default_crs = sf::st_crs(4326)) +
  labs(
    x = 'Longitude',
    y = 'Latitude',
    title = 'Positive COVID-19 Tests (April 12, 2020)')

#Map 2
ggplot(zip_covid19) + 
  geom_sf(aes(fill = positive_rate_cat)) +
  geom_sf(data = nycNursingHomes, colour = "red", size = 0.1) +
  scale_fill_brewer(palette = "GnBu", name = "Per SQKM") +
  coord_sf(
    xlim = c(-74.25, -73.72),
    ylim = c(40.5, 40.91),
    default_crs = sf::st_crs(4326)
  ) +
  labs(
    x = "Longitude",
    y = "Latitude",
    title = "Positive COVID-19 Tests and Nursing Home Locations (April 12, 2020)")

#Map 3
ggplot(zip_covid19) + 
  geom_sf(aes(fill = positive_rate_cat)) +
  geom_sf(data = food_store_map, colour = "red", size = 0.01, alpha = 0.2) +
  scale_fill_brewer(palette = "GnBu", name = "Per SQKM") +
  coord_sf(
    xlim = c(-74.25, -73.72),
    ylim = c(40.5, 40.91),
    default_crs = sf::st_crs(4326)) +
  labs(
    x = "Longitude",
    y = "Latitude",
    title = "Positive COVID-19 Tests and Retail Food Store Locations (April 12, 2020)")

#Map 4

breaks_qt2 <- classIntervals(c(min(popData$elderlyPop) - .0001,
                               popData$elderlyPop), n = 8, style = "quantile")
popData <- mutate(popData, 
                      elderlypop_count = cut(elderlyPop, breaks_qt2$brks,dig.lab = 4, digits=100)) 

borough_centroids <- popData %>%
  group_by(boro_name) %>%
  summarise(geometry = sf::st_union(geometry)) %>%
  mutate(centroid = sf::st_centroid(geometry)) %>%
  mutate(x = sf::st_coordinates(centroid)[,1],
         y = sf::st_coordinates(centroid)[,2])

ggplot(popData) + 
  geom_sf(aes(fill = elderlypop_count)) +
  geom_label_repel(data = borough_centroids,
                   aes(x = x, y = y, label = boro_name),
                   label.size = 0.2,
                   size = 4,
                   segment.color = "grey50",
                   segment.size = 0.5) +
  scale_fill_brewer(palette = "GnBu", name = 'Per SQKM') +
  coord_sf(
    xlim = c(-74.25, -73.72),
    ylim = c(40.5, 40.91),
    default_crs = sf::st_crs(4326)) +
  labs(
    x = 'Longitude',
    y = 'Latitude',
    title = 'NYC Elderly Population by Borough')

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.

tmap_mode("view")
## ℹ tmap mode set to "view".
htmlMap <- tm_shape(popData) +
  tm_polygons("elderlyPop", 
              style="quantile", 
              title="NYC Elderly \nPopulation \nper sqkm")
## 
## ── tmap v3 code detected ───────────────────────────────────────────────────────
## [v3->v4] `tm_polygons()`: instead of `style = "quantile"`, use fill.scale =
## `tm_scale_intervals()`.
## ℹ Migrate the argument(s) 'style' to 'tm_scale_intervals(<HERE>)'[v3->v4] `tm_polygons()`: migrate the argument(s) related to the legend of the
## visual variable `fill` namely 'title' to 'fill.legend = tm_legend(<HERE>)'
tmap_mode("view")
## ℹ tmap mode set to "view".
tmap_last()
## Warning in tmap_last(): A map has not been created yet.
## NULL
htmlMap_widget <- tmap::tmap_leaflet(htmlMap)
## Registered S3 method overwritten by 'jsonify':
##   method     from    
##   print.json jsonlite
saveWidget(htmlMap_widget, "nyc_elderly_pop_tmap.html")