R How to shade week-end period with dygraphs?

2020-07-11 09:47发布

How can I shade week-end period with dyShading, in r dygraphs?

I use the code below, but is there a simple way?

library(lubridate)
library(xts)
library(dygraphs)
dateP<-seq(as.POSIXct("2015-01-01 "), as.POSIXct("2015-02-01 "), by="days")
toto<-rep(6,length(dateP))
tutu<-xts(toto, order.by=dateP)


dateWD<-dateP[isWeekend(dateP, wday = 0:4)]
ok_periods<-0
i=1
j=1
while (i<(length(dateWD)-1)){
  ok_periods[j] <- list(list(from = dateWD[i], to = dateWD[i+1]))
  i=i+2
  j=j+1
}

add_shades<-function(x,periods){
  for(period in periods){
   x<-dyShading(x, from=period$from,to=period$to)
 }
 x
  }
 dygraph(tutu) %>% 
   add_shades(ok_periods)

标签: r dygraphs
0条回答
登录 后发表回答