Leaflet is used for creating interactive maps. Many websites use it to create maps for reporting purposes. This document serves as an brief introduction to some of its features.

The package leaflet must be installed. We call it up with the library function. It is best to use the “pipe” operator %>% to pass data as the function calls can become quite complex.

The basic steps used in creating a map in leaflet:

  1. Create a map widget by calling leaflet().
  2. Add layers to the map by using layer functions to modify the map widget.
  3. Repeat step 2 as many times as you like
  4. Print the map widget to display it.

Here is a very basic map centered on our classroom. m is the map widget. We use addTiles() to add a base map then we use addMarkers to identify UCLA. Then we print the map.

library(leaflet)
m <- leaflet() %>%
addTiles() %>%  
addMarkers(lng=-118.441110, lat=34.070112, popup="UCLA")
m  # Print the map

We can incorporate the maps library available in R. We can overlay a map of the United States. We extract the US map and save it to a new object called mapStates. A new map widget is created and the state boundaries are shaded.

library(maps)
## 
##  # ATTENTION: maps v3.0 has an updated 'world' map.        #
##  # Many country borders and names have changed since 1990. #
##  # Type '?world' or 'news(package="maps")'. See README_v3. #
mapStates <- map("state", fill = TRUE, plot = FALSE)
leaflet(data = mapStates) %>% 
   addTiles() %>%
   addPolygons(fillColor = topo.colors(10, alpha = NULL), stroke = FALSE)

Just to show you that we can use other nations.

mapStates = map("france", fill = TRUE, plot = FALSE)
leaflet(data = mapStates) %>% 
   addTiles() %>%
   addPolygons(fillColor = topo.colors(10, alpha = NULL), stroke = FALSE)

We have more choices for a basemap. You can use addWMSTiles() to add WMS (Web Map Service) tiles. The map below shows the Base Reflectivity (a measure of the intensity of precipitation) using the WMS from the Iowa Environmental Mesonet:

leaflet() %>% 
  addTiles() %>% 
  setView(lng = -98.5795, lat = 39.8282, zoom = 4) %>%
  addWMSTiles(
    "http://mesonet.agron.iastate.edu/cgi-bin/wms/nexrad/n0r.cgi",
    layers = "nexrad-n0r-900913",
    options = WMSTileOptions(format = "image/png", transparent = TRUE),
    attribution = "Weather data © 2012 IEM Nexrad"
  )

The weather map is current but would be updated every time we access the weather web site.

We can add many markers to our maps. All we need is a vector of longitudes and corresponding latitudes for our points. Here, we are accessing a dataset called volcanoes.csv which has the latitude and longitude for large volcanoes.

volcanoes <- read.csv("http://www.stat.ucla.edu/~vlew/datasets/volcanoes.csv")  
leaflet(data = volcanoes[,]) %>% 
  addTiles() %>%
  addMarkers(~Longitude, ~Latitude, popup = ~as.character(Name))

We are not limited to this marker, we can have any marker we want and we can resize it. Here, we create VolcIcons (a volcano icon) from one of Google’s map icons. We resize it using a few options and add it to our map.

VolcIcons <- icons(
  iconUrl = "http://maps.google.com/mapfiles/kml/shapes/volcano.png",
  iconWidth = 19, iconHeight = 23
)

leaflet(data = volcanoes[,]) %>% 
  addTiles() %>%
  addMarkers(~Longitude, ~Latitude, popup = ~as.character(Name), icon=VolcIcons)

We can also add our own shapes to a map. This is a little advanced, but there exist information stored in the form of a JSON file (Java Script Object Notation). We have a lot of information on zip codes, leaflet can transform the information into something mappable using the addTopoJSON function.

topoData <- readLines("zips_us_topo.json") %>% 
paste(collapse = "\n")
## Warning in readLines("zips_us_topo.json"): incomplete final line found on
## 'zips_us_topo.json'
leaflet() %>% setView(lng = -98.5795, lat = 39.8282, zoom = 4) %>%
  addTiles() %>%
  addTopoJSON(topoData, weight = 2, color = "#444444", fill = TRUE)