Leaflet package in R
- Supports interactive maps that provide features like panning and zooming.
- Supports various elements for the composition of maps such as Map tiles, Markers, Polygons, Lines, Popups, and GeoJSON.
- Supports full dynamic map creation from the R Console itself.
- Easy to embed map objects in various markdown files.
- Can also display maps in non-spherical Mercator projections
- Augment map features using chosen plugins from leaflet plugins repository
We can install the leaflet library in the R Language by using:
# installs the leaflet package install.packages("leaflet")
Run the above command in the R console and select desired cran mirror to install the Leaflet R Package.
Creating Interactive maps using leaflet package
To create a map widget using the leaflet package in the R Language we use the leaflet() function. The leaflet() function returns a map object that can be viewed in itself or can be modified by adding more layers. The basic leaflet widget shows an empty map widget with no tiles or markers.
Syntax: map <- leaflet()
Example: Basic map object made by using the leaflet() function.
Adding tiles to map
Since above code creates a map widget that is empty. To add tiles to it for the creation of the desired map we use the addTiles() function of the leaflet package. By default, if no argument is passed it creates an OpenStreetMap map tile on top of the map widget. For custom map tile we pass that map as an argument to the addTiles() function.
Syntax: map <- leaflet() %>%addTiles()
Example: Basic map with OpenStreetMap tile.
Adding Markers on Map
Now since we have created a basic map, it in itself is not that useful. we can now add markers on the map using the addMarkers() function of the leaflet package. we can add as many markers as we require by just adding new layers.
Syntax: map <- leaflet() %>%addTiles() %>% addMarkers( lng, lat, popup)
- lng: determines the longitude of the marker position
- lat: determines the latitude of the maeker position
- popup: determines the text that would be shown on marker popup
Example: Interactive map with a marker in Delhi India.
We can add multiple layers to the leaflet map using addTiles() or addMarker() function. Now if we want to give the user a choice of which layer he/she wants to look at, we can do that by using layer control. Layers can be assigned to named groups. To name a group in the leaflet we add group argument to that layer. Now, these groups can be toggled on and off by the user. The Base Groups that form the basics of the map for example tiles are selected with radio buttons so that you can only choose one at a time, and the Overlay Groups that can have multiple layers active at the same time get checkboxes.
To implement layers control, add group names to individual layers with the group = argument AND add the layers control layer using addLayersControl().
Syntax: leaflet() %> addTiles(group = “One”) %>% addProviderTiles(providers$Esri.WorldImagery, group = “Two”) %>% addLayersControl(baseGroups = c(“One”, “Two”) options = layersControlOptions(collapsed = FALSE))
Example: Basic leaflet map with two options for tiles and two markers with checkboxes.
Plot data on top of map
Spatial objects in your R environment can also be added as map layers, provided that they have a CRS defined with a datum. We can use those data points on top of the leaflet map using the addMarkers() or the addPloygons() function depending on the type of dataset. If the dataset is single-dimensional we use markers and for multidimensional data we use polygons.
Example: Plotted data variables as markers on top of the leaflet map. The data set used in the example can be downloaded here.