Add different legends in different layers on the booklet map in R

library(leaflet) library(htmltools) library(htmlwidgets) library(dplyr) # df1 <- data.frame(points=c("p1", "p2"), lat=c(49.47259, 49.48095), long=c(-103.7054, -103.6126), value=c(50.34, 100.25)) df2 <- data.frame(points=c("p1", "p2"), lat=c(49.47809, 49.66849), long=c(-103.5614, -103.0224), value=c(300.56, 505.34)) # pal1 <- colorNumeric( palette = "PRGn", domain = df1$value ) # pal2 <- colorNumeric( palette = "PRGn", domain = df2$value ) # n <- leaflet() %>% addTiles(group="1st layer") %>% addTiles(group="2nd layer") %>% addCircles(data=df1, lng=~long, lat=~lat, weight = 3, radius=250, color = ~pal1(value), stroke = TRUE, fillOpacity = 0.8,group="1st layer") %>% addCircles(data=df2, lng=~long, lat=~lat, weight = 3, radius=250, color = ~pal2(value), stroke = TRUE, fillOpacity = 0.8,group="2nd layer") %>% addLegend("bottomright", pal = pal1, values = df1$value, title = "legend_df1") %>% addLegend("topright", pal = pal2, values = df2$value, title = "legend_df2") %>% addLayersControl(baseGroups=c("1st layer","2nd layer"), options=layersControlOptions(collapsed = F)) n 

I want so that when clicking on “1st layer” only “legend_df1” will appear, and when I press “2nd level”, only “legend_df2” will appear, and “legend_df1” will disappear. Therefore, in each layer different legends will appear, and not both legends together. Can anyone help me out?

+7
r leaflet
source share

No one has answered this question yet.

See related questions:

657
What is the difference between the assignment operators "=" and "<-" in R?
5
Show different legends when changing layer - leaflet
2
Leaflet add / remove legends with layer selection
2
Leaflet: add the same layer to two different maps
2
Legends on elevator maps
one
Flyer Map R - Change Legends Based on Selected Layer Group
one
add a hospital layer to display in the leaflet using R
one
flyer card: remove road layer
one
Display legendary flyer map
0
add legend to flyer card

All Articles