Black Lives Matter. Please consider donating to Black Girls Code today.

Legend Highlight on Plotly Map

Hi All,

I wanted to implement legend highlight on plotly Map.

Below is the plotly code that I have taken from this link: https://plotly.com/r/choropleth-maps/

library(plotly)
df <- read.csv(“https://raw.githubusercontent.com/plotly/datasets/master/2011_us_ag_exports.csv”)
df$hover <- with(df, paste(state, ‘
’, “Beef”, beef, “Dairy”, dairy, “
”,
“Fruits”, total.fruits, “Veggies”, total.veggies,

”, “Wheat”, wheat, “Corn”, corn))
l <- list(color = toRGB(“white”), width = 2)
g <- list(
scope = ‘usa’,
projection = list(type = ‘albers usa’),
showlakes = TRUE,
lakecolor = toRGB(‘white’)
)
fig <- plot_geo(df, locationmode = ‘USA-states’)
fig <- fig %>% add_trace(
z = ~total.exports, text = ~hover, locations = ~code,
color = ~total.exports, colors = ‘Purples’
)
fig <- fig %>% colorbar(title = “Millions USD”)
fig <- fig %>% layout(
title = ‘2011 US Agriculture Exports by State
(Hover for breakdown)’,
geo = g
)
fig

I have 2 things:

  1. In leaflet, we have something called highlightOptions to highlight the specific state once you hover over it. If you run the above code that I have, you will be able to see a neat choropleth map. Just like we have highlightOptions under leaflet, I want to know whether there is an option within plotly maps for highlighting the state (with a dark black color border surrounding the state) once you hover over it.

  2. The above has the legends as a color bar on the side. But I want to display the legends as quantiles. We have a concept called addLegend in leaflet as we see here: https://rstudio.github.io/leaflet/legends.html. You will be able to see the legends on the bottom left. What I want to do is, I want to have the legends as quantiles in plotly and also once you hover over one of the quantiles or range of values in the legend, I should be able to see the states highlighted (with a dark black color border surrounding the state) specific to that quantile or range of value that is hovered over.

Please guide to get this done in plotly maps. I know it is possible using leaflet.

Thanks in advance.