R:你能从R包dygraphs改变dyRangeSelector的图表类型吗?

时间:2017-06-13 14:33:31

标签: r dygraphs

我使用R包 dygraphs 来创建交互式时间序列图表。 我希望主图表和选择器都是条形图。 我设法将主图作为条形图。

但是,我想知道是否有办法更改图表下方显示的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);
            }
            }")

1 个答案:

答案 0 :(得分:0)

你可以试试stepplot也许它不是真正的条形图但是非常接近而选择器也是stepplot

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