Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R leaflet map - Change legends based on selected layer group

Tags:

r

legend

leaflet

I am making an R leaflet map (not Shiny) and I have two control groups, and based on the selection I would like a different legend to become visible. Currently I only manage to have both legends visible at all time.

Below is the code for the leaflet map, and the output can be seen in the image.

Two legends are always visible

leaflet() %>% addSearchOSM() %>% 
  addProviderTiles(providers$CartoDB.Positron,
                   options = providerTileOptions(noWrap = TRUE),
                   group = "kaart") %>%
  # addFullscreenControl() %>%
  addCircleMarkers(data = table@data,
             lat = ~lng, 
             lng = ~lat,
             color = ~palverbruikplaats(Verbruiksplaats),
             label = bepaalPopup(),
             group = "Verbruikplaatscircles"
             )%>%
  addCircleMarkers(data = table@data,
                   lat = ~lng, 
                   lng = ~lat,
                   color = ~palstatus(`Status omschrijving`),
                   label = bepaalPopup(),
                   group = "statuscircles"
                    )%>%
  leaflet::addLegend("bottomleft", pal = palverbruikplaats, values = verbruikplaatsuniek, title = "Legenda") %>%
  leaflet::addLegend("bottomleft", pal = palstatus, values = statusuniek, title = "Legenda") %>%
  addLayersControl(baseGroups = c("Verbruikplaatscircles", "statuscircles"),
                      options = layersControlOptions(collapsed = FALSE))
like image 388
Younes Avatar asked Jun 01 '18 10:06

Younes


People also ask

How do I show and hide map layers in leaflet?

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.

What is a Leaflet map?

Oftentimes Leaflet maps are designed to have multiple polygon layers. These types of maps are great for demonstrating to viewers correlations between categories such as GDP, education level, and poverty rates when they switch between layer groups. This post will cover how to create such maps. These maps can have multiple variables.

How do I add a legend to a leaflet?

map <- leaflet(countries) %>% addTiles() Use the addLegendfunction to add a legend. The easiest way to use addLegendis to provide pal(a palette function, as generated from colorNumericet al.) and values, and let it calculate the colors and labels for you.

How to toggle layers on and off in R?

Bookmark this question. Show activity on this post. In the package leaflet for R, you have the ability to toggle layers on and off with the addLayersControl () option. If you use legends for each individual layer, however, these layers are not toggled on and off with the layers control.


1 Answers

In your addLayersControl did you mean to set the overlayGroups argument instead of baseGroups?

library(leaflet)
leaflet() %>%
  addTiles(group = "OpenStreetMap") %>%
  addCircleMarkers(runif(20, -75, -74), runif(20, 41, 42), group = "Markers1", color ="red") %>%
  addMarkers(runif(20, -75, -74), runif(20, 41, 42), group = "Markers2") %>%
  addLegend(values = 1, group = "Markers1", position = "bottomleft", labels = "1", colors= "red") %>%
  addLegend(values = 2, group = "Markers2", position = "bottomleft", labels = "2" ,colors= "blue") %>%  
  addLayersControl(overlayGroups = c("Markers1", "Markers2"),
                   options = layersControlOptions(collapsed = FALSE))

enter image description here

like image 104
Weihuang Wong Avatar answered Oct 20 '22 12:10

Weihuang Wong