在 dygraphs 中为时间序列添加日期
Add date for time series in dygraphs
我有以下数据,我试图用 R 中的 dygraphs 绘制:
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
每条线代表 ts.rmean 和 ts.rmax 从 1 月 1 日到 12 月 31 日之间的每日值。由于我没有指定日期,绘图的 x 轴显示从 1 到 366 的每一行的索引。可以修改数据,使 x 轴显示 月-日?
你可以这样做:
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)
我有以下数据,我试图用 R 中的 dygraphs 绘制:
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
每条线代表 ts.rmean 和 ts.rmax 从 1 月 1 日到 12 月 31 日之间的每日值。由于我没有指定日期,绘图的 x 轴显示从 1 到 366 的每一行的索引。可以修改数据,使 x 轴显示 月-日?
你可以这样做:
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)