stats_noob
stats_noob

Reputation: 5907

R: Lack of Interactivity

I am using the R programming language. I am following this tutorial over here:

https://plotly.com/r/filter/

This tutorial claims to produce an interactive graph with a filter. Yet when I run the code, I don't see any filter. Can anyone tell me if I am doing something wrong (or perhaps I have misunderstood the code)?

library(plotly)

fig <- plot_ly(
  type = 'scatter',
  x = mtcars$hp,
  y = mtcars$qsec,
  text = rownames(mtcars),
  hoverinfo = 'text',
  mode = 'markers',
  transforms = list(
    list(
      type = 'filter',
      target = 'y',
      operation = '>',
      value = mean(mtcars$qsec)
    )
  )
)

fig

This code produces the following graph:

enter image description here

Yet there does not seem to be any "filter" button.

Can someone please tell me if I am doing something wrong?

Thanks

Upvotes: 0

Views: 120

Answers (2)

stats_noob
stats_noob

Reputation: 5907

The following code is from https://community.plotly.com/t/need-help-on-using-dropdown-to-filter/6596. (see answer above by @rodrigocfaria):

library(plotly)    

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]
      )
  )) %>% 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])
        )
      )
    )
  )

#view plot
p

enter image description here

Upvotes: 0

rodrigocfaria
rodrigocfaria

Reputation: 440

Actually the filter in your code is only showing data points where y > mean(mtcars$qsec). I believe that you're looking for something like dropdown filters, like the ones discussed here: https://community.plotly.com/t/need-help-on-using-dropdown-to-filter/6596.

Upvotes: 1

Related Questions