Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add different legends in different layers on leaflet map in R

Tags:

r

leaflet

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 that when I click on "1st layer" then only "legend_df1" will appear and when I click on "2nd layer" then only "legend_df2" will appear and "legend_df1" will be vanished. Therefore, in each layer different legends will appear, not both legends together. Can anybody please help me out?

like image 331
shashank Avatar asked Jun 14 '16 09:06

shashank


People also ask

How to add legend in leaflet R?

Use the addLegend function to add a legend. The easiest way to use addLegend is to provide pal (a palette function, as generated from colorNumeric et al.) and values , and let it calculate the colors and labels for you.


1 Answers

This is now possible with overlayGroups

library(leaflet)

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 = "inferno",
  domain = df1$value
)

pal2 <- colorNumeric(
  palette = "viridis",
  domain = df2$value
)

leaflet() %>% 
  addProviderTiles(providers$CartoDB.DarkMatter) %>% 
  addCircleMarkers(data=df1, lng=~long, lat=~lat, 
                   color = ~pal1(value),
                   group="group_1") %>%
  addCircleMarkers(data=df2, lng=~long, lat=~lat, 
                   color = ~pal2(value),
                   group="group_2") %>%
  addLegend("bottomright", pal = pal1, title="grp1",
            values = df1$value, group="group_1") %>%
  addLegend("bottomright", pal = pal2, title="grp2",
            values = df2$value, group="group_2") %>%
  addLayersControl(overlayGroups = c("group_1","group_2"),
                   options = layersControlOptions(collapsed = FALSE))

enter image description here

like image 90
Rich Pauloo Avatar answered Oct 16 '22 03:10

Rich Pauloo