Plot at least two high-quality static maps with one using the COVID-19 data and one using a related factor. You can use either plot method for sf or ggplot method.
# Load packages
library(sf)
## Warning: package 'sf' was built under R version 4.3.3
## Linking to GEOS 3.11.2, GDAL 3.8.2, PROJ 9.3.1; sf_use_s2() is TRUE
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.3.3
library(mapview)
## Warning: package 'mapview' was built under R version 4.3.3
library(readr)
## Warning: package 'readr' was built under R version 4.3.1
library(ggmap)
## Warning: package 'ggmap' was built under R version 4.3.3
## ℹ Google's Terms of Service: <https://mapsplatform.google.com>
## Stadia Maps' Terms of Service: <https://stadiamaps.com/terms-of-service/>
## OpenStreetMap's Tile Usage Policy: <https://operations.osmfoundation.org/policies/tiles/>
## ℹ Please cite ggmap if you use it! Use `citation("ggmap")` for details.
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.3.2
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(stringr)
## Warning: package 'stringr' was built under R version 4.3.3
library(magrittr)
##
## Attaching package: 'magrittr'
## The following object is masked from 'package:ggmap':
##
## inset
library(RColorBrewer)
## Warning: package 'RColorBrewer' was built under R version 4.3.1
library(tmap)
## Warning: package 'tmap' was built under R version 4.3.3
## Breaking News: tmap 3.x is retiring. Please test v4, e.g. with
## remotes::install_github('r-tmap/tmap')
library(leaflet)
## Warning: package 'leaflet' was built under R version 4.3.3
library(classInt)
## Warning: package 'classInt' was built under R version 4.3.3
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.3.2
## Warning: package 'tidyr' was built under R version 4.3.2
## Warning: package 'purrr' was built under R version 4.3.3
## Warning: package 'forcats' was built under R version 4.3.2
## Warning: package 'lubridate' was built under R version 4.3.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats 1.0.0 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.1
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ tidyr::extract() masks magrittr::extract()
## ✖ dplyr::filter() masks stats::filter()
## ✖ magrittr::inset() masks ggmap::inset()
## ✖ dplyr::lag() masks stats::lag()
## ✖ purrr::set_names() masks magrittr::set_names()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(ggrepel)
## Warning: package 'ggrepel' was built under R version 4.3.3
library(ggpubr)
## Warning: package 'ggpubr' was built under R version 4.3.3
# Read in data
covid <- st_read("NYC_Covid.shp")
## Reading layer `NYC_Covid' from data source
## `C:\Users\naial\OneDrive\Documents\School\Geog393\Section 3\Serssion_9\NYC_Covid.shp'
## using driver `ESRI Shapefile'
## Simple feature collection with 248 features and 8 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: 278322.4 ymin: 36581.52 xmax: 325373 ymax: 83121.54
## Projected CRS: NAD83(HARN) / New York Long Island
covid <- st_transform(covid, 4326)
# Set color palettes
pal1 <- brewer.pal(7, "BuPu")
pal2 <- brewer.pal(7, "RdPu")
# Plot maps
plot(covid['total'],
main = "Total Population Covid-19 Cases in NYC",
breaks = "quantile", nbreaks=7,
pal = pal1,
graticule = st_crs(4326),
axes = TRUE)
plot(covid['hispanc'],
main = "Hispanic Population Covid-19 Cases in NYC",
breaks = "quantile", nbreaks=7,
pal = pal2,
graticule = st_crs(4326),
axes = TRUE)
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.
# Define breaks
breaks_qt_tot <- classIntervals(c(min(covid$total) - .00001,
covid$total), n = 7, style = "quantile")
breaks_qt_hisp <- classIntervals(c(min(covid$hispanc) - .00001,
covid$hispanc), n = 7, style = "quantile")
# Retrieve breaks
covid <- mutate(covid, total_cat = cut(total, breaks_qt_tot$brks, dig.lab = 4, digits=1))
hispanic <- mutate(covid, hispanic_cat = cut(hispanc, breaks_qt_hisp$brks, dig.lab = 4, digits=1))
# Define plots
totalPlot <- ggplot(covid) +
geom_sf(aes(fill=total_cat)) +
geom_sf_label(data = covid %>% dplyr::filter(total > 150000),
aes(label = total),
label.size = .09,
size = 3) +
scale_fill_brewer(palette = "Greens", name='Total Positive Cases') +
coord_sf(xlim = c(-74.26, -73.65),
default_crs = sf::st_crs(4326) ) +
labs(x='Longitude', y='Latitude',
title='Total COVID cases in NYC');
hispanicPlot <- ggplot(hispanic) +
geom_sf(aes(fill=hispanic_cat)) +
geom_sf_label(data = hispanic %>% dplyr::filter(hispanc > 100000),
aes(label = total),
label.size = .09,
size = 3) +
scale_fill_brewer(palette = "Reds", name='Total Hispanic Positive Cases') +
coord_sf(xlim = c(-74.26, -73.65),
default_crs = sf::st_crs(4326) ) +
labs(x='Longitude', y='Latitude',
title='Total Hispanic COVID cases in NYC');
totalPlot
## Warning in st_point_on_surface.sfc(sf::st_zm(x)): st_point_on_surface may not
## give correct results for longitude/latitude data
hispanicPlot
## Warning in st_point_on_surface.sfc(sf::st_zm(x)): st_point_on_surface may not
## give correct results for longitude/latitude data
# Export to pdf
ggarrange(totalPlot, hispanicPlot, nrow = 1, ncol = 2) %>%
ggexport(filename = file.path(getwd(), 'covidggplots.pdf'),
width=10.0, height=6.50, # the unit is inch
pointsize = 9)
## Warning in st_point_on_surface.sfc(sf::st_zm(x)): st_point_on_surface may not
## give correct results for longitude/latitude data
## Warning in st_point_on_surface.sfc(sf::st_zm(x)): st_point_on_surface may not
## give correct results for longitude/latitude data
## file saved to C:/Users/naial/OneDrive/Documents/School/Geog393/Section 3/Serssion_9/covidggplots.pdf
Create a web-based interactive map for COIVD-19 data using tmap, mapview, or leaflet package and save it as a HTML file.
# Create static map
tm_shape(covid) +
tm_polygons("total",
style = "quantile",
title = "NYC COVID-19 Total Cases",
palette = "PuBuGn") +
# Add interactivity
tmap_mode("view")
## tmap mode set to interactive viewing
tmap_last()
## tmap mode set to interactive viewing