【发布时间】: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")
)
【问题讨论】: