Skip to content

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

Open
@sergiballestar

Description

@sergiballestar

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions