Dropdown filter in plotly, r - doesn't show “All”

I am trying to filter in r by characters in a column but I can’t get it to show all data in that column as a button also. I am also fitting a line to the data and would like to show all lines when the “All” button is pressed, and the lines be named accordingly.

Here is a sample:


library(plotly)    

qfitspec <- lm(Petal.Length ~ poly(Sepal.Length, 1), data = iris)

p <- iris %>%
  plot_ly(
    type = 'scatter', 
    x = ~Sepal.Length, 
    y = ~Petal.Length,
    text = ~Species,
    hoverinfo = 'text',
    mode = 'markers', 
    transforms = list(
      list(
        type = 'filter',
        target = ~Species,
        operation = '=',
        value = unique(iris$Species)[1]
      )
    ))%>%
  add_lines(y = ~fitted(qfitspec), name = "Species")%>%
  layout(
      updatemenus = list(
        list(
          type = 'dropdown',
          active = 0,
          buttons = list(
            list(method = "restyle",
                 args = list("transforms[0].value", unique(iris$Species)[1]),
                 label = unique(iris$Species)[1]),
            list(method = "restyle",
                 args = list("transforms[0].value", unique(iris$Species)[2]),
                 label = unique(iris$Species)[2]),
            list(method = "restyle",
                 args = list("transforms[0].value", unique(iris$Species)[3]),
                 label = unique(iris$Species)[3])
          )
        )
      )
    )

Ideally I would have a 4th button called “All” that displayed all the data in the species column and show all the fitted lines in the subset. Thanks

You achieve this by fir change:
first change operation from ‘=’ ‘in’

and add 4th button
list(method = “restyle”,
args = list(“transforms[0].value”, unique(iris$Species)),
label = ‘ALL’)

Hi @ Ndivhuw,

Thanks for taking the time to address this question. I have run the code below using your suggestion and it doesn’t appear to be plotting the desired results. It appears that the “All” button now shows just the points from the “setosa” group in Species and the line is the fitted line for the “virginica” group. Any ideas on why this is? I have also tried this exercise with other example data and posted to stackoverflow

qfitspec <- lm(Petal.Length ~ poly(Sepal.Length, 1), data = iris)

p <- iris %>%
  plot_ly(
    type = 'scatter', 
    x = ~Sepal.Length, 
    y = ~Petal.Length,
    text = ~Species,
    hoverinfo = 'text',
    mode = 'markers', 
    transforms = list(
      list(
        type = 'filter',
        target = ~Species,
        operation = 'in',
        value = unique(iris$Species)[1]
      )
    ))%>%
  add_lines(y = ~fitted(qfitspec), name = "Species")%>%
  layout(
    updatemenus = list(
      list(
        type = 'dropdown',
        active = 0,
        buttons = list(
          list(method = "restyle",
               args = list("transforms[0].value", unique(iris$Species)[1]),
               label = unique(iris$Species)[1]),
          list(method = "restyle",
               args = list("transforms[0].value", unique(iris$Species)[2]),
               label = unique(iris$Species)[2]),
          list(method = "restyle",
               args = list("transforms[0].value", unique(iris$Species)[3]),
               label = unique(iris$Species)[3]),
          list(method = "restyle",
               args = list("transforms[0].value", unique(iris$Species)),
               label = 'All')
        )
      )
    )
  )
p