library(tidyverse)
library(ggmap)
# set default theme
theme_set(theme_minimal())AE 16: Drawing maps with {ggmap}
Packages
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.
I recommend a zoom level of between 8-12. Trial and error should help you decide on the best value.
# 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
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-23). 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-23
filter(month(created_date) %in% 3:5, year %in% 2018:2023)
# add code hereYour turn: Now visualize the change in food complaints over time without making a map. How else could you represent this data? Does making it a map improve the understanding of the data, or add more confusion?
# add code hereVisualize food poisoning complaints 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?
- Consider adjusting your
zoomfor this geographic region. - Try a different set of map tiles. Which one looks both interpretable as well as aesthetically pleasing?
# 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 hereFootnotes
These reports were obtained from the NYC Open Data portal API.↩︎
Also the location of Cornell Tech.↩︎
