Rafael Díaz
Rafael Díaz

Reputation: 2289

r - how to plot dygraphs in same panel

I want to make a graph for time series with dygraph package, I have several time series and I want to compare the behavior year by year as shown in the image. Any suggestions.

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)

enter image description here

Upvotes: 1

Views: 273

Answers (1)

jalind
jalind

Reputation: 491

How about something like this...

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))

Upvotes: 1

Related Questions