Geographical Data Visualization is a Snap using Leaflet
--
The Leaflet package is the de facto package for creating interactive maps in R. There is little wonder why. Dynamic, intuitive and yielding pleasant results, it scratches an itch that R users had for a long time. It can help create maps that inform, guide decisions, and in some cases even entertain.
Some of its Amazing Features
- Interactive panning/zooming
- Compose maps using unique combinations of:
- Map tiles
- Markers
- Polygons
- Lines
- Popups
- GeoJSON capability
You can also create maps right from the R console, RStudio or other IDE.
- Embed the maps in knitr/R Markdown documents and Shiny apps
- Easily render spatial objects from the
sp
orsf
packages, or data frames with latitude/longitude columns - Utilize map bounds and mouse events to drive Shiny logic
- Display maps in non-spherical mercator projections. This means mapping of ellipsoidal latitude and longitude coordinates onto a plane without it wrapping around a sphere, and thus showing accurate relative landmass sizes
- Augment map features using chosen plugins from leaflet plugins repository
To install this R package, run this command at your R prompt:
install.packages('leaflet','magrittr')
library(leaflet)
library(magrittr)
Once you have installed and loaded it, you can use this package at the R console, within R Markdown documents, and within Shiny applications.
You create a Leaflet map with these basic steps:
- Create a map widget by calling
leaflet()
. - Add layers (i.e., features) to the map by using layer functions (e.g.
addTiles
,addMarkers
,addPolygons
) to modify the map widget.
Notice how this logic is similar to the “grammar of graphics.” If you are already using ggplot, leaflet ends up being very intuitive.