带有按钮的 Plotly Graphs 中的意外图例和值

问题描述 投票:0回答:1

我一直在尝试创建一个 plotly 对象,它使用按钮在两个图形之间切换。但是,单击按钮时出现的图例和值并不是我所期望的。我一直在使用以下指南作为参考:

下拉列表
自定义按钮

这是我使用的数据示例:

library(plotly)
library(RColorBrewer)

# Create MWE dataframe 
set.seed(32)
ae_data <- data.frame(
  Program = rep(c("Biology", "Math", "History", "English", "Computer Science",
                  "Chemistry", "Business", "Philosophy", "Psychology", "Physics"), 5),
  Year = rep(2018:2022, each = 10),
  Application = round(runif(50, min = 100, max = 900), digits = 0),
  Enrolment = round(runif(50, min = 150, max = 950), digits = 0)
)

这是我用来生成图表的代码:

# Generate the appropriate number of colors
graph_colors <- colorRampPalette(brewer.pal(8, "Set2"))(length(unique(ae_data$Program)))

# Create graph
ae_data %>%
  plot_ly(
    type = "scatter",
    mode = "lines + markers",
    colors = graph_colors
  ) %>%
  add_trace(
    x = ~Year,
    y = ~Application,
    color = ~Program
  ) %>%
  add_trace(
    x = ~Year,
    y = ~Enrolment, 
    color = ~Program,
    visible = F
  ) %>%
  layout(
    updatemenus = list(
      list(
        type = "buttons",
        buttons = list(
          list(
            label = "Application",
            method = "update",
            args = list(list(visible = c(F, T)),
                        list(yaxis = list(title = "Application")))
          ),
          list(
            label = "Enrolment",
            method = "update",
            args = list(list(visible = c(T, F)),
                        list(yaxis = list(title = "Enrolment")))
          )
        )
      )
    )
  )

最终发生的事情是,一旦我单击其中一个按钮,我的应用程序和注册框架的图例和值就会混淆。单击我的每个按钮,我都会获得一半的申请和注册框架。我希望按钮能够根据我对 updatemenus 的论点在上面的跟踪中确定的帧之间切换。我试过使用其他“方法”输入,但根据文档,“更新”似乎是正确的。

这里有一些图片来说明我的预期值和实际值:

应用的预期图:

点击应用程序的实际图表:

r plot plotly
1个回答
0
投票

此问题的一个可能解决方案是使用 Plotly 中的 update_layout() 函数更新图形的布局。具体来说,您可以使用图例参数来指定图例在数据更新时的行为。试试下面的代码:

fig <- plot_ly(data, x = ~year, y = ~value, color = ~group, type = "scatter", mode = "lines", hoverinfo = "text",
               text = ~paste("Group: ", group, "<br>Year: ", year, "<br>Value: ", value))

fig <- fig %>% layout(
  updatemenus = list(
    list(
      type = "buttons",
      showactive = TRUE,
      buttons = list(
        list(method = "restyle",
             args = list(list(y = list(~data$value[data$group == "A"], ~data$value[data$group == "B"])),
                         list(type = "scatter", mode = "lines")),
             label = "All"),
        list(method = "restyle",
             args = list(list(y = list(~data$value[data$group == "A"])),
                         list(type = "scatter", mode = "lines")),
             label = "Group A"),
        list(method = "restyle",
             args = list(list(y = list(~data$value[data$group == "B"])),
                         list(type = "scatter", mode = "lines")),
             label = "Group B")
      )
    )
  ),
  legend = list(
    traceorder = "normal",
    font = list(
      family = "sans-serif",
      size = 12,
      color = "black"
    ),
    bgcolor = "#E2E2E2",
    bordercolor = "#FFFFFF",
    borderwidth = 2
  )
)

fig
© www.soinside.com 2019 - 2024. All rights reserved.