r dygraphs 在多天中按一天中的时间绘制阴影区域

r dygraphs shaded region by time of day across multiple days

在 R dygraphs 中,如何跨多天按一天中的时间对区域进行着色,而不是将它们逐一列出?例如,我想在下面的示例中每天遮蔽 07:00 - 08:15(我的完整示例还有很多天)。

#generate data
library(xts)
library(dygraphs)

#generate sample data
#
x <- xts(cumsum(rnorm(400000, 0, 0.2)), Sys.time() - 400000:1)
x <- to.minutes(x)

#display dygraph
dygraph(x) %>%
  dyCandlestick() %>%
  dyRangeSelector(height=20)

谢谢!

我能够解决我的问题。代码如下供参考:

另外,感谢以下提供的想法:

R xts: generating 1 minute time series from second events

R How to shade week-end period with dygraphs?

http://databasefaq.com/index.php/answer/20331/r-dygraphs-dyshading-r-dygraph


#load libraries
library(xts)
library(dygraphs)

#generate sample data
x <- xts(cumsum(rnorm(400000, 0, 0.2)), Sys.time() - 400000:1)
x <- to.minutes(x)

#display dygraph without shading
dygraph(x) %>%
  dyCandlestick() %>%
  dyRangeSelector(height=20)


#####################
#function to creating shading in a list
add_shades <- function(x, periods, ...) {
  for( period in periods ) {
    x <- dyShading(x, from = period$from , to = period$to, ... )
  }
  x
}

#####################
#creates the list that feeds into the "add_shades" function
ok_periods<-0
i=1
j=1
while (i<(length(index(x[(.indexhour(x)==9 & .indexmin(x)==30)])))){
  ok_periods[j] <- list(list(from = index(x[(.indexhour(x)==16 & .indexmin(x)==15)][i]), to = index(x[(.indexhour(x)==9 & .indexmin(x)==30)][i+1])))
  i=i+1
  j=j+1
}

#####################
#graph with shading
dygraph(x) %>%
  dyCandlestick() %>%
  add_shades(ok_periods, color = "#FFFFCC" ) %>%
  dyRangeSelector(height=20)

太棒了!我正在尝试做一些非常相似的事情,除了我有多年的 month/day 范围。一定可以用类似的方式索引月份和日期吗?

head(dailyprecipxts)
               [,1]
2002-01-01  16.2199
2002-01-06  48.7243
2002-01-11 105.5646
2002-01-16   7.0446
2002-01-21  47.3491
2002-01-26  21.2664

我想创建以下形式的 ok_periods 列表:

[[1]]
[[1]]$from
[1] "2002-07-01"

[[1]]$to
[1] "2002-10-01"


[[2]]
[[2]]$from
[1] "2003-07-01"

[[2]]$to
[1] "2003-10-01"

每年的 months/day 范围都相同,与前面的示例不同,我没有时间信息。