【发布时间】:2021-08-06 23:19:26
【问题描述】:
我一直试图在R 中使用dygraph 绘制时间序列。似乎我们只能传入带有日期及其值的数据框。所有其他列将被自动忽略。在这里,我复制了我的数据和结果图:
library(dygraphs)
library(tidyverse)
library(plotly)
library(data.table)
dates <- seq(as.POSIXct("2021-01-01 05:00:00"), as.POSIXct("2021-01-05 05:00:00"), by = 8*3600)
df <- data.table(date = dates,
percentage = round(runif(length(dates), min = 0, max = 1), digits = 2),
team = sample(c("A", "B", "C", "D", "E"), size = length(dates), replace = T)
)
dygraph(df) %>%
dyOptions(drawPoints = T, pointSize = 3) %>%
dyAxis(name = "y", label = "percentage") %>%
dyLegend(show = "follow")
图表如下所示:
正如我们所见,每个日期对应的团队并未显示在图例中。我想看到鼠标悬停的团队。更清楚地说,我可以使用ggplot 和ggplotly 来做到这一点,但是plotly 包相对较重,我仍然想在我的Shiny 应用程序中使用dygraph。下面是使用 ggplotly 的样子:
p <- ggplot(df, aes(x = date, y = percentage)) + geom_line() + geom_point(aes(group = team))
ggplotly(p)
有什么方法可以给 dygraph 添加标签并实现同样的效果?我会很感激任何帮助
【问题讨论】: