Difficulty reproducing stacked bar graph in R using dygraphs

时间:2018-03-23 00:18:16

标签: r dygraphs r-dygraphs

I’ve been using the dygraphs R package to produce some wonderful timeseries plots, but am having great difficulty reproducing the examples located here: http://rstudio.github.io/dygraphs/gallery-custom-plotters.html

I’m particularly interested in creating a stacked bar chart: Desired stacked bar chart

My data is an xts/zoo object and plots nicely using the standard dygraph function: sample plot

However, I am unsure where the dyStackedBarGroup function comes from. It appears these functions must be created, and point to the specific plotters in .js files.

I can see for the first example, how dyBarChart is created, but there is no stackedbarchar.js/stackedbargroup.js in my local dygraph installation (however I can see the file in https://github.com/rstudio/dygraphs/tree/master/inst/plotters).

I’ve attempted to source all the functions and .js files from the github page which do not appear to be made available when loading the dygraphs package locally, but I remain unsuccessful.

Am I doing something completely wrong?

1 个答案:

答案 0 :(得分:5)

stackedGraph中的dyOptions参数设置为TRUEdyOptions(stackedGraph = TRUE)

条形图的javascript文件可以在dygraphs包目录的“examples / plotters / barchart.js”中找到。

数据:

lungDeaths <- cbind(mdeaths, ldeaths)

代码:

# create dygraph plotter
library('dygraphs')
dyBarChart <- function(dygraph) {
  dyPlotter(dygraph = dygraph,
            name = "BarChart",
            path = system.file("examples/plotters/barchart.js", package = "dygraphs"))
}

dygraph(lungDeaths) %>%   # create dygraph of lungDeaths
  dyBarChart() %>%        # create bar chart with the passed dygraph
  dyOptions(stackedGraph = TRUE)  # make it as stacked bar chart

enter image description here