Search code examples
rdygraphsr-dygraphs

Difficulty reproducing stacked bar graph in R using 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?


Solution

  • set stackedGraph argument in dyOptions to TRUE. dyOptions(stackedGraph = TRUE).

    The javascript file for the barchart can be found at "examples/plotters/barchart.js" of the dygraphs package directory.

    Data:

    lungDeaths <- cbind(mdeaths, ldeaths)
    

    Code:

    # 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