How can I combine group and stack bar chart with layout.shape in background using R Plotly?

As mentioned in the comments stacked+grouped bar charts aren’t implemented yet in plotly. Here is a ggplot2 / ggplotly workaround:

library(plotly)
library(ggplot2)

DF <- data.frame(
  stringsAsFactors = FALSE,
             Month = c("2019-Dec","2019-Dec",
                       "2019-Dec","2019-Dec","2020-Jan","2020-Jan","2020-Jan"),
              Week = c(4L, 4L, 5L, 5L, 1L, 1L, 1L),
               Cat = c("A", "B", "A", "C", "A", "B", "C"),
                 n = c(17L, 6L, 21L, 10L, 19L, 20L, 12L)
)

p <- ggplot(DF, aes(x = Week, y = n, fill = Cat)) + geom_bar(stat = 'identity', position = 'stack') + facet_grid( ~ Month) + theme_bw()

ggplotly(p)

result

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top