#3.7 Lab Assignment #The third and last R-spatial lab is to visualize data that we assembled during the first two labs. As geovisualization is often exploratory, you are encouraged to be more creative.

Load packages

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.

#join covid and population data
joined_data <- st_join(nyc_COVID_sf, PopZipNYC, by = "ZIPCODE", join = st_equals)


# Create the map for COVID-19 confirmed cases
map_covid <- ggplot() +
  geom_sf(data = joined_data, aes(fill = PERCENT_POSITIVE), color = 'gray') +
  scale_fill_gradient(low = "white", high = "red") +
  labs(title = 'COVID-19 Confirmed Cases') +
  theme_minimal() +
  theme(legend.position = "top", plot.title = element_text(size = 16, face = "bold"), axis.title = element_text(size = 14))

# Create the map for the elderly population
map_elderly <- ggplot() +
  geom_sf(data = joined_data, aes(fill = elderlyPop), color = 'gray') +
  scale_fill_gradient(low = "white", high = "blue") +
  labs(title = 'Elderly Population in NYC') +
  theme_minimal() +
  theme(legend.position = "top", plot.title = element_text(size = 16, face = "bold"), axis.title = element_text(size = 14))

# Arrange the maps side by side on a single page
multi_map_figure <- map_covid + map_elderly + plot_layout(ncol = 2)

# Display the multi-map figure
multi_map_figure

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.

#mapview

# Convert the joined dataset to an sf object
joined_sf <- st_as_sf(joined_data, coords = c("lon", "lat"), crs = 4326)

# Create the interactive map using mapview
covid_map <- mapview(joined_sf,
                     zcol = c("PERCENT_POSITIVE", "elderlyPop"),
                     legend = TRUE,
                     map.types = c("OpenStreetMap", "Esri.WorldImagery"),
                     col.regions = list(colorRampPalette(c("lightblue", "darkblue")),
                                        colorRampPalette(c("lightgreen", "darkgreen"))),
                     stroke.width = 0.5)

 

# Display the interactive map
covid_map
# Save the interactive map as an HTML file

mapview::mapshot(covid_map, 'covid_map_mapview.html')

#leaflet

# reproject, if needed
joined_data <- st_transform(joined_data, 4326)


pal_fun <- colorQuantile("YlOrRd", NULL, n = 5)

p_tip <- paste0("GEOID: ", joined_data$GEOID);

p_popup <- paste0("<strong>Noise Complaint Density: </strong>", 
                  joined_data$elderlyPop%>%round(3)%>%format(nsmall = 3), 
                  " /sqkm",
                  " <br/>",
                 
                  sep="")

leaflet(joined_data) %>%
  addPolygons(
    stroke = FALSE, 
    fillColor = ~pal_fun(elderlyPop),
    fillOpacity = 0.8, smoothFactor = 0.5, 
    label = p_tip,
    popup = p_popup)   %>%
  addTiles()%>%
  addLegend("bottomright",  
            pal=pal_fun,   
            values=~elderlyPop,  
            title = 'elderly Population <br> per sqkm')