2

In R dygraphs, how do I shade regions by time of day, across multiple days, without listing them each out? For example, I'd like to shade 07:00 - 08:15 each day in the following example (my full example has many more days).

#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)

Thanks!

Russell
  • 41
  • 5
  • ``Error in function_list[[i]](value) : could not find function "dyCandlestick"`` I think its deprecated. – Cyrus Mohammadian Nov 20 '16 at 21:54
  • For the purposes of shading, it doesn't matter if it's candlesticks, line chart, bar chart, etc - although candlesticks are very much supported https://rstudio.github.io/dygraphs/gallery-candlestick.html – Russell Nov 22 '16 at 17:11
  • Also, sessionInfo shows I'm running **dygraphs_1.1.1.3** – Russell Nov 22 '16 at 17:35

2 Answers2

2

I was able to solve my problem. Code is below for reference:

Also, thanks to the following for ideas:

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)
Community
  • 1
  • 1
Russell
  • 41
  • 5
  • One other link that proved helpful: http://stackoverflow.com/questions/11871572/subsetting-tricks-for-xts-in-r – Russell Nov 22 '16 at 21:44
0

This is great! I am trying to do something very similar, except I have month/day ranges across multiple years. It must be possible to index months and days in a similar fashion?

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

I'd like to create the ok_periods list of the form:

[[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"

The months/day ranges are the same for each year, and unlike the previous example, I have no time-of-day information.

brbell01
  • 3
  • 1