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

Category bar chart: grouped labels

Hey everyone,

I’ve been trying to create an horizontal bar graph with grouped labels. While i am successful in generating the graph properly, i have yet to find a way to replace the ticktext with a proper group label.

My data frame (test) consists of 3 columns: x1 contains the values(numbers), id the variable ids(character) and cl the groups by which they are categorised (0 to 10) .
My script (with custom colors) is the following:

cols <- c("#795548", "#4caf50", "#ffeb3b", "#2196f3", "#ff9800", "#673ab7", "#00bcd4", "#e91e63",
          "#f44336", "#9e9e9e", "#607d8b") # color code
# Plot
plot <- plot_ly(test, x=~x1, y=~id, type='bar', orientation='h', color=as.character(test$cl), colors=cols) %>%
  layout(xaxis = list(zeroline = T, showline = T, ticks= 'inside'),
         yaxis = list(tickfont=list(size=4), categoryorder='array', categoryarray=test$cl),
         margin = list(b = 100))

My output looks like this:


I would want the y labels to be replaced by the cl name that corresponds to each group and placed in a the middle of their corresponding group as such:

Does anyone have any input on this?

Thanks in advance!

There probably isn’t a easy/built-in way for this. You can see the Subcategory Axes in this link Axes in R. It needs a bunch of customized code in the layout() to create custom chart elements.