North Atlantic Storms

Mapping Hurricanes Data II

Gaston Sanchez

Maps

Leaflet: Web Interactive Maps

Leaflet

Leaflet is a JavaScript library used to build interactive maps for websites and web mapping applications.

The R package "leaflet" lets you create and customize Leaflet maps in R.

These maps can be used directly from the R console, from RStudio, in Markdown documents (e.g. qmd, Rmd), and in Shiny applications.

Basic Usage

You create a Leaflet map with these basic steps:

  1. Create a map widget by calling leaflet().

  2. Add layers (i.e. features) to the map by using layer functions (e.g.  addTiles, addMarkers, addPolygons) to modify the map widget.

  3. Repeat step 2 as desired.

  4. Print the map widget to display it.

Leaflet: Basic Example

View Code
# view with marker at UC Berkeley
leaflet() |>
  addTiles() |> # default OpenStreetMap map tiles
  addMarkers(lng = -122.2579, lat = 37.8718, popup = "Berkeley")

Basic Example: Map and Marker

View Code
# view with marker at Berkeley, CA
leaflet() |>
  addTiles() |>
  setView(lng = -122, lat = 38, zoom = 8) |>
  addMarkers(lng = -122.27, lat = 37.87, popup = "Berkeley")

Basic Example: Map and Marker (cont’d)

View Code
# view with marker at Berkeley, CA
leaflet() |>
  addTiles() |>
  setView(lng = -100, lat = 37, zoom = 4) |>
  addMarkers(lng = -122.27, lat = 37.87, popup = "Berkeley")

Map Tiles

Leaflet uses map tiles from:

Adding Data Objects

Both leaflet() and the map layer functions have an optional data parameter that is designed to receive spatial data in one of several forms:

  • From base R:
    • lng/lat matrix
    • data frame with lng/lat columns
  • From the "sf" package:
    • the data frame of class "sf"
  • From the "maps" package
    • the data frame from returned from map()

Example: adding "sf" data polygons

Leaflet Maps

View Code
storms |>
  filter(year == 1975) |>
leaflet() |>
  setView(lng = -50, lat = 30, zoom = 3) |>
  addTiles() |>
  addProviderTiles(provider = "NASAGIBS.ViirsEarthAtNight2012") |>
  addCircleMarkers(
    lng = ~long, 
    lat = ~lat,
    radius = 1, 
    color = "#DDFF03")

Leaflet Maps

View Code
storms |>
  filter(year == 1975) |>
leaflet() |>
  setView(lng = -50, lat = 30, zoom = 3) |>
  addTiles() |>
  addProviderTiles(provider = "NASAGIBS.ViirsEarthAtNight2012") |>
  addCircleMarkers(
    lng = ~long, 
    lat = ~lat,
    radius = 1, 
    color = "#DDFF03", 
    weight = ~wind/5)