rxtsdygraphs

Moving from a normal bar chart to a stacked bar on dygraphs


I've got an xts df of the following format:

structure(c("May 2022", "Jun 2022", "Jul 2022", "Aug 2022", "Sep 2022", 
"Oct 2022", "Nov 2022", "Dec 2022", " 3035.199", " 5500.000", 
"11568.750", " 2510.000", " 6999.999", "21792.149", " 9750.000", 
" 5624.999", " 2250.000", " 4136.975", " 6525.500", " 2771.875", 
" 4637.500", "16273.499", " 6000.000", " 4494.649", " 2500.000", 
"    0.000", " 3029.000", " 2803.500", "    0.000", "14481.250", 
" 4374.998", " 4062.498", "    0.000", " 3075.000", " 6939.249", 
" 1500.000", " 4183.157", " 5769.000", " 3559.500", " 3250.000"
), class = c("xts", "zoo"), index = structure(c(1651363200, 1654041600, 
1656633600, 1659312000, 1661990400, 1664582400, 1667260800, 1669852800
), tzone = "UTC", tclass = "yearmon"), .Dim = c(8L, 5L), .Dimnames = list(
    NULL, c("Month", "Cat 1", "Cat 2", "Cat 3", "Cat 4")))

I'm trying to create a stacked bar chart using the dygraphs library.

library(dygraphs)
library(lubridate)
today <- as.Date(Sys.time())
    last_6 <- today+months(-6)
        dygraph(df) %>%
          dyAxis("y", label= "Total") %>%
          dyRangeSelector(dateWindow = c(last_6, today)) %>%
          dyMultiColumnGroup(c("Cat 1", "Cat 2", "Cat 3", "Cat 4"))

This produce a bar chart that looks like this:Current bar chart

I was wondering if anyone had any advice on how to make stacked bar chart? Many of the guides talk about bringing in plotters, but unfortunately they are not detailed enough for me to properly understand what is going on.

Adding this:

dyStackedBarGroup(c("Cat 1", "Cat 2", "Cat 3", "Cat 4"))

instead of the dyMultiColumnGroup line leads to a:

Error in cumulativeYval + points : non-numeric argument to binary operator

Solution

  • While Quinten's got a great working answer, I thought I would add to it.

    You don't need separate ts objects; you do need the data to be numeric, though.

    Assuming the data from dput is named df:

    # drop the months column, change data to numeric, restore ts class
    df <- df[ ,-1] %>% sapply(as.numeric) %>%
      ts(start = c(2022, 5), deltat = 1/12)
    

    Now you're ready to graph.

    dygraph(df) %>%
      dyAxis("y", label= "Total") %>%
      dyRangeSelector(dateWindow = c(today() - months(6), today())) %>%
      dyStackedBarGroup(dimnames(df)[[2]])
    

    enter image description here