r dygraphs阴影区域在一天中的时间跨越多天

时间:2016-11-20 19:20:06

标签: r dygraphs

在R dygraphs中,如何在多天内按时间对区域进行着色,而不是每次都列出?例如,我想在下面的示例中每天07:00 - 08:15(我的完整示例还有更多天)。

"**link(http://google.com)*{Google}**"

谢谢!

2 个答案:

答案 0 :(得分:0)

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

另外,感谢以下想法:

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)

答案 1 :(得分:0)

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

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"

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