knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
library(leaflet) library(magrittr)
The Leaflet package includes functions to show and hide map layers. You can allow users to decide what layers to show and hide, or programmatically control the visibility of layers using server-side code in Shiny.
In both cases, the fundamental unit of showing/hiding is the group.
A group is a label given to a set of layers. You assign layers to groups by using the group
parameter when adding the layers to the map.
leaflet() %>% addTiles() %>% addMarkers(data = coffee_shops, group = "Food & Drink") %>% addMarkers(data = restaurants, group = "Food & Drink") %>% addMarkers(data = restrooms, group = "Restrooms")
Many layers can belong to same group. But each layer can only belong to zero or one groups (you can't assign a layer to two groups).
You can use Leaflet's layers control feature to allow users to toggle the visibility of groups.
outline <- quakes[chull(quakes$long, quakes$lat),] map <- leaflet(quakes) %>% # Base groups addTiles(group = "OSM (default)") %>% addProviderTiles(providers$CartoDB.Positron, group = "Positron (minimal)") %>% addProviderTiles(providers$Esri.WorldImagery, group = "World Imagery (satellite)") %>% # Overlay groups addCircles( ~ long, ~ lat, ~ 10 ^ mag / 5, stroke = FALSE, group = "Quakes", fillColor = "tomato" ) %>% addPolygons( data = outline, lng = ~ long, lat = ~ lat, fill = FALSE, weight = 2, color = "#FFFFCC", group = "Outline" ) %>% # Layers control addLayersControl( baseGroups = c( "OSM (default)", "Positron (minimal)", "World Imagery (satellite)" ), overlayGroups = c("Quakes", "Outline"), options = layersControlOptions(collapsed = FALSE) ) map
addLayersControl()
distinguishes between base groups, which can only be viewed one group at a time, and overlay groups, which can be individually checked or unchecked.
Although base groups are generally tile layers, and overlay groups are usually markers and shapes, there is no restriction on what types of layers can be placed in each category.
Only one layers control can be present on a map at a time. If you call addLayersControl()
multiple times, the last call will win.
You can use showGroup()
and hideGroup()
to show and hide groups from code. This mostly makes sense in a Shiny context with leafletProxy
, where perhaps you might toggle group visibility based on input controls in a sidebar.
You can also use showGroup()
/hideGroup()
in conjunction with addLayersControl()
to set which groups are checked by default.
map %>% hideGroup("Outline")
Finally, you can remove the layers in a group using clearGroup()
. Note that this doesn't just remove the layers from the group, it also removes them from the map. (It does not, however, remove the group itself from the map; it still exists, but is empty.)
If markers are added to different groups, and when using marker clustering as described in the marker page, leaflet will generate different sets of clusters for different groups. This allows showing/hiding of marker clusters belonging to a group independently of other marker clusters in other groups.
quakes <- quakes %>% dplyr::mutate(mag.level = cut(mag,c(3,4,5,6), labels = c('>3 & <=4', '>4 & <=5', '>5 & <=6'))) quakes.df <- split(quakes, quakes$mag.level) l <- leaflet() %>% addTiles() names(quakes.df) %>% purrr::walk( function(df) { l <<- l %>% addMarkers(data=quakes.df[[df]], lng=~long, lat=~lat, label=~as.character(mag), popup=~as.character(mag), group = df, clusterOptions = markerClusterOptions(removeOutsideVisibleBounds = FALSE), labelOptions = labelOptions(noHide = FALSE, direction = 'auto')) }) l %>% addLayersControl( overlayGroups = names(quakes.df), options = layersControlOptions(collapsed = FALSE) )
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.