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

An histogram, a transforms filter, some colors and a legend

Hello everybody.

I am struggling with a problem, which might be a bug or a mistake from me… So I am either looking for a correction or a work around… or any insight that might help :slight_smile:

I am willing to create an histogram plotting various groups of my data.
Moreover, I need to apply some filter over the data.
The filter is intended to be dynamic, so I am using a transforms filter https://plotly.com/r/filter/.

So far, so good. The following Minimal Working Example does the job:

suppressMessages(library(plotly))
fig <- plot_ly(
  type = 'histogram',
  x = mtcars$hp,
  showlegend=TRUE,
  color = factor(mtcars$cyl),
  transforms=list(list(type = "filter" , target = 'x', operation = ">=",value = 100))
)
fig <- config(fig, displayModeBar = FALSE, showTips = FALSE)
htmlwidgets::saveWidget(as_widget(fig), "MWE_histo_bug.html")

Now come the trouble:
In the plot produced by the code above, the click on the legend which is supposed to enable or disable the visible attribute of the matching trace, presents a buggy behavior.
Clicking on the second or the third elements (corresponding to 6 and 8 in the MWE) works as expected: the trace is set to not visible; double clicking does not isolate the trace as expected.
Clicking on the first element (4 on the MWE) does not work, the trace is not set to not visible. It is only possible to double-click it, which isolate the trace as expected.

It seems to come from some incompatibility between the transform filter, the color and the histogram type graph.
Others threads reports some trouble on this : https://github.com/ropensci/plotly/issues/1502

Moreover, the plots with scatter type, colors groups and filter works well:

library(plotly)
fig <- plot_ly(
  type = 'scatter',
  x = mtcars$hp,
  y = mtcars$qsec,
  showlegend=TRUE,
  mode = 'markers',
  name = paste('cyl=',mtcars$cyl,sep=""),
  transforms=list(
      list(type = "filter" , target = 'x', operation = ">=",value = 100)
  )
)
fig <- config(fig, displayModeBar = FALSE, showTips = FALSE)
htmlwidgets::saveWidget(as_widget(fig), "MWE_scatter.html")

And the plot with histogram type, colors and no transform filter works also like a charm:

library(plotly)
fig <- plot_ly(
  type = 'histogram',
  x = mtcars$hp,
  showlegend=TRUE,
  color = factor(mtcars$cyl)
)
fig <- config(fig, displayModeBar = FALSE, showTips = FALSE)
htmlwidgets::saveWidget(as_widget(fig), "MWE_histo.html")

Any help is welcome :slight_smile:

Feel free to ask for more details if needed :slight_smile:

An ugly but functional work around is to add a first trace (that will kind of “absord” the buggy behavior), and hide the trace.

library(plotly)
ghostData <- data.frame (SomeValues  = c(0), Groups       = c(0))
fig <- plot_ly(
    type = 'histogram',
    x = ghostData$SomeValues,
    showlegend=FALSE,
    color = factor(ghostData$Groups),
    transforms=list(list(type = "filter" , target = 'x', operation = ">=",value = 100))
)

fig <- add_trace(
    fig,
    type = 'histogram',
    visible='TRUE',
    x = mtcars$hp,
    showlegend=TRUE,
    color = factor(mtcars$cyl),
    transforms=list(list(type = "filter" , target = 'x', operation = ">=",value = 100))
)

fig <- config(fig, displayModeBar = FALSE, showTips = FALSE)
htmlwidgets::saveWidget(as_widget(fig), "MWE_histo_bug_workaround.html")