Setp 1: Read Your Data

acsCovidDat <- sf::st_read("C:/Users/Zack/Documents/Data Analysis and Viz/WEEK9/acsPopByZip.gpkg", layer= "NYC_COVID_Pop_by_Zip")
## Reading layer `NYC_COVID_Pop_by_Zip' from data source 
##   `C:\Users\Zack\Documents\Data Analysis and Viz\WEEK9\acsPopByZip.gpkg' 
##   using driver `GPKG'
## Simple feature collection with 180 features and 13 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 913129 ymin: 120020.9 xmax: 1067113 ymax: 272710.9
## Projected CRS: NAD83 / New York Long Island (ftUS)

Step 2: Static Map of COVID-19 Cases

covid_map <- ggplot(acsCovidDat) +
  geom_sf(aes(fill = Positiv), color = "white", size = 0.1) +
  scale_fill_distiller(palette = "Reds", direction = 1, name = "COVID-19 Cases") +
  labs(title = "Confirmed COVID-19 Cases by ZIP Code in NYC") +
  theme_minimal() +
  coord_sf(datum = NA)

covid_map

Step 4: Side-by-Side Multi-Map Figure

ggarrange(covid_map, elderly_map, 
          labels = c("A", "B"),
          ncol = 2, nrow = 1)

Step 5: Add Labels to the COVID Map

label_data <- acsCovidDat %>%
  filter(Positiv > quantile(Positiv, 0.95, na.rm = TRUE)) %>%
  st_centroid() %>%
  mutate(lon = st_coordinates(.)[,1],
         lat = st_coordinates(.)[,2])

covid_map_labeled <- covid_map +
  geom_text_repel(data = label_data, 
                  aes(x = lon, y = lat, label = ZIPCODE), 
                  size = 3)

covid_map_labeled

Step 6: Replace Leaflet with tmap for RPubs

library(tmap)
tmap_mode("view")  # interactive view

tm_covid <- tm_shape(acsCovidDat %>% st_transform(4326)) +
  tm_polygons("Positiv", 
              palette = "Reds", 
              title = "COVID-19 Cases",
              popup.vars = c("ZIP Code" = "ZIPCODE", 
                             "Place" = "PO_NAME", 
                             "Cases" = "Positiv")) +
  tm_basemap("CartoDB.DarkMatter") +
  tm_view(set.view = c(-74, 40.7, 10))

tm_covid