R How to shade week-end period with dygraphs?

怎甘沉沦 提交于 2019-12-06 04:17:41

问题


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)

来源:https://stackoverflow.com/questions/31449429/r-how-to-shade-week-end-period-with-dygraphs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!