Can dygraphs be used to plot by day/month rather than a full date object?

571 Views Asked by At

I'm wondering if there is a way to create a dygrpah that plots variables by a partial date object (such as day of the year or month) for comparison.

For example, if I had daily data for five years, can I plot a variable by 'day of the year' to compare two different years?

library(dygraphs)
library(xts)

dates <- seq(as.Date("1996-01-01"), as.Date("2000-12-31"), "month")
var <- rnorm(length(dates)) 
df <- data.frame(dates,var)
x <- xts(df, order.by = df$dates)
dy <- dygraph(x)
dy

EDIT: For clarification, this is the type of plot I'm looking for - multiple years of data plotted along the same x-axis range which would correspond to the month of the year, but not the year itself. I'd like to do this in dygrpahs to utilize the interactivity, but it seems like the x-axis always relies on a full date object that wouldn't allow me to plot by day/month and not year.

d96 <- subset(df, dates >= "1996-01-01" & dates <= "1996-12-31")
d97 <- subset(df, dates >= "1997-01-01" & dates <= "1997-12-31")
d98 <- subset(df, dates >= "1998-01-01" & dates <= "1998-12-31")

plot(d96$var, type = 'l', lwd = 2, ylim = c(-4,4))
lines(d97$var, col = "blue", lwd = 2)
lines(d98$var, col = "red", lwd = 2)

enter image description here

0

There are 0 best solutions below