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

标签 r dygraphs

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

#generate data
library(xts)
library(dygraphs)

#generate sample data
#http://stackoverflow.com/questions/9778632/r-xts-generating-1-minute-time-series-from-second-events
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)

关于r dygraphs 多天内按一天中的时间绘制阴影区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40708341/

相关文章:

r - 如何将列表的所有元素放入/保存到 R 中的一张 Excel 工作表中?

r - data.table 中的 .N 带条件

r - 根据汇总计数计算序列

R Shiny : dygraphs series reacting to `date_window`

dygraphs - 如何在 dygraphs 中获取多个 x 轴值

javascript - 删除 Dygraphs 右侧的空白

r - 使用 R 中的行平均值标准化数据

r - mgcv:如何指定平滑和因子之间的交互?

javascript - 为 dygraphs javascript 图表创建工具提示

dygraphs - 在 dygraph 中动态/以编程方式添加/删除系列