Legend Highlight on Leaflet Map

Hi All,

I wanted to implement legend highlight on the Leaflet Map. If you run the below code that I have, you will be able to see the state getting highlighted (with a dark black color surrounding the state) once you hover over it. You will be able to see the legends on the bottom left. What I want to do is, once you hover over one of the quantiles or range of values in the legend, I should be able to see the states highlighted specific to that quantile or range of value that is hovered over.

Below is the code. I copied the code from "Displaying shapes with custom projections" from this link: https://rstudio.github.io/leaflet/projections.html . I have done some modifications to have the legends and also the highlighting part of each state.

library(sp)
library(leaflet)
library(albersusa)

spdf <- rmapshaper::ms_simplify(usa_sf(), keep = 0.1)
pal <- colorQuantile("Blues", domain = spdf$pop_2014, n=5)
epsg2163 <- leafletCRS(
crsClass = "L.Proj.CRS",
code = "EPSG:2163",
proj4def = "+proj=laea +lat_0=45 +lon_0=-100 +x_0=0 +y_0=0 +a=6370997 +b=6370997 +units=m +no_defs",
resolutions = 2^(16:7))

leaflet(spdf, options = leafletOptions(crs = epsg2163)) %>%
addPolygons(weight = 1, color = "#444444", opacity = 1,
fillColor = ~pal(pop_2014),
highlightOptions = highlightOptions(color = "black", weight = 2.5, bringToFront = TRUE),fillOpacity = 0.7, smoothFactor = 0.5,
label = ~paste(name, pop_2014),
labelOptions = labelOptions(direction = "auto"))%>%
addLegend(pal = pal, values = spdf$pop_2014, opacity = 0.7, title = "Coverage Rates (%)",
position = "bottomleft")

Need someones help for this. Let me know if you need anymore information.

Thanks in advance.

This is a verbatim duplicate of the post here:

Hi All,

I wanted to implement legend highlight on the Leaflet Map. If you run the below code that I have, you will be able to see the state getting highlighted (with a dark black color surrounding the state) once you hover over it. You will be able to see the legends on the bottom left. What I want to do is, once you hover over one of the quantiles or range of values in the legend, I should be able to see the states highlighted specific to that quantile or range of value that is hovered over.

Below is the code. I copied the code from "Displaying shapes with custom projections" from this link: https://rstudio.github.io/leaflet/projections.html . I have done some modifications to have the legends and also the highlighting part of each state.

library(sp)
library(leaflet)
library(albersusa)

spdf <- rmapshaper::ms_simplify(usa_sf(), keep = 0.1)
pal <- colorQuantile("Blues", domain = spdf$pop_2014, n=5)
epsg2163 <- leafletCRS(
crsClass = "L.Proj.CRS",
code = "EPSG:2163",
proj4def = "+proj=laea +lat_0=45 +lon_0=-100 +x_0=0 +y_0=0 +a=6370997 +b=6370997 +units=m +no_defs",
resolutions = 2^(16:7))

leaflet(spdf, options = leafletOptions(crs = epsg2163)) %>%
addPolygons(weight = 1, color = "#444444", opacity = 1,
fillColor = ~pal(pop_2014),
highlightOptions = highlightOptions(color = "black", weight = 2.5, bringToFront = TRUE),fillOpacity = 0.7, smoothFactor = 0.5,
label = ~paste(name, pop_2014),
labelOptions = labelOptions(direction = "auto"))%>%
addLegend(pal = pal, values = spdf$pop_2014, opacity = 0.7, title = "Coverage Rates (%)",
position = "bottomleft")

Need someones help for this. Let me know if you need anymore information.

Thanks in advance.

Hi All,

I am reposting the below issue which I am still facing.

I haven't got any reply. Hence I am reposting it. Looking forward to hear from someone on this.

Thanks in advance.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.