walkerke / mapgl

R interface to Mapbox GL JS v3 and Maplibre GL JS
https://walker-data.com/mapgl
Other
91 stars 5 forks source link

Feature Request: Link legends to layers using layer groups #56

Open mstavro opened 2 weeks ago

mstavro commented 2 weeks ago

Is it currently possible to link legends to map layers, like the leaflet package does via the group argument? For example, if I have a map with two layers using two color scales, I’d like to bind one legend to each layer, rather than having two legends appear added to one another.

As always, thanks for this great package.

walkerke commented 2 weeks ago

Thanks for the note! Multiple legends on the map are possible with the argument add = TRUE. See this issue:

https://github.com/walkerke/mapgl/issues/45

Is that what you are looking for, or something different?

One improvement I need to make is off-setting legends in the same position, but you can put them in different parts of the map.

mstavro commented 2 weeks ago

Hi Kyle. Sort of - based on issue 45, I’m looking for that clear_legend() functionality but outside of a Shiny app, say a Quarto document instead.

The solution for add = TRUE works for maps with only a few layers, but I’ve run into situations where I will have maps with a lot of layers using continuous gradients (say, 5 layers with 5 color ramps, so 5 legends needed). In this case, there’s just a lot of clutter.

In leaflet, I was able to deal with this by setting a group argument which:

  1. Displays the legend when the group layer is activated;
  2. Hides the legend when the group layer is deactivated.

It’s that second part mainly that I’m suggesting - deactivating legends for layers that aren’t active. Might be easier said than done. See the bottom left in the image below.

A visual example of how this works with leaflet using the checkboxes, to my knowledge radio buttons don’t work well with this functionality, at least in the R wrapper of leaflet.

Sidebar: currently when I use visibility = none on a layer, the button in the layer dropdown currently gives the user the impression that the layer is active: the button background is gray when it should be white, indicating the layer is invisible - when the user clicks, the button is still gray (rightfully so, since the layer is now visible). Not a big deal, but a little confusing if the user is trying to toggle layers off that appear underneath the top layer.

walkerke commented 2 weeks ago

Ah I see - thanks for the example. That would be a new feature. I think it would be do-able - we currently offer the ability to identify legends by an ID; they could be linked to a layer and conceivably turned on-off by hiding the div element when the corresponding layer in the layer switcher is clicked.