Lesson 6 Interactive Map: leaflet

6.1 Overview

Questions

  • How do you generate interactive plots of spatial data to enable pan, zoom and hover/click for more detail?

Objectives

Learn variety of methods for producing interactive spatial output using libraries:

  • plotly: makes any ggplot2 object interactive
  • mapview: quick view of any spatial object
  • leaflet: full control over interactive map

6.2 Things You’ll Need to Complete this Tutorial

R Skill Level: Intermediate - you’ve got basics of R down.

We will continue to use the sf and raster packages and introduce the plotly, mapview, and leaflet packages in this tutorial.

6.4 States: plotly

The plotly::ggplotly() function outputs a ggplot into an interactive window capable of pan, zoom and identify.

6.5 States: mapview

The mapview::mapview() function can work for a quick view of the data, providing choropleths, background maps and attribute popups. Performance varies on the object and customization can be tricky.

6.6 States: leaflet

The leaflet package offers a robust set of functions for viewing vector and raster data, although requires more explicit functions.

6.6.1 Choropleth

Drawing from the documentation from Leaflet for R - Choropleths, we can construct a pretty choropleth.

6.6.2 Popups and Legend

Adding a legend and popups requires a bit more work, but achieves a very aesthetically and functionally pleasing visualization.

6.7 Challenge: leaflet for regions

Use Lesson ?? final output to create a regional choropleth with legend and popups for percent water by region.

6.7.1 Answers

6.8 Raster: leaflet

TODO: show raster overlay using NEON raster dataset example

6.9 Key Points

  • Interactive maps provide more detail for visual investigation, including use of background maps, but is only relevant in a web context.
  • Several packages exist for providing interactive views of data.
  • The plotly::ggplotly() function works quickly if you already have a ggplot object, which is best for static output.
  • The mapview::mapview() function can work for a quick view of the data, providing choropleths, background maps and attribute popups. Performance varies on the object and customization can be tricky.
  • The leaflet package provides a highly customizable set of functions for rendering of interactive choropleths with background maps, legends, etc.