首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R:缺乏交互性

R:缺乏交互性
EN

Stack Overflow用户
提问于 2021-02-09 00:43:20
回答 2查看 76关注 0票数 0

我使用的是R编程语言。我在这里遵循这个教程:

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

本教程声称要生成一个带有过滤器的交互式图形。然而,当我运行代码时,我没有看到任何过滤器。如果我做错了什么(或者我误解了代码),有人能告诉我吗?

代码语言:javascript
复制
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

此代码生成以下图形:

然而,似乎没有任何“过滤器”按钮。

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

谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-02-09 01:06:19

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

票数 1
EN

Stack Overflow用户

发布于 2021-02-09 02:45:13

以下代码来自https://community.plotly.com/t/need-help-on-using-dropdown-to-filter/6596。(见上面@rodrigocfaria的回答):

代码语言:javascript
复制
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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66105723

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档