Show different legends when changing layer - leaflet

I'm trying to make a map of my leaf without a legend, when both layers are selected, and when only one layer is selected, this is the legend of the layer.

I found these solutions using Javascript:

But I could not understand how to apply these methods in R. Can someone point me to a document to read or give me a hint?

Here are some test data:

df1 Tickets long lat 1 28 -71.12088 42.37418 2 4 -71.09524 42.36911 3 3 -71.11935 42.38532 df2 Tickets long lat 4 33 -71.1213 42.37401 5 1 -71.0915 42.37255 6 21 -71.1226 42.37512 7 1 -71.1016 42.36411 

Here is the code snippet:

 base_map <- leaflet() %>% addProviderTiles('Stamen.TonerLite') %>% setView(-71.128184, 42.3769824, zoom = 14) pal1 <- colorNumeric(palette = "RdBu", domain = df1$Tickets) pal2 <- colorNumeric(palette = "RdBu", domain = df2$Tickets) base_map %>% addCircles(data = df1, lng = df1$long, lat = df1$lat, color = pal1(df1$Tickets), radius = 5, group = "Group 1 Dots") %>% addLegend("bottomright", pal = pal1, values = df1$Tickets, title = "1 Dots") %>% addCircles(data = df2, lng = df2$long, lat = df2$lat, color = pal2(df2$Tickets), radius = 5, group = "Group 2 Dots") %>% addLegend("bottomright", pal = pal2, values = df2$Tickets, title = "2 Dots") %>% addLayersControl(overlayGroups = c("Group 1 Dots", "Group 2 Dots"), options = layersControlOptions(collapsed = FALSE)) 

Thanks.

+5
source share

Source: https://habr.com/ru/post/1245723/


All Articles