R Spatial Lab Assignment # 3

Task 1: Two Statistic Maps

load("zip_codes_spatial_data.RData")

# First Map

nyc_final_sf <- nyc_final_sf %>%
  mutate(covid_percent = (Positive.x/Total)*100) %>%
  filter(!is.na(covid_percent))

ggplot(nyc_final_sf) +
  geom_sf(aes(fill=covid_percent)) + 
  scale_fill_gradient(low="palegreen", high = "darkgreen") +
  labs(title="Percent of Covid Tests Positive by Zipcodes", fill="Percent COVID-19", x="Longitude", y="Latitude") +
  coord_sf()+
  theme(panel.border = element_rect(color = "black", fill=NA, linewidth=1))

nyc_final_sf <- nyc_final_sf %>%
  mutate(elderly_percent = (elderlyPop/totPop)*100) %>%
  filter(!is.na(elderly_percent))

ggplot(nyc_final_sf) +
  geom_sf(aes(fill=elderly_percent)) + 
  theme_minimal()+
  scale_fill_gradient(low="pink", high = "red") +
  labs(title="Percent of Elderly Population by Zipcodes", fill="Percent Elderly", x="Longitude", y="Latitude") +
  theme(panel.border = element_rect(color = "black", fill=NA, linewidth=1))

Task 2: Multi-Map Figure

covid_map <- ggplot(nyc_final_sf) +
  geom_sf(aes(fill=covid_percent)) + 
  scale_fill_gradient(low="palegreen", high = "darkgreen") +
  labs(title="Percent of Covid Tests Positive by Zipcodes", fill="Percent COVID-19", x="Longitude", y="Latitude") +
  coord_sf()+
  scale_x_continuous(n.breaks = 4) +
  scale_y_continuous(n.breaks = 4) +
  theme(panel.grid.major = element_line(color = "grey60"),
    panel.border = element_rect(color = "black", fill = NA),
    plot.title = element_text(size = 10, hjust = 0.5),
    axis.title = element_text(size = 8),
    legend.title = element_text(size = 7))#, panel.border = element_rect(color = "black", fill=NA, linewidth=1))

elderly_map <- ggplot(nyc_final_sf) +
  geom_sf(aes(fill=elderly_percent)) + 
  scale_fill_gradient(low="pink", high = "red") +
  labs(title="Percent of Elderly Population by Zipcodes", fill="Percent Elderly", x="Longitude", y="Latitude") +
  coord_sf()+
  scale_x_continuous(n.breaks = 4) +
  scale_y_continuous(n.breaks = 4) +
  theme(panel.grid.major = element_line(color = "grey60"),
    panel.border = element_rect(color = "black", fill = NA),
    plot.title = element_text(size = 10, hjust = 0.5),
    axis.title = element_text(size = 8),
    legend.title = element_text(size = 7))#, panel.border = element_rect(color = "black", fill=NA, linewidth=1))


covid_map + elderly_map

Task 3: Interactive Map

leaflet(nyc_final_sf) %>%
  addTiles()
leaftlet_map <- leaflet(nyc_final_sf) %>%
  addPolygons(
    stroke=FALSE,
    fillColor= ~colorNumeric("Blues", covid_percent)(covid_percent),
    fillOpacity = 0.8, 
    smoothFactor = 0.5, # make it nicer
    label = ~paste("ZIPCODE:",ZIPCODE, "<br>Rate:",covid_percent),
    popup = ~paste("COVID-19 Positive Rate:", covid_percent)) 
## Warning: sf layer is not long-lat data
## Warning: sf layer has inconsistent datum (+proj=lcc +lat_0=40.1666666666667 +lon_0=-74 +lat_1=41.0333333333333 +lat_2=40.6666666666667 +x_0=300000 +y_0=0 +datum=NAD83 +units=us-ft +no_defs).
## Need '+proj=longlat +datum=WGS84'
leaftlet_map <- leaflet(nyc_final_sf) %>%
  addPolygons(
    stroke=FALSE,
    fillColor= ~colorNumeric("Greens", FoodStoreNum)(FoodStoreNum),
    fillOpacity = 0.8, 
    smoothFactor = 0.5, # make it nicer
    label = ~paste("ZIPCODE:",ZIPCODE, "<br>Rate:",covid_percent),
    popup = ~paste("COVID-19 Positive Rate:", covid_percent)) 
## Warning: sf layer is not long-lat data
## Warning: sf layer has inconsistent datum (+proj=lcc +lat_0=40.1666666666667 +lon_0=-74 +lat_1=41.0333333333333 +lat_2=40.6666666666667 +x_0=300000 +y_0=0 +datum=NAD83 +units=us-ft +no_defs).
## Need '+proj=longlat +datum=WGS84'