R:缺乏互动性

R: Lack of Interactivity

我正在使用 R 编程语言。我在这里关注本教程:

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

本教程声称使用过滤器生成交互式图形。然而,当我 运行 代码时,我没有看到任何过滤器。谁能告诉我我是否做错了什么(或者我可能误解了代码)?

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

此代码生成下图:

但是似乎没有任何“过滤器”按钮。

如果我做错了什么,有人可以告诉我吗?

谢谢

实际上,您代码中的过滤器仅显示 y > mean(mtcars$qsec) 的数据点。我相信您正在寻找类似下拉过滤器的东西,就像这里讨论的那样:https://community.plotly.com/t/need-help-on-using-dropdown-to-filter/6596.

以下代码来自https://community.plotly.com/t/need-help-on-using-dropdown-to-filter/6596。 (请参阅上面@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