Black Lives Matter. Please consider donating to Black Girls Code today.

Grouped AND Stacked Barplot

Hello, I know this was asked in the past. Is plotly still not able to do a Grouped AND Stacked Barplot similar to the solution here: https://stackoverflow.com/questions/45055661/combine-grouped-and-stacked-bar-graph-in-r?

1 Like

hello, you can. see my post here: Stacked bar chart - specify color platte for a column
my question is about how to change color platte. if you know, please help. thanks.

Hey @flachboard and @gcmicah

continuing from Stacked bar chart - specify color platte for a column couldn’t you just use subplots with sharedY = T. Something like:

grp <- c("A","B","C","D")
xval <- as.factor(c("x","y","x","y"))
frame <- merge(grp, xval, all=T)
yval <- as.factor(c('low', 'low-medium', 'medim-high', 'high'))
df <- tbl_df(cbind(frame, yval))
colnames(df) <- c("category", "Type", "Range")

p <- df %>% 
  group_by(category) %>% 
  arrange(Type) %>%
  plot_ly(
    x = ~Type, 
    y = ~Range, 
    color= ~category,
    colors = 'Reds',
    type = 'bar',
    legendgroup = "A")

pp <- df %>% 
  group_by(category) %>% 
  arrange(Type) %>%
  plot_ly(
    x = ~Type, 
    y = ~Range, 
    color= ~category,
    colors = 'Reds',
    type = 'bar',
    legendgroup = "A",
    showlegend=F)


subplot(p,pp,shareY = T) %>% layout(barmode = 'stack')

Yes, it is possible to make a stacked+grouped bar plot. But the “grouped” has to be done via subplots. More details here: