Drawing maps with ggmap

Application exercise
Modified

March 11, 2024

Packages

library(tidyverse)
library(ggmap)

# set default theme
theme_set(theme_minimal())

Load NYC 311 reports

Let’s first load a subset of 311 service requests in New York City.1

# load data
nyc_311 <- read_csv(file = "data/nyc-311.csv")
glimpse(nyc_311)

This subset includes 311 service requests related to Food Poisoning in commercial establishments (e.g. restaurants, cafeterias, food carts).

Register a Stadia Maps API Key

Your turn: Store your Stadia Maps API key using the function

register_stadiamaps(key = "YOUR-API-KEY", write = TRUE)

replacing "YOUR-API-KEY" with your actual API key. Otherwise you will not be able to obtain map tiles and complete the application exercise.

Obtain map tiles for New York City

Your turn: Use bboxfinder.com to find bounding box coordinates for New York City. Then, use get_stamenmap() to obtain map tiles for New York City and visualize the map.

Note

I recommend a zoom level of 11.

# store bounding box coordinates
nyc_bb <- c(
  left = TODO,
  bottom = TODO,
  right = TODO,
  top = TODO
)

nyc <- get_stadiamap(
  bbox = nyc_bb,
  zoom = TODO
)

# plot the raster map
TODO(nyc)

Food poisoning rates

A video clip from the TV show 'Parks and Recreation' where a character is in a hospital gown and saying to himself in a mirror 'Stop. Pooping.'

The COVID-19 pandemic caused massive disruption in the restaurant industry. Due to social distancing measures and lockdowns, restaurant traffic decreased significantly.

While this had significant financial ramifications, one potentially overlooked consequence is the impact on food poisoning rates. With fewer people eating out, the number of food poisoning complaints may have decreased.

Your turn: Visualize the geospatial distribution of complaints related to food poisoning in NYC in March, April, and May over a four-year time period (2018-21). Construct the chart in such a way that you can make valid comparisons over time and geographically. What impact did COVID-19 have on food poisoning cases in NYC? Did it vary geographically?

nyc_covid_food_poison <- nyc_311 |>
  # generate a year variable
  mutate(year = year(created_date)) |>
  # only keep reports in March, April, and May from 2018-21
  filter(month(created_date) %in% 3:5, year %in% 2018:2021)

# add code here

Visualize food poisoning complains on Roosevelt Island

Your turn: Now focus on food poisoning complaints on or around Roosevelt Island.2 Use get_stamenmap() to obtain map tiles for the Roosevelt Island region and overlay with the food poisoning complaints. What type of chart is more effective for this task?

Note

Consider adjusting your zoom for this geographic region.

# Obtain map tiles for Roosevelt Island
roosevelt_bb <- c(
  left = TODO,
  bottom = TODO,
  right = TODO,
  top = TODO
)
roosevelt <- get_stadiamap(
  bbox = roosevelt_bb,
  zoom = TODO
)

# add code here

Footnotes

  1. These reports were obtained from the NYC Open Data portal API.↩︎

  2. Also the location of Cornell Tech.↩︎