Polygon Mapping

SDS 192: Introduction to Data Science

Lindsay Poirier
Statistical & Data Sciences, Smith College

Fall 2022

For Today

  • Quiz 2 Posted!
  • Project 3 Template Posted!
  • Review: Projections and Coordinate Reference Systems
  • Mapping Geographic Boundaries
  • Importing and Mapping Shapefiles
  • Chloropleth Maps

Learning Check: What function do we use to encode a data frame with geometry based on lat/long data?

Learning Check: What function do we use convert to a new CRS?

Learning Check: What CRS does leaflet expect data to be in?

Mapping Polygons

  • Not all cartographic data is encoded as a latitude and longitude! Some cartographic data is encoded as regularly or irregularly shaped polygons
  • Can demarcate:
    • Administrative boundaries (e.g. census tracts, zip codes, states),
    • Feature boundaries (e.g. buildings, bodies of water, etc.)
    • Buffers (e.g. areas at a specified distance from a point source)

Administrative Boundaries: US Census

Administrative Boundaries: US Census

Administrative Boundaries: US Census

Administrative Boundary Soup

NYC Boundaries Map

Feature Boundaries

Feature Boundaries

Shapefiles

  • File for storing geospatial feature data
  • Actually a series of files (.shp, .shx, and .dbf) that must all be present in the directory for the shapefile to import.
  • Imported file ends in .shp and contains feature geometry

Importing Shapefiles

  • Function st_read() from sf package used to read in shapefiles

library(sf)
nyc_cd <- st_read("../data/nyc_community_districts/nycd.shp")
Reading layer `nycd' from data source 
  `/Users/lpoirier_1/Documents/GitHub/sds-192-public-website-quarto/website/data/nyc_community_districts/nycd.shp' 
  using driver `ESRI Shapefile'
Simple feature collection with 71 features and 3 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: 913175.1 ymin: 120128.4 xmax: 1067383 ymax: 272844.3
Projected CRS: NAD83 / New York Long Island (ftUS)

Importing Shapefiles

nyc_cd |> head(4)

Mapping Polygons with Leaflet

library(leaflet)
nyc_cd <- nyc_cd |>
  st_transform(4326)
leaflet(width = "100%") |>
  setView(lat = 40.7, lng = -74.0, zoom = 10) |> 
  addProviderTiles("CartoDB.Positron") |>
  addPolygons(data = nyc_cd)

Chloropleth Maps

  • Presents a numeric variable aggregated by a geospatial unit
  • Represents the value of the aggregated numeric variable via intensity of color
    • Values presented via a sequential or diverging palette

Mapping Population: How to Join?

library(tidyverse)
cd_pop <- read_csv("https://data.cityofnewyork.us/resource/xi7c-iiu2.csv?$select=borough,cd_number,_2010_population%20as%20population_2010")

cd_pop |> 
  select(borough,cd_number) |> 
  head(5)
nyc_cd |> 
  select(BoroCD) |> 
  head(5)

Cleaning Geographic ID Fields

cd_pop <- cd_pop |>
  mutate(borough_num = case_when( 
    borough == "Manhattan" ~ 1,
    borough == "Bronx" ~ 2,
    borough == "Brooklyn" ~ 3,
    borough == "Queens" ~ 4,
    borough == "Staten Island" ~ 5)) |>
  mutate(cd = str_pad(cd_number, 2, side="left", "0")) |>
  mutate(BoroCD = paste0(borough_num, cd) |> as.numeric())

nyc_cd <- 
  nyc_cd |>
  left_join(cd_pop, by = c("BoroCD" = "BoroCD"))

NYC Commmunity District Chloropleth: Numeric

library(RColorBrewer)
pal_num <- colorNumeric(palette = "YlOrRd", 
                        domain = nyc_cd$population_2010)

leaflet(width = "100%") |>
  setView(lat = 40.7, lng = -74.0, zoom = 10) |> 
  addProviderTiles("CartoDB.Positron") |>
  addPolygons(data = nyc_cd,
              fillColor = ~pal_num(population_2010), 
              stroke = FALSE,
              fillOpacity = 0.5) |>
  addLegend(data = nyc_cd, 
            values = ~population_2010,
            pal = pal_num,
            title = "Population")

NYC Commmunity District Chloropleth: Bin

library(RColorBrewer)
pal_bin <- colorBin(palette = "YlOrRd", 
                        domain = nyc_cd$population_2010, n = 4)

leaflet(width = "100%") |>
  setView(lat = 40.7, lng = -74.0, zoom = 10) |> 
  addProviderTiles("CartoDB.Positron") |>
  addPolygons(data = nyc_cd,
              fillColor = ~pal_bin(population_2010), 
              stroke = FALSE,
              fillOpacity = 0.5) |>
  addLegend(data = nyc_cd, 
            values = ~population_2010,
            pal = pal_bin,
            title = "Population")

NYC Commmunity District Chloropleth: Quantile

library(RColorBrewer)
pal_quant <- colorQuantile(palette = "YlOrRd", 
                        domain = nyc_cd$population_2010, n = 4)

leaflet(width = "100%") |>
  setView(lat = 40.7, lng = -74.0, zoom = 10) |> 
  addProviderTiles("CartoDB.Positron") |>
  addPolygons(data = nyc_cd,
              fillColor = ~pal_quant(population_2010), 
              stroke = FALSE,
              fillOpacity = 0.5) |>
  addLegend(data = nyc_cd, 
            values = ~population_2010,
            pal = pal_quant,
            title = "Population")