class: center, middle, inverse, title-slide # Lec 30: Interactive Maps ## SDS 192: Introduction to Data Science ###
Shiya Cao
Statistical & Data Sciences
, Smith College
###
Fall 2024
--- # Today's Learning Goals * Create interactive maps in `leaflet`. --- # `leaflet` for R * Start by calling `leaflet()` function, setting the original view, and adding basemap tiles. ```r library(leaflet) leaflet() |> addTiles() |> addMarkers( lng = -72.64039, lat = 42.31622, popup = "Smith College" ) ``` --- # `leaflet` Supports `sf` ```r library(sf) library(maps) library(tidyverse) # Load maps package map object of US counties USA_counties <- maps::map("county", plot = FALSE, fill = TRUE) # Convert maps package map object to sf object using st_as_sf() USA_counties_sf <- USA_counties |> st_as_sf(coords = c("x", "y")) |> st_transform(crs = 4326) ``` ```r # Filter to Massachusetts MA_counties_sf <- USA_counties_sf |> separate(ID, c("State", "County"), sep = ",") |> filter(State == "massachusetts") rownames(MA_counties_sf) <- NULL ``` --- # Plotting `sf` objects in `leaflet` ```r leaflet() |> addTiles()|> addPolygons(data = MA_counties_sf) ``` --- # Projections Checklist * Are you using `leaflet`? * Project everything in EPSG 4326. * Are you using `ggplot2`? * Choose a projection scheme that is appropriate for your data. * Project everything in that scheme. --- # `macleish` Package * Weather data * Temperature, wind speed, humidity, pressure, rainfall, etc. * Spatial data * Buildings, streams, forests, slopes, etc. ```r library(macleish) ``` --- # Checking Projections ```r macleish_layers |> pluck("buildings") |> st_crs() ``` ``` Coordinate Reference System: User input: EPSG:4326 wkt: GEOGCRS["WGS 84", DATUM["World Geodetic System 1984", ELLIPSOID["WGS 84",6378137,298.257223563, LENGTHUNIT["metre",1]]], PRIMEM["Greenwich",0, ANGLEUNIT["degree",0.0174532925199433]], CS[ellipsoidal,2], AXIS["geodetic latitude (Lat)",north, ORDER[1], ANGLEUNIT["degree",0.0174532925199433]], AXIS["geodetic longitude (Lon)",east, ORDER[2], ANGLEUNIT["degree",0.0174532925199433]], USAGE[ SCOPE["Horizontal component of 3D system."], AREA["World."], BBOX[-90,-180,90,180]], ID["EPSG",4326]] ``` --- # Colors in `leaflet` ```r library(macleish) wetlands <- macleish_layers |> pluck("wetlands") leaflet() |> addTiles()|> addPolygons(data = wetlands, color = ~SHAPE_LEN, popup = ~IT_VALDESC) ``` --- # `leaflet` Uses Color Palette Functions * For numerical variables: * `colorNumeric()`: Maps numbers to colors in a specified palette. * `colorBin()`: Maps numbers into a specified intervals (e.g. 0-10, >10-20, etc.). * `colorQuantile()`: Maps numbers into a specified number of buckets (intervals calculated automatically). * For categorical variables: * `colorFactor()`: Maps categories into a specified number of buckets. * All take two arguments: * `palette` (can be `RColorBrewer` or `viridis`). * `domain` (can be `NULL`). --- # Step 1: Define the Color Palette .pull-left[ ```r wetland_pal <- colorNumeric( palette = "viridis", domain = wetlands |> pull(SHAPE_LEN) ) ``` * This is now a function that * takes a **SHAPE_LEN** * returns an HTML **color** ] .pull-right[ ```r wetland_pal(339.8323) ``` ``` [1] "#355E8D" ``` ```r wetland_pal(799.8939) ``` ``` [1] "#FDE725" ``` ] --- # Step 2: Use the Palette to Map to Color ```r leaflet() |> addTiles() |> addPolygons(data = wetlands, color = ~wetland_pal(SHAPE_LEN), fillOpacity = 0.8, weight = 0.1, popup = ~IT_VALDESC) ```