Classwork 9

geom_sf() and ggmap()

Author

Byeong-Hak Choe

Published

April 14, 2025

Modified

April 16, 2025

Loading R packages

library(tidyverse)
library(skimr)
library(ggthemes)
library(ggmap)
library(sf)

Question 1. NYC dog bites with geom_sf()

The following dataset is for Question 1:

# GeoPackage file (.gkpg), a modern format for map data
nyc_zips_sf <- st_read("https://bcdanl.github.io/data/nyc_zips_sf.gkpg")  # sf format
Reading layer `nyc_zips_sf' from data source 
  `https://bcdanl.github.io/data/nyc_zips_sf.gkpg' using driver `GPKG'
Simple feature collection with 262 features and 11 fields
Geometry type: POLYGON
Dimension:     XY
Bounding box:  xmin: -74.25576 ymin: 40.49584 xmax: -73.6996 ymax: 40.91517
Geodetic CRS:  WGS 84


nyc_dog_bites <- read_csv("https://bcdanl.github.io/data/nyc_dog_bites_all.csv") 


nyc_dog_license <- read_csv(
  'https://bcdanl.github.io/data/nyc_dog_license.csv')


  • Plot a ZIP code level map of dog bite rate. \[ \text{Dog Bite Rate} = \frac{\text{Number of dog bites}}{\text{Number of dogs}} \]




Question 2. Google Map Routes

  • Plot a Google Map of Manhattan showing the following three routes:
route1 <- route(
  from = "Central Park, NYC",
  to   = "Empire State Building, NYC",
  mode = "driving",
  structure = "route"
)

route2 <- route(
  from = "Empire State Building, NYC",
  to   = "Times Square, NYC",
  mode = "driving",
  structure = "route"
)

route3 <- route(
  from = "Times Square, NYC",
  to   = "Central Park, NYC",
  mode = "driving",
  structure = "route"
)
  • In your map:
    • Include a legend identifying each route;
    • Mark both origins and destinations with distinct points;
    • Add labels to clearly indicate each origin and destination.




Question 3. đźš• NYC Yellow Taxi Trip Analysis to the MET

You are provided a sample dataset of NYC yellow taxi trips from March 2016:

nyc_taxi <- read_csv("https://bcdanl.github.io/data/yellow_tripdata_2016-03-sample.csv") 


Q3a

Create a data.frame whose taxi trip drop off is at the Metropolitan Museum of Art (MET), and taxi trip pick up is within Manhattan.

  • Coordinates for the MET drop-off are defined by the range_lon_MET and range_lat_MET vectors:
range_lon_MET <- c(-73.963, -73.9615)
range_lat_MET <- c(40.7785, 40.780)
  • Coordinates for the Manhattan pick-up are defined by the range_lon and range_lat vectors:
range_lon_Manhattan <- c(-74.03, -73.92)
range_lat_Manhattan <- c(40.70, 40.88)


Q3b

From the resulting data.frame in Q3a, retrieve the Google Maps’ routes for the top 2 most expensive taxi trips to the MET.


Q3c

  • Plot a Google Map of Manhattan showing the top 2 most expensive tax trip path to the MET.
  • Visualize pick-up locations using a marker.
  • Display the label of “Metropolitan Museum of Art” on the map.

Discussion

Welcome to our Classwork 9 Discussion Board! đź‘‹

This space is designed for you to engage with your classmates about the material covered in Classwork 9.

Whether you are looking to delve deeper into the content, share insights, or have questions about the content, this is the perfect place for you.

If you have any specific questions for Byeong-Hak (@bcdanl) regarding the Classwork 9 materials or need clarification on any points, don’t hesitate to ask here.

All comments will be stored here.

Let’s collaborate and learn from each other!

Back to top