To install this R package, run this command at your R prompt:
# to install the development version from Github, run
You create a Leaflet map with these basic steps:
1. Create a map widget by calling it
2. Add layers to the map by using layer functions (e.g. addTiles, addMarkers, addPolygons) to modify the map widget.
3. Print the map widget to display it.
Here’s a basic example:
addTiles() %>% # Add default OpenStreetMap map tiles
addMarkers(lng=174.768, lat=-36.852, popup="The birthplace of R")
m # Print the map
In case you’re not familiar with the magrittr pipe operator (
%>%), here is the equivalent without using pipes:
m <- leaflet()
m <- addTiles(m)
m <- addMarkers(m, lng=174.768, lat=-36.852, popup="The birthplace of R")
leaflet() returns a Leaflet map widget, which stores a list of objects that can be modified or updated later. Most functions in this package have an argument map as their first argument, which makes it easy to use the pipe operator %>% in the magrittr package, as you have seen from the example below.
The map widget can be initialized with certain parameters. This is achieved by populating the options argument as shown below.
# Set value for the minZoom and maxZoom settings.
leaflet(options = leafletOptions(minZoom = 0, maxZoom = 18))
leafletOptions() can be passed by any option described in the leaflet reference document. Using the
leafletOptions(), you can set a custom CRS and have your map displayed in a non-spherical Mercator projection, as described in projections.
You can manipulate the attributes of the map widget using a series of methods:
setView() sets the center of the map view and the zoom level.
fitBounds() fits the view into the rectangle [lng1, lat1] – [lng2, lat2].
clearBounds() clears the bound so that the view will be automatically determined by the range of latitude/longitude data in the map layers if provided.
The Data Object
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:
data frame with lng/lat columns
From The SP Package:
The data argument is used to derive spatial data for functions that need it. For example, if data is a SpatialPolygonsDataFrame object, then calling addPolygons on the map widget will let you know to add the polygons from that SpatialPolygonsDataFrame.
It is straightforward to derive these variables from sp objects, since they always represent spatial data in the same way. On the other hand, for a normal matrix or data frame, any numeric column could potentially contain spatial data. So, we resort to guessing based on column names:
The latitude variable is guessed by looking for columns named lat or latitude (case-insensitive.)
The longitude variable is guessed by looking for lng, long, or longitude.
You can always explicitly identify latitude/longitude columns by providing lng and lat arguments to the layer function.
For example, we do not specify the values for the arguments lat and lng in
addCircles() below, but the columns Lat and Long in the data frame df will be automatically used:
# add some circles to a map
df = data.frame(Lat = 1:10, Long = rnorm(10))
leaflet(df) %>% addCircles()
You can also explicitly specify the Lat and Long columns:
leaflet(df) %>% addCircles(lng = ~Long, lat = ~Lat)
A map layer may use a different data object to override the data provided in
leaflet(). We can rewrite the above example as:
leaflet() %>% addCircles(data = df)
leaflet() %>% addCircles(data = df, lat = ~ Lat, lng = ~ Long)
Below are examples of using sp and maps, respectively:
Sr1 = Polygon(cbind(c(2, 4, 4, 1, 2), c(2, 3, 5, 4, 2)))
Sr2 = Polygon(cbind(c(5, 4, 2, 5), c(2, 3, 2, 2)))
Sr3 = Polygon(cbind(c(4, 4, 5, 10, 4), c(5, 3, 2, 5, 5)))
Sr4 = Polygon(cbind(c(5, 6, 6, 5, 5), c(4, 4, 3, 3, 4)), hole = TRUE)
Srs1 = Polygons(list(Sr1), "s1")
Srs2 = Polygons(list(Sr2), "s2")
Srs3 = Polygons(list(Sr4, Sr3), "s3/4")
SpP = SpatialPolygons(list(Srs1, Srs2, Srs3), 1:3)
leaflet(height = "300px") %>% addPolygons(data = SpP)
mapStates = map(“state”, fill = TRUE, plot = FALSE)
leaflet(data = mapStates) %>% addTiles() %>%
addPolygons(fillColor = topo.colors(10, alpha = NULL), stroke = FALSE)
The Formula Interface
The arguments of all layer functions can take normal R objects, such as a numeric vector for the lat argument, or a character vector of colors for the color argument. They can also take a one-sided formula, in which case the formula will be evaluated using the data argument as the environment. For example, ~ x means the variable x in the data object and you can write arbitrary expressions on the right-hand side, for example, ~ sqrt(x + 1).
m = leaflet() %>% addTiles()
df = data.frame(
lat = rnorm(100),
lng = rnorm(100),
size = runif(100, 5, 20),
color = sample(colors(), 100)
m = leaflet(df) %>% addTiles()
m %>% addCircleMarkers(radius = ~size, color = ~color, fill = FALSE)
m %>% addCircleMarkers(radius = runif(100, 4, 10), color = c('red'))
Now, let’s move on to the first set of real exercises on the leaflet package!