Search code examples
rxtsdygraphs

Add date for time series in dygraphs


I have the following data that I am trying to plot with dygraphs in R:

ts.rmean ts.rmax
0001-01-01  3.163478    5.86
0002-01-01  3.095909    4.67
0003-01-01  3.112000    6.01
0004-01-01  2.922800    5.44
0005-01-01  2.981154    5.21
0006-01-01  3.089167    5.26
0007-01-01  3.168000    6.28
0008-01-01  3.040400    5.00
0009-01-01  2.809130    6.04
0010-01-01  3.002174    4.64
0011-01-01  3.002000    4.93
0012-01-01  3.081250    5.28
0013-01-01  2.687083    4.62

Each line represents a daily value between 01 Jan - 31 Dec for ts.rmean and ts.rmax. Since I have not specified the date, the x-axis of the plot shows the index of each line from 1 to 366. It is possible to modify the data so that the x-axis would show Month-Day?


Solution

  • You could do something like this:

    library(dygraphs)
    library(xts)
    
    #convert the rownames of your data frame to a year-month-day, 
    #used 2012 because it has 366 days and subsetted to fit the example
    rownames(data)<-strptime(paste0("2012-",1:366),format="%Y-%j")[1:nrow(data)]
    
    #transform to xts
    data<-as.xts(data)
    
    #plot
    dygraph(data)