Steven A. Assigment 9 R-Spatial

R Spatial Lab Assignment # 2

(To start): Load Zip Code level ACS and Covid data from lab 8

setwd('C:/Users/steve/Downloads/G385_R_Data_Viz/R_Spatial')

# Load mega sf file from lab 8 
mega <-st_read("nyc_mega_covid_acs_rf_nh_data.gpkg")
## Reading layer `nyc_mega_covid_acs_rf_nh_data' from data source 
##   `C:\Users\steve\Downloads\G385_R_Data_Viz\R_Spatial\nyc_mega_covid_acs_rf_nh_data.gpkg' 
##   using driver `GPKG'
## Simple feature collection with 248 features and 35 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 913129 ymin: 120020.9 xmax: 1067494 ymax: 272710.9
## Projected CRS: NAD83 / New York Long Island (ftUS)
str(mega)
## Classes 'sf' and 'data.frame':   248 obs. of  36 variables:
##  $ ZIPCODE.x        : chr  "00083" "10001" "10002" "10003" ...
##  $ pop_total        : num  3 19146 74310 53487 NA ...
##  $ elder65_pop      : num  0 2500 15815 6296 NA ...
##  $ male_pop         : num  0 9799 35957 26930 NA ...
##  $ malePctg         : num  0 51.2 48.4 50.3 NA ...
##  $ female_pop       : num  3 9347 38353 26557 NA ...
##  $ white_pop        : num  1 12485 24033 40503 NA ...
##  $ black_pop        : num  2 1092 5969 3130 NA ...
##  $ hispanic_pop     : num  1 2435 20065 4375 NA ...
##  $ adult_pop        : num  2 17658 64499 49620 NA ...
##  $ citizen_adult    : num  2 13800 52528 42971 NA ...
##  $ ZIPCODE.y        : chr  NA "10001" "10002" "10003" ...
##  $ BLDGZIP          : chr  NA "0" "0" "0" ...
##  $ PO_NAME          : chr  NA "New York" "New York" "New York" ...
##  $ POPULATION       : num  NA 22413 81305 55878 NA ...
##  $ AREA             : num  NA 17794941 26280129 15538376 NA ...
##  $ STATE            : chr  NA "NY" "NY" "NY" ...
##  $ COUNTY           : chr  NA "New York" "New York" "New York" ...
##  $ ST_FIPS          : chr  NA "36" "36" "36" ...
##  $ CTY_FIPS         : chr  NA "061" "061" "061" ...
##  $ URL              : chr  NA "http://www.usps.com/" "http://www.usps.com/" "http://www.usps.com/" ...
##  $ SHAPE_AREA       : num  NA 0 0 0 NA 0 0 0 0 0 ...
##  $ SHAPE_LEN        : num  NA 0 0 0 NA 0 0 0 0 0 ...
##  $ NEIGHBORHOOD_NAME: chr  NA "Chelsea/NoMad/West Chelsea" "Chinatown/Lower East Side" "East Village/Gramercy/Greenwich Village" ...
##  $ BOROUGH_GROUP    : chr  NA "Manhattan" "Manhattan" "Manhattan" ...
##  $ label            : chr  NA "10001, 10118" "10002" "10003" ...
##  $ lat              : num  NA 40.8 40.7 40.7 NA ...
##  $ lon              : num  NA -74 -74 -74 NA ...
##  $ COVID_CASE_COUNT : num  NA 1542 5902 2803 NA ...
##  $ COVID_CASE_RATE  : num  NA 5584 7836 5193 NA ...
##  $ POP_DENOMINATOR  : num  NA 27613 75323 53978 NA ...
##  $ COVID_DEATH_COUNT: num  NA 35 264 48 NA 0 1 4 118 37 ...
##  $ COVID_DEATH_RATE : num  NA 126.8 350.5 88.9 NA ...
##  $ PERCENT_POSITIVE : num  NA 7.86 12.63 6.93 NA ...
##  $ TOTAL_COVID_TESTS: num  NA 20158 48197 41076 NA ...
##  $ geom             :sfc_MULTIPOLYGON of length 248; first list element: List of 1
##   ..$ :List of 1
##   .. ..$ : num [1:216, 1:2] 998310 998283 998251 998178 998050 ...
##   ..- attr(*, "class")= chr [1:3] "XY" "MULTIPOLYGON" "sfg"
##  - attr(*, "sf_column")= chr "geom"
##  - attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA NA NA NA NA NA NA NA NA ...
##   ..- attr(*, "names")= chr [1:35] "ZIPCODE.x" "pop_total" "elder65_pop" "male_pop" ...
names(mega)
##  [1] "ZIPCODE.x"         "pop_total"         "elder65_pop"      
##  [4] "male_pop"          "malePctg"          "female_pop"       
##  [7] "white_pop"         "black_pop"         "hispanic_pop"     
## [10] "adult_pop"         "citizen_adult"     "ZIPCODE.y"        
## [13] "BLDGZIP"           "PO_NAME"           "POPULATION"       
## [16] "AREA"              "STATE"             "COUNTY"           
## [19] "ST_FIPS"           "CTY_FIPS"          "URL"              
## [22] "SHAPE_AREA"        "SHAPE_LEN"         "NEIGHBORHOOD_NAME"
## [25] "BOROUGH_GROUP"     "label"             "lat"              
## [28] "lon"               "COVID_CASE_COUNT"  "COVID_CASE_RATE"  
## [31] "POP_DENOMINATOR"   "COVID_DEATH_COUNT" "COVID_DEATH_RATE" 
## [34] "PERCENT_POSITIVE"  "TOTAL_COVID_TESTS" "geom"

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

p1 <- ggplot(data = mega) +
  geom_sf(aes(fill = COVID_CASE_RATE)) +
  scale_fill_viridis_c(option = "plasma", na.value = "lightgrey") +
  labs(title = "COVID-19 Case Rate (per 100,000 people) by Zip Code",
       fill = "Case Rate") +
  theme_minimal()

# define second plot - elderly population estimate 
p2 <- ggplot(data = mega) +
  geom_sf(aes(fill = elder65_pop)) +
  scale_fill_viridis_c(option = "plasma", na.value = "lightgrey") +
  labs(title = "ACS Estimate of Elderly Population (65+ years old) by Zip Code",
       fill = "Elderly Population") +
  theme_minimal() 
#library to make them side by side, I tried using gridExtra but its hard to set equal ratio
library(patchwork)
## Warning: package 'patchwork' was built under R version 4.4.3
side_by_side <- p1 + p2 + plot_layout(ncol = 2)
side_by_side

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.

# library for interactive maps 
library(tmap)
## Warning: package 'tmap' was built under R version 4.4.3
#?tmap
#?tm_shape
#?tm_fill
#?tm_scale
#?tm_borders

# set to view for interactive or plot for static
tmap_mode("view")
## ℹ tmap mode set to "view".
#define data used for map 
covid_death_rate <- tm_shape(mega) +
  tm_fill("COVID_DEATH_RATE", # map layer for polyogns, fill (fill color) 
          fill.scale = tm_scale(values = "brewer.yl_or_rd"), # "Visual variable that determines the fill color" - from help
          fill.legend = tm_legend(title = "Death Rate")) +  # legend title name
  tm_borders(fill_alpha = 0.5) + # transparency of borders
  tm_title("COVID-19 Death Rate (per 100,000 people) by ZIP Code")

covid_death_rate
tmap_save(covid_death_rate, filename = "covid_death_rate.html")
## Interactive map saved to C:\Users\steve\Downloads\G385_R_Data_Viz\R_Spatial\week9\covid_death_rate.html
#cols4all::c4a_gui()