R Spatial Lab Assignment # 3

Loading in Data

food_retail <- st_read(dsn = 'C:/Users/rsaya/Downloads/GEOG 39300/Section_3/Lecture_9/Data/nyc/nyc_aggregate.gpkg', 
                       layer = 'food_sf')
## Reading layer `food_sf' from data source 
##   `C:\Users\rsaya\Downloads\GEOG 39300\Section_3\Lecture_9\Data\nyc\nyc_aggregate.gpkg' 
##   using driver `GPKG'
## Simple feature collection with 248 features and 2 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -74.25576 ymin: 40.49584 xmax: -73.6996 ymax: 40.91517
## Geodetic CRS:  WGS 84
nursing_homes <- st_read(dsn = 'C:/Users/rsaya/Downloads/GEOG 39300/Section_3/Lecture_9/Data/nyc/nyc_aggregate.gpkg', 
                         layer = 'NH_sf')
## Reading layer `NH_sf' from data source 
##   `C:\Users\rsaya\Downloads\GEOG 39300\Section_3\Lecture_9\Data\nyc\nyc_aggregate.gpkg' 
##   using driver `GPKG'
## Simple feature collection with 248 features and 2 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -74.25576 ymin: 40.49584 xmax: -73.6996 ymax: 40.91517
## Geodetic CRS:  WGS 84

Task 1: Static Map 1

covid19 <- st_read(dsn = 'C:/Users/rsaya/Downloads/GEOG 39300/Section_3/Lecture_9/Data/nyc/nyc_aggregate.gpkg', 
                   layer = 'c19_sf')
## Reading layer `c19_sf' from data source 
##   `C:\Users\rsaya\Downloads\GEOG 39300\Section_3\Lecture_9\Data\nyc\nyc_aggregate.gpkg' 
##   using driver `GPKG'
## Simple feature collection with 189 features and 24 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: -74.25576 ymin: 40.49584 xmax: -73.70098 ymax: 40.91517
## Geodetic CRS:  WGS 84
breaks_1 <- classIntervals(c(min(covid19$total_covid_tests) - 1,
                             covid19$total_covid_tests), n = 7, style = "quantile")
covid19 <- mutate(covid19,
                  c19_test = cut(total_covid_tests, breaks_1$brks, 
                                 dig.lab = 3, digits = 1))
ggplot(covid19)+
  geom_sf(aes(fill=c19_test))+
  scale_fill_brewer(palette = "PuBuGn", name = "# of Tests")+
  coord_sf(default_crs = sf::st_crs(4326))+
  labs(x = "Longitude", y = "Latitude",
       title = "Total COVID-19 Tests") 

Task 1: Static Map 2

census <- st_read(dsn = 'C:/Users/rsaya/Downloads/GEOG 39300/Section_3/Lecture_9/Data/nyc/nyc_aggregate.gpkg', 
                  layer = 'census_sf')
## Reading layer `census_sf' from data source 
##   `C:\Users\rsaya\Downloads\GEOG 39300\Section_3\Lecture_9\Data\nyc\nyc_aggregate.gpkg' 
##   using driver `GPKG'
## Simple feature collection with 248 features and 10 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -74.25576 ymin: 40.49584 xmax: -73.6996 ymax: 40.91517
## Geodetic CRS:  WGS 84
census <- mutate(census, totPop = replace(totPop, is.na(totPop), 0))

breaks_2 <- classIntervals(c(min(census$totPop) - 1,
                             census$totPop), n = 7, style = "quantile")
census <- mutate(census,
                 totpop_brks = cut(totPop, breaks_2$brks, 
                                   dig.lab = 3, digits = 1))
ggplot(census)+
  geom_sf(aes(fill=totpop_brks))+
  scale_fill_brewer(palette = "PuBuGn", name = "Total Population")+
  coord_sf(default_crs = sf::st_crs(4326))+
  labs(x = "Longitude", y = "Latitude",
       title = "Total Population in NYC") 

Task 2: ggplot Map 1

covid19 <- st_read(dsn = 'C:/Users/rsaya/Downloads/GEOG 39300/Section_3/Lecture_9/Data/nyc/nyc_aggregate.gpkg', 
                   layer = 'c19_sf')
## Reading layer `c19_sf' from data source 
##   `C:\Users\rsaya\Downloads\GEOG 39300\Section_3\Lecture_9\Data\nyc\nyc_aggregate.gpkg' 
##   using driver `GPKG'
## Simple feature collection with 189 features and 24 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: -74.25576 ymin: 40.49584 xmax: -73.70098 ymax: 40.91517
## Geodetic CRS:  WGS 84
breaks_3 <- classIntervals(c(min(covid19$covid_case_count) - 1,
                             covid19$covid_case_count), n = 7, style = "quantile")
covid19 <- mutate(covid19,
                  c19_case = cut(covid_case_count, breaks_3$brks, 
                                 dig.lab = 3, digits = 1))

label_coords1 <- covid19 %>% sf::st_centroid() %>% 
  dplyr::filter(covid_case_count > 9000) %>% 
  sf::st_coordinates();

label_data1 <- covid19 %>% sf::st_centroid() %>% 
  dplyr::filter(covid_case_count > 9000) %>% 
  dplyr::mutate(x = label_coords1[,1], y = label_coords1[,2])

