R Spaital Lab Assignment # 3

task 1:

static map of case count and total population

nyc_zipdata <- st_read('lab2_nyczip.gpkg')
## Reading layer `lab2_nyczip' from data source 
##   `C:\Users\Andy\Desktop\R_Course\assignments\Session_8\R-Spatial_II_Lab\lab2_nyczip.gpkg' 
##   using driver `GPKG'
## Simple feature collection with 250 features and 15 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -74.25576 ymin: 40.49584 xmax: -73.6996 ymax: 40.91517
## Geodetic CRS:  WGS 84
#case count
cases_p1 <- cases_p1 <- ggplot(nyc_zipdata) +
  geom_sf(data = nyc_zipdata, aes(fill = case_count)) +
  geom_sf_text(data = nyc_zipdata, aes(label = case_count), color = "black", size = 2, check_overlap = TRUE) +
  scale_fill_gradient(low = "white", high = "red", na.value = "transparent", name = "Case Count", guide = "legend") +
  labs(title = "NYC Case Counts by Zip Code", fill = "Case Count") +
  theme(axis.title.x=element_blank(), 
            axis.title.y = element_blank())
cases_p1

# total population
pop_p1 <- ggplot(nyc_zipdata) +
  geom_sf(data= nyc_zipdata, aes(fill=totPop)) +
  geom_sf_text(data=nyc_zipdata, aes(label= totPop), color ='black', size=2, check_overlap=TRUE) +
  scale_fill_gradient(low = "white", high = "blue", na.value = "transparent", name = "Population", guide = "legend") +  
  labs(title = "NYC total population by zip code", fill = "Population") +
  theme(axis.title.x=element_blank(), 
            axis.title.y = element_blank())
pop_p1

task 2

case rate and food retail stores

nyc_zipdata1 <- nyc_zipdata %>%filter(tests != 0) %>% mutate(case_rate = round(case_count / tests,3))
#case_rate has added st_graticule geom
bbox <- st_bbox(nyc_zipdata1)
case_rate <- ggplot(nyc_zipdata1) +
  geom_sf(data = st_graticule(bbox, crs=st_crs(4326)), 
          color = "gray50", size = 0.5, alpha=0.2) +
  geom_sf(data= nyc_zipdata1, aes(fill=case_rate))+
  geom_sf_text(data=nyc_zipdata1, aes(label=case_rate),color = 'black',size=2, check_overlap=T) +
  scale_fill_gradient(low='white', high='red', name= 'Positive test rate',guide='legend') +
  labs(title="Positive COVID test rate by ZIPCODE NYC", fill= 'Positive test rate') +
  theme(axis.title.x=element_blank(), 
        axis.title.y = element_blank()) 

  
food_retail <-ggplot(nyc_zipdata1) +
  geom_sf(aes(fill = food_retail)) +
  geom_sf_text(aes(label = round(food_retail, 1)), color = 'black', size = 2, check_overlap = TRUE) +
  scale_fill_gradient(low = 'white', high = 'red', name = 'Food Retail', guide = 'legend') +
  labs(title = 'Food Retail by ZIP Code NYC', fill = 'Food Retail') +
  theme(axis.title.x = element_blank(), 
        axis.title.y = element_blank())
#library for side by side grid of two maps
require(gridExtra);
## Loading required package: gridExtra
## 
## Attaching package: 'gridExtra'
## The following object is masked from 'package:dplyr':
## 
##     combine
grid.arrange(case_rate, food_retail, nrow=1)

task 3

leaflet html map of percent population asian

color_quantile <-colorQuantile("YlOrRd", NULL, n = 6)
nyc_zipdata1 <- nyc_zipdata1 %>% 
  mutate(pct_asian = round(asianPop / totPop * 100, 2))  %>%
  filter(!is.na(pct_asian) & pct_asian != 0)

asianpop_leaflet <- leaflet(nyc_zipdata1) %>% 
  addProviderTiles("OpenStreetMap") %>% 
  addPolygons(
    fillColor = ~color_quantile(pct_asian),
    fillOpacity = 0.9,
    stroke = FALSE,
    label = ~paste("ZIP Code:", ZIPCODE, "Percent Asian:", pct_asian),
    group = 'asian pop') %>%
  addLegend("bottomright",
            pal = color_quantile,
            values = ~pct_asian,
            title = 'percent of population that is Asian')

asianpop_leaflet
htmlwidgets::saveWidget(asianpop_leaflet, 'percent_asian_zipcode_nyc.html')