1

我有一个具有以下结构的数据集(数据=历史):

structure(list(date = c("23-03-2019", "23-03-2019", "23-03-2019", 
"23-03-2019", "05-05-2020", "05-05-2020", "05-05-2020", "05-05-2020", 
"17-06-2021", "17-06-2021", "17-06-2021", "17-06-2021"), cumvol = c(0.004, 
0.034, 0.054, 0.057, 0.005, 0.048, 0.068, 0.075, 2.009, 2.029, 
2.049, 2.064), time = structure(c(26457, 26636, 26658, 27216, 
25152, 25614, 25667, 25668, 56966, 57268, 57303, 58986), units = "secs", class = c("hms", 
"difftime")), Year = c("2019", "2019", "2019", "2019", "2020", 
"2020", "2020", "2020", "2021", "2021", "2021", "2021"))

最重要的是,我正在绘制来自单独 df 的数据:

structure(list(date = structure(c(19038, 19038, 19038, 19038), class = "Date"), 
cumvol = c(0.029, 0.034, 0.07, 0.075), time = structure(c(29674, 29674, 29691, 29719), 
class = c("hms", "difftime"), units = "secs")), Year = c("2022", "2022", "2022", "2022"))

使用 ggplot 和 plotly 我生成了一个非常简单的折线图,它突出显示了最近数据与历史数据,然后我将其转换为 plotly。

plot <- ggplot()+geom_line(data=historic,aes(x=time, y=cumvol, group=date),color='#BAB0AC', alpha=0.5)+
      geom_line(data=recent,aes(x=time, y=cumvol, group=date),size=1.2,color='#E15758')+
      geom_point(data=most_recent, aes(x=time,y=cumvol), color='#E15759',size=3)+geom_hline(yintercept = 0)+  theme(title=element_text(size=12),panel.background = element_rect(fill='white',color='black'),legend.position='right')+
        labs(title = "Vol",subtitle = "Cum Vol so far", x = "Time", y = "Vol")
    
    chart <- plotly::ggplotly(plot)

我想使用串扰的过滤功能(在这里找到:https ://rstudio.github.io/crosstalk/using.html )添加一些功能,以允许用户按“年份”和“日期”进行过滤,但是我不想丢失绘制在顶部的最新数据。

我想我需要像这样将这样的过滤器应用于“图表”对象:

bscols(widths = c(3, 9),
       list(
filter_checkbox(id = "Year", label = "Year", group = ~Year)
filter_select(id = "date", label = "Date", group = ~date), chart))

我希望用户能够通过 filter_checkbox 过滤“年份”,但使用 filter_select 过滤个别日子。但这并没有真正奏效。

我还尝试使用 Crosstalk 的 SharedData 函数转换数据,尽管有两点增加了复杂性,因为 Crosstalk 指南似乎假设图表是直接从 plotly 生成的。这也很困难,因为我的图表是从两个非常独立的数据框生成的。

我想知道是否有人知道如何解决我的问题?

4

0 回答 0