r - 如何在同一面板中绘制 dygraphs

r - how to plot dygraphs in same panel

我想用 dygraph package 制作时间序列图,我有几个时间序列,我想逐年比较行为,如下所示图片。任何建议。

library(zoo)
serie1 <- zoo(rnorm(365), seq(as.Date('2001-01-01'),as.Date('2001-12-31'),by = 1))
serie2 <- zoo(rnorm(365), seq(as.Date('2002-01-01'),as.Date('2002-12-31'),by = 1))
serie3 <- zoo(rnorm(365), seq(as.Date('2003-01-01'),as.Date('2003-12-31'),by = 1))

plot(serie1, col = 2, ylim = c(-4,4), ylab = "", xlab = "Month")
par(new = T)
plot(serie2, col = 3, ylim = c(-4,4), ylab = "", xlab = "Month")
par(new = T)
plot(serie3, col = 4, ylim = c(-4,4), ylab = "", xlab = "Month")
legend("topleft", legend = paste("year",2001:2003), col = 2:4, lty = 1)

这样的事情怎么样...

library(dygraphs)
serie <- list(serie1, serie2, serie3)

full_serie <- do.call(cbind.data.frame, serie)
names(full_serie) <- c("serie1", "serie2", "serie3")


library(htmltools)
dy_graph <- dygraph(full_serie) %>%
            dySeries("serie1") %>%
            dySeries("serie2") %>%
            dySeries("serie3")


htmltools::browsable(htmltools::tagList(dy_graph))