I'm trying to make my leaflet map show no legend when both layers are selected, and when only one layer is selected show that layer's legend.
I found these solutions using Javascript:
- How to switch between multiple map legends with leaflet.js?
- https://gis.stackexchange.com/questions/92257/leaflet-add-and-remove-legends-with-layer-selection
But I haven't been able to figure out how to apply these techniques in R. Can anyone point me to a document to read, or could give me a hint?
Here is 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 a snippet of code:
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.