plotly / plotly/plotly.R

highlight() fails in lineplots after crosstalk filter if plot_ly()'s "color" argument matches highlight_key()'s key

Open
#1,540 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
R
Stars
2.7k
Forks
641
PR merge metrics
No merged PRs in 30d

Description

I am making an interactive plotly plot with R using highlight() and crosstalk filter_select() functions. However, when the argument "color" of plotly's lines match highlight_key(), it becomes impossible to highlight any light of the filtered data in the plot.

Let's say you pick up the example in "Interactive web-based data visualization with R, plotly, and shiny" by Carson Sievert, in the Figure 16.10 of the chapter 16.1 "Graphical queries"
The example provided by the book works fine, you can highlight an item after filtering data:

library(gapminder)
g <- highlight_key(gapminder, ~country)
continent_filter <- filter_select("filter", "Select a country", g, ~continent)

p <- plot_ly(g) %>%
  group_by(country) %>%
  add_lines(x = ~year, y = ~lifeExp, color = ~continent) %>%
  layout(xaxis = list(title = "")) %>%
  highlight(selected = attrs_selected(showlegend = FALSE))

bscols(continent_filter, p, widths = 12)

enter image description here

However, when "color" argument of add_lines() matches highlight_key() variable (in this case, ~country) it is not possible to filter AND highlight the filtered data

library(gapminder)
g <- highlight_key(gapminder, ~country)
continent_filter <- filter_select("filter", "Select a country", g, ~continent)

p <- plot_ly(g) %>%
  group_by(country) %>%
  # Please mind that in the next line IS NOT "color = ~continent" as in the last example
  add_lines(x = ~year, y = ~lifeExp, color = ~country) %>%
  layout(xaxis = list(title = "")) %>%
  highlight(selected = attrs_selected(showlegend = FALSE))

bscols(continent_filter, p, widths = 12)

enter image description here

Is it possible to highlight items in an interactive plot_ly lineplot (because I've checked in boxplot and it works) when the color argument of plotly's plot matches the highlight_key?
I've also tried with geom_line() + ggplotly() but won't work neither

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the minimal R reproduction using plot_ly(), add_lines(), highlight_key(), and filter_select(), comparing color = ~country with color = ~continent. Trace how line plots handle crosstalk filtering and highlighting, then verify that filtered items can still be highlighted when color matches the highlight_key() variable.

Written by the indexing model from the issue text.

Assessment

Tech stack
r
Domain
data-visualization
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.