2017-06-13 115 views
0

我使用R包dygraphs創建交互式時間序列圖。 我希望主圖表和選擇器都是條形圖。 我設法將主圖形作爲條形圖。R:你可以從R包dygraph改變dyRangeSelector的圖表類型

但是我想知道是否有方法來改變圖表下方出現的圖表類型的dyRangeSelector。目前是一個簡單的線性圖。 請參閱下面的代碼。

任何幫助將不勝感激。非常感謝你提前。

install.packages(c("dygraphs","xts")) 
library(dygraphs) 
library(xts) 

# First I create my Time series 
df <- as.xts(ts(start =c(1950), end=c(2010), 
        data = runif(61,0,5))) 

# Create the graph 
dygraph(df, main = "Frequency") %>% 
    dyRangeSelector(dateWindow = c(min(index(df)), max(index(df)))) %>% 
    dyOptions(useDataTimezone = TRUE, plotter = 
       "function barChartPlotter(e) { 
      var ctx = e.drawingContext; 
      var points = e.points; 
      var y_bottom = e.dygraph.toDomYCoord(0); // see  http://dygraphs.com/jsdoc/symbols/Dygraph.html#toDomYCoord 

      // This should really be based on the minimum gap 
      var bar_width = 2/3 * (points[1].canvasx - points[0].canvasx); 
      ctx.fillStyle = e.color; 

      // Do the actual plotting. 
      for (var i = 0; i < points.length; i++) { 
      var p = points[i]; 
      var center_x = p.canvasx; // center of the bar 

      ctx.fillRect(center_x - bar_width/2, p.canvasy, 
      bar_width, y_bottom - p.canvasy); 
      ctx.strokeRect(center_x - bar_width/2, p.canvasy, 
      bar_width, y_bottom - p.canvasy); 
      } 
      }") 

回答

0

你可以嘗試stepplot也許它不是真正的條形圖,但非常接近和選擇器stepplot太

dygraph(df, main = "Frequency") %>% 
    dyRangeSelector(dateWindow = c(min(index(df)), max(index(df)))) %>% 
    dyOptions(stepPlot = TRUE ,fillGraph = TRUE, fillAlpha = 0.4) 
+0

感謝布魯內爾 - 是不幸的是它是不一樣的,但總比沒有好。我還注意到一個問題,RangeSelector是線性還是條形圖,零點沒有正確表示。也就是說,「零」值不在RangeSelector的x軸上,但它們始終位於它的上面。有沒有辦法解決這個問題? – Gio