7

I'm wondering how to hide/toggle legends based on addLayerControl() in Leaflet for R? When option layer a is toggled, then the data of option layer b is not shown by addPolygons() because I can use group. However, the addLegend() option doesn't have the group functionality so I'm not able to hide the legend of group layer b (blue) when I selected group layer a (red):

enter image description here

source

Do you have any idea how to do this?

Regards,

Joris

bathyscapher
  • 1,615
  • 1
  • 13
  • 18
Yoorizz
  • 217
  • 2
  • 12

2 Answers2

6

In case anyone is looking at this in the future, there's a new viable method.

If you're using the development/Github version of the leaflet package, addLegend() now supports the group and layerId arguments.

(Run the command devtools::install_github('rstudio/leaflet') to install)

Even the latest CRAN version (1.1.0) supports layerID.

Assuming you go with the development version, the following code should work:

leaflet() %>%
    #Polygon Layers
    addPolygons(data = exampleDataOne, group = "Group A") %>%
    addPolygons(data = exampleDataTwo, group = "Group B") %>%

    #Legend Layers
    addLegend(values = exampleValuesOne, group = "Group A", 
              position = "bottomright") %>%
    addLegend(values = exampleValuesTwo, group = "Group B",
              position = "bottomright") %>%

    #Layers Control
    addLayersControl(overlayGroups = c("Group A","Group B"),
              options = layersControlOptions(collapsed = FALSE))

This should have the desired effect of only displaying a legend when the corresponding layer is active.

One potential issue is that by default, all leaflet layers are active. If you have overlapping polygons, this can lead to a less visually appealing map. And, if you have multiple legends, this will also be an issue.

What you can do is use the hideGroup() function.

If you wanted to have only Group A from the above example active at start, you could add

%>% hideGroup("Group B")

to the end of the code block above.

ceph
  • 96
  • 1
  • 9
  • 1
    Nice solution, however it's important to note that it (seems) doesn't work with `overlayGroups` option in `addLayersControl` – Bastien Feb 21 '18 at 18:19
  • While this explanation is very clear and concise, it didn't work for me with markers. When I assign the same group name to the `addCircles()` and `addLegend()` layers, say `Group A`, then the `%>% hideGroup("Group A")` operations only hides the markers, it doesn't make the legend go away. I am puzzled. – Sun Bee Jul 14 '19 at 03:18
4

This code worked for me:

observeEvent(input$mymap_groups,{
    mymap <- leafletProxy("mymap", data = SalesMap)
    mymap %>% clearControls()
    if (input$mymap_groups == '1') {
      mymap %>% addLegend(position="bottomright", pal=pal1, values=SalesMap$SALES, title="a")
    }
    else if (input$mymap_groups == '2') {
      mymap %>% addLegend(position="bottomright", pal=pal2, values=SalesMap$Bonnen, title="b")
    }
  })

You can use input$mymap_groups to identify what kind of group is selected.In the observeEvent() you can use an if/else statement to create a legend based on a group.

Yoorizz
  • 217
  • 2
  • 12
  • 2
    For people looking at this answer: this has the advantage of keeping the layers as baseGroups() (radio buttons) BUT only when leaflet is used in shiny. – YGS Feb 14 '19 at 20:57