为什么不介绍=; quate;使用“转换”时工作在r绘图中过滤

发布于 2025-01-19 09:25:35 字数 2372 浏览 0 评论 0原文

我对r绘制的新手很陌生,所以我仍然习惯所有参数。我正在探索的一个主题是如何通过过滤创建下拉菜单事件。我发现了使用此处找到的IRIS数据集找到了这个有用的示例noreferrer”> https://community.plotly.com/t/need-help-on-using-dropdown-to-filter/6596/2 ,这是我下面提供的相同代码,并进行了一些轻微的更改。我正在尝试如何更改“ setosa”和“ versicolor”直方图的注释和形状,同时仍然能够根据数据更改图形。但是,当我尝试在布局()中使用方法=“ update”时,注释/形状会更改,但是即使在“ setosa”和“ versicolor”之间切换时,数据也保持不变。有人知道为什么会发生这种情况以及如何解决吗?

这是可再现的代码。我希望能够在同时更改注释/形状的同时过滤与花样相对应的直方图图形(以创建下拉菜单)。

library(plotly)
datasets::iris



line_shape <- list(type = "line",
                         x0 = 0,
                         x1 = 1,
                         xref = "paper",
                         y0 = 6,
                         y1 = 6,
                         line = list(color = "black"))

line_shape2 <- list(type = "line",
                   x0 = 0,
                   x1 = 1,
                   xref = "paper",
                   y0 = 6,
                   y1 = 6,
                   line = list(color = "red"))





line_annotation <- list( x = 5, y = 6.4, xref = "x", yref = "y",
                        text = "Sepal", showarrow = FALSE,
                        font = list(color = 'black'))


line_annotation2 <- list( x = 5, y = 6.4, xref = "x", yref = "y",
                         text = "Sepal", showarrow = FALSE,
                         font = list(color = 'red'))


p <- iris %>%
  plot_ly(
    type = 'histogram', 
    x = ~Sepal.Length,
    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 = "update",
                 args = list(list("transforms[0].value", unique(iris$Species)[1]),
                             list(shapes = list(line_shape, c()),
                              annotations = list(line_annotation, c()))),
                 label = unique(iris$Species)[1]),
            list(method = "update",
                 args = list(list("transforms[0].value", unique(iris$Species)[2]),
                        list(shapes = list(c(), line_shape2),
                        annotations = list(c(), line_annotation2))),
                 label = unique(iris$Species)[2])
          )
        )
      )
    )
p

I'm fairly new to r plotly so I'm still getting accustomed to all the arguments. One topic that I'm exploring is how to create a dropdown menu event by filtering. I found this helpful example using the iris dataset found here https://community.plotly.com/t/need-help-on-using-dropdown-to-filter/6596/2, it's the same code I provided below with some slight changes. I was experimenting with how I can change the annotations and shapes for the "Setosa" and "Versicolor" histograms respectively while still being able to change the graphic in terms of the data. However, when I tried to use method = "update" in layout(), the annotations/shapes changed, but the data remained the same even when I toggled between "Setosa" and "Versicolor." Does anybody know why this happens, and how to fix it?

Here is the reproducible code. I want to be able to filter the histogram graphics that correspond to the flower species (to create a dropdown menu) while changing their annotations/shapes simultaneously.

library(plotly)
datasets::iris



line_shape <- list(type = "line",
                         x0 = 0,
                         x1 = 1,
                         xref = "paper",
                         y0 = 6,
                         y1 = 6,
                         line = list(color = "black"))

line_shape2 <- list(type = "line",
                   x0 = 0,
                   x1 = 1,
                   xref = "paper",
                   y0 = 6,
                   y1 = 6,
                   line = list(color = "red"))





line_annotation <- list( x = 5, y = 6.4, xref = "x", yref = "y",
                        text = "Sepal", showarrow = FALSE,
                        font = list(color = 'black'))


line_annotation2 <- list( x = 5, y = 6.4, xref = "x", yref = "y",
                         text = "Sepal", showarrow = FALSE,
                         font = list(color = 'red'))


p <- iris %>%
  plot_ly(
    type = 'histogram', 
    x = ~Sepal.Length,
    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 = "update",
                 args = list(list("transforms[0].value", unique(iris$Species)[1]),
                             list(shapes = list(line_shape, c()),
                              annotations = list(line_annotation, c()))),
                 label = unique(iris$Species)[1]),
            list(method = "update",
                 args = list(list("transforms[0].value", unique(iris$Species)[2]),
                        list(shapes = list(c(), line_shape2),
                        annotations = list(c(), line_annotation2))),
                 label = unique(iris$Species)[2])
          )
        )
      )
    )
p

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文