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:
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:
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.