2016-11-20 46 views
0

在R dygraphs中,我如何根據一天中的時間,跨越多個日期來遮蔽區域,而無需將它們分別列出?例如,我想在下面的例子中每天07:00 - 08:15進行遮蔽(我的完整示例有更多天)。r dygraphs陰影區域的時間跨越多天

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

謝謝!

+0

''錯誤[我]](價值): 不能fin d函數「dyCandlestick」''我認爲它已被棄用。 –

+0

出於陰影的目的,它是否是燭臺,折線圖,條形圖等 - 儘管燭臺非常受支持https://rstudio.github.io/dygraphs/gallery-candlestick.html – Russell

+0

此外, sessionInfo顯示我正在運行** dygraphs_1.1.1.3 ** – Russell

回答

0

我能解決我的問題。代碼如下供參考:

而且,這要歸功於思路如下:在function_list

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) 
+0

另一個證明有用的鏈接:http://stackoverflow.com/questions/11871572/subsetting-tricks-for-xts-in-r – Russell