You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R语言Plot_ly堆叠柱状图下拉筛选功能异常问题求助

Plotly堆叠柱状图Update Menus筛选异常问题

我在使用R语言的plotly包时遇到了更新菜单(update menus)功能异常的问题。此前在chloropleth图中可行的方法,应用到堆叠柱状图时却无法正常工作,我无法确定原因。

测试代码

library(plotly)

# Simplified data frame for testing
test_data <- data.frame(
  time = rep(1:10, each = 4),
  total = rnorm(40, 100, 10),
  Species_name = rep(c("Species A", "Species B", "Species C", "Species D"), 10),
  mesh_grp = rep(c("Group 1", "Group 2"), 20)
)

p <- test_data %>%
  plot_ly(
          x = ~time, 
          y = ~total, 
          split = ~Species_name, 
          color =~Species_name, 
          type = 'bar', 
    transforms = list(
      list(
        type = 'filter',
        target = ~mesh_grp,
        operation = '=',
        value = unique(test_data$mesh_grp)[1]
      )
  )) %>% layout(
    barmode = "stack",
    updatemenus = list(
      list(
        type = 'dropdown',
        active = 0,
        buttons = list(
          list(method = "restyle",
               args = list("transforms[0].value", unique(test_data$mesh_grp)[1]),
               label = unique(test_data$mesh_grp)[1]),
          list(method = "restyle",
               args = list("transforms[0].value", unique(test_data$mesh_grp)[2]),
               label = unique(test_data$mesh_grp)[2])
        )
      )
    )
  )

p

问题现象

按照预期,选择下拉菜单中的“Group 1”应筛选出包含Species A和Species C的数据,但实际结果显示了所有物种,且仅包含奇数时间点的数据。

请问我哪里出错了?感谢您的帮助!

内容的提问来源于stack exchange,提问作者d_lang

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 19:13:11