Control legend behavior using Plotly's highlight feature with ggplotly

I asked this over at stack overflow here yesterday, and while it hasn't been that long, I figured this might be a better place to pose my question.

I've spent a good deal of time trying to figure out how to customize or control the legend behavior of Plotly when using Plotly's highlight() feature in combination with plotly::ggplotly(). Here is an example:

library(ggplot2)
library(plotly)
library(dplyr)

test_data <- data.frame( 
  name = LETTERS[1:5], 
  metric = sample(x = c(500:800), size = 100)
  ) %>% 
  arrange(name) %>% 
  group_by(name) %>% 
  mutate(index = row_number()) %>% 
  data.frame()


plotly_obj <- ggplotly(
  test_data %>% 
    highlight_key(~name) %>% 
    ggplot() + 
    geom_line(
      aes(
        x = index, 
        y = metric, 
        color = name
        ), 
      size = .75
      ) + 
    scale_x_continuous(breaks = c(1:20)) +
    scale_color_brewer(palette = "Accent") + 
    theme_bw()
  )

highlight(
  plotly_obj, 
  on = "plotly_hover",
  off = "plotly_doubleclick",
  opacityDim = .1
  ) %>% 
  config(displayModeBar = F)

This produces a chart that is very close to what I'm looking for, however, the legend reacts to the hover highlighting by adding a second instance of the group being highlighted in the legend:

Chart 1

I found a way to completely disable the legend behavior with highlight by adding selected = attrs_selected(showlegend = FALSE) inside the function above (after the opacityDim = .1 line), which produces the following:

Chart 2

Is there a way to force the legend to highlight the group/line that is currently selected via hover inside the legend instead of either 1.) creating a duplicate highlighted item in the legend like the first chart or 2.) completely disabling the legend in the second chart? I'm by no means an expert with Plotly, and I have a feeling my issues may be the result of my ignorance regarding Plotly's crosstalk mechanic and possibly not understanding how ggplotly is passing the aes() features to the legend. However, I know very little JavaScript, and I have yet to find a way to force the legend to behave this way. Please let me know if I'm missing something.

The only thing I can add is that the problem does not seem to be ggplotly-related, as the same problem arises when the plot is generated directly via plot_ly.

library(ggplot2)
library(plotly)
library(dplyr)

test_data <- data.frame( 
  name = LETTERS[1:5], 
  metric = sample(x = c(500:800), size = 100)
) %>% 
  arrange(name) %>% 
  group_by(name) %>% 
  mutate(index = row_number()) %>% 
  data.frame()

test_data %>% 
  highlight_key(~name) %>%
  plot_ly(x=~index, y=~metric, color=~name, type="scatter", mode="lines") %>%  
  highlight(on = "plotly_hover")
1 Like

I should’ve done this myself as well so thank you, but I thought it may have been an issue with ggplotly. I wonder if this is a bug or if it’s working as intended? And would changing this require digging into the JS/JSON? I wonder. No one has answered on Stack.

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