【问题标题】:Managing multiple Times Series with Crosstalk and plotly使用 Crosstalk 和 plotly 管理多个时间序列
【发布时间】:2018-11-24 10:05:17
【问题描述】:

我正在尝试使用带有串扰的 plotly 来显示多个时间序列的动态图。根据是否选中复选框,我希望时间序列是否显示在图表上。 在下面的代码中,我在同一个数据帧 (df_final) 中创建了两个时间序列 1 和 2。 到目前为止,我有两个问题:

  • 当没有选中任何复选框时,仍然有一个时间序列 图表
  • 当两个复选框都被选中时,而不是显示两次 系列,代码认为只有 1 个唯一的时间序列。

我不知道如何改进上述任何一项,有什么想法吗?

另外,我不确定我是否使用了正确的工具(串扰等)。欢迎任何其他建议。

library(plotly)
library(crosstalk)

#Create two identical dataframe
df_1 <- economics
df_2 <- economics

#Add a key to each dataframe to dissociate the two time series
df_1$ts <- 1
df_2$ts <- 2

#Modify the first dataframe
df_2$psavert <- economics$psavert + 1

df_final <- rbind(df_1, df_2)

shared_df_final <- SharedData$new(df_final)

bscols(
       list(filter_checkbox("Time series", "Time series", shared_df_final, ~ts, inline = TRUE)),
       plot_ly(data = shared_df_final, x = ~date, height = 700) %>%
         add_lines(y = ~df_final$psavert, line = list(color = "#00526d", width = 1),
                   hoverinfo = "text", text = "ts")
)

【问题讨论】:

    标签: r r-plotly


    【解决方案1】:

    您是否正在寻找类似的东西:

    plot_ly() %>%
      add_lines(data = df_1, x= ~date, y = ~psavert, name = "First") %>%
      add_lines(data = df_2, x= ~date, y = ~psavert, name = "Second") %>%
      layout(
        updatemenus = list(
          list(
            y = 0.8,
            type= 'buttons',
            buttons = list(
              list(method = "restyle",
                   args = list("visible", list(TRUE, TRUE)),
                   label = "Both"),
              list(method = "restyle",
                   args = list("visible", list(TRUE, FALSE)),
                   label = "First"),
    
              list(method = "restyle",
                   args = list("visible", list(FALSE, TRUE)),
                   label = "Second")))
        )
      )
    

    【讨论】:

    • 谢谢!这种工作,但我一直在寻找两个检查按钮而不是三个,因为将来我将不得不在同一个情节上管理多达 5-6 个时间序列。有更好的主意吗?
    • 也可以是下拉按钮
    • 下拉按钮并不理想。我正在寻找每个时间序列的复选框输入(显示或不显示)。如果我有 5 个时间序列,我可以使用 5 个检查按钮进行管理。一个下拉菜单将有 5^2 = 25 行。
    • @Liky,从上面的示例中,您可以根据需要删除both 标签。但一开始会显示所有轨迹,之后只会显示您选择的轨迹。
    猜你喜欢
    • 1970-01-01
    • 2021-01-10
    • 1970-01-01
    • 2017-07-14
    • 1970-01-01
    • 2021-02-28
    • 1970-01-01
    • 2015-01-06
    • 2023-04-08
    相关资源
    最近更新 更多