ggplot(covid19)+
  geom_sf(aes(fill=c19_case))+
  geom_label_repel(data = label_data1, 
                   aes(x=x, y=y, label = covid_case_count),
                   label.size = 0.9, size = 3, 
                   segment.color = 'brown1',
                   segment.size = 0.8)+
  scale_fill_brewer(palette = "YlGnBu", name = "# of Cases")+
  coord_sf(default_crs = sf::st_crs(4326))+
  labs(x = "Longitude", y = "Latitude",
       title = "Total COVID-19 Cases") 

Task 2: ggplot Map 2

nursing_homes <- st_read(dsn = 'C:/Users/rsaya/Downloads/GEOG 39300/Section_3/Lecture_9/Data/nyc/nyc_aggregate.gpkg', 
                         layer = 'NH_sf')
## Reading layer `NH_sf' from data source 
##   `C:\Users\rsaya\Downloads\GEOG 39300\Section_3\Lecture_9\Data\nyc\nyc_aggregate.gpkg' 
##   using driver `GPKG'
## Simple feature collection with 248 features and 2 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -74.25576 ymin: 40.49584 xmax: -73.6996 ymax: 40.91517
## Geodetic CRS:  WGS 84
breaks_4 <- classIntervals(c(min(nursing_homes$NH_location) - 0.1,
                             nursing_homes$NH_location), n = 2, style = "quantile")

nursing_homes <- mutate(nursing_homes,
                        NH_loc = cut(NH_location, breaks_4$brks, 
                                     dig.lab = 2, digits = 1))

label_coords2 <- nursing_homes %>% sf::st_centroid() %>% 
  dplyr::filter(NH_location > 4) %>% 
  sf::st_coordinates();

label_data2 <- nursing_homes %>% sf::st_centroid() %>% 
  dplyr::filter(NH_location > 4) %>% 
  dplyr::mutate(x = label_coords2[,1], y = label_coords2[,2])

ggplot(nursing_homes)+
  geom_sf(aes(fill=NH_loc))+
  geom_label_repel(data = label_data2, 
                   aes(x=x, y=y, label = NH_location),
                   label.size = 0.9, size = 3, 
                   segment.color = 'brown1',
                   segment.size = 0.8)+
  scale_fill_brewer(palette = "YlGnBu", name = "# of Locations")+
  coord_sf(default_crs = sf::st_crs(4326))+
  labs(x = "Longitude", y = "Latitude",
       title = "Nursing Homes Locations") 

Task 2: Save Map to PDF

Total_Covid19_Cases <- ggplot(covid19)+
  geom_sf(aes(fill=c19_case))+
  geom_label_repel(data = label_data1, 
                   aes(x=x, y=y, label = covid_case_count),
                   label.size = 0.9, size = 3, 
                   segment.color = 'brown1',
                   segment.size = 0.8)+
  scale_fill_brewer(palette = "YlGnBu", name = "# of Cases")+
  coord_sf(default_crs = sf::st_crs(4326))+
  labs(x = "Longitude", y = "Latitude",
       title = "Total COVID-19 Cases") 

Nursing_Homes_Locations <- ggplot(nursing_homes)+
  geom_sf(aes(fill=NH_loc))+
  geom_label_repel(data = label_data2, 
                   aes(x=x, y=y, label = NH_location),
                   label.size = 0.9, size = 3, 
                   segment.color = 'brown1',
                   segment.size = 0.8)+
  scale_fill_brewer(palette = "YlGnBu", name = "# of Locations")+
  coord_sf(default_crs = sf::st_crs(4326))+
  labs(x = "Longitude", y = "Latitude",
       title = "Nursing Homes Locations") 

pdf(file = 'C:/Users/rsaya/Downloads/GEOG 39300/Section_3/Lecture_9/task2_plot.pdf', width = 6.5, height = 8.0, pointsize = 9);
Total_Covid19_Cases; Nursing_Homes_Locations;
dev.off()
## png 
##   2
ggarrange(Total_Covid19_Cases, Nursing_Homes_Locations, nrow = 2, ncol = 1) %>% 
  ggexport(filename = 'C:/Users/rsaya/Downloads/GEOG 39300/Section_3/Lecture_9/task2_plot.pdf')
## file saved to C:/Users/rsaya/Downloads/GEOG 39300/Section_3/Lecture_9/task2_plot.pdf

Task 3: html Map

breaks_5 <- classIntervals(c(min(covid19$covid_death_count) - 1,
                             covid19$covid_death_count), n = 5, style = "quantile")
pal_fun <- colorQuantile("GnBu", NULL, n = 5)

p_tip <- paste0("Neighborhood: ", covid19$neighborhood_name)

p_popup <- paste0("Total Covid 19 Deaths: ",
                  covid19$covid_death_count,
                  "<br/>",
                  "Total Covid 19 Cases: ",
                  covid19$covid_case_count,
                  sep="")
p_highlight <- leaflet::highlightOptions(opacity = 1.0, fillColor = 'mediumspringgreen')
p_label <- leaflet::labelOptions(opacity = 0.7)

htmlMap <- leaflet(covid19) %>% 
  addPolygons(stroke = FALSE,
              fillColor = ~pal_fun(covid_death_count),
              fillOpacity = 0.9, smoothFactor = 0.5,
              popup = p_popup,
              label = p_tip,
              highlightOptions = p_highlight,
              labelOptions = p_label) %>% 
  addTiles(group = "OSM") %>% 
  addLegend("topright", 
            colors = brewer.pal(5, "GnBu"),
            labels = paste0(format(breaks_5$brks[-1], digits = 2)),
            values=~covid_death_count,
            title = 'COVID-19 Deaths <br> per Neighborhood')
htmlMap
htmlwidgets::saveWidget(htmlMap, 'nyc_covid19_leaflet_widget.html')