Horizontal grouped bar chart with two x axes

#1

Hi guys,

I am trying to plot two data series that differ in scales, in order to do that I will need the grouped barplot to have two different axes.

I can plot the grouped horizontal by doing as:

x <- c('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December')
y1 <- c(20, 14, 25, 16, 18, 22, 19, 15, 12, 16, 14, 17)
y2 <- c(19, 14, 22, 14, 16, 19, 15, 14, 10, 12, 12, 16)
data <- data.frame(x, y1, y2)

p <- plot_ly(data, y = ~x, x = ~y1, type = 'bar', name = 'Primary Product', marker = list(color = 'rgb(49,130,189)')) %>%
    add_trace(x = ~y2, name = 'Secondary Product', marker = list(color = 'rgb(204,204,204)')) %>%
    layout(xaxis = list(title = "", tickangle = -45),
           yaxis = list(title = ""),
           margin = list(b = 100),
           barmode = 'group')

The problem is to plot using a secondary x axis:

p <- plot_ly(data, y = ~x, x = ~y1, type = 'bar', name = 'Primary Product', marker = list(color = 'rgb(49,130,189)')) %>%
    add_trace(x = ~y2, name = 'Secondary Product', marker = list(color = 'rgb(204,204,204)', yaxis = "y2")) %>%
    layout(xaxis = list(title = "", tickangle = -45),
           yaxis = list(title = ""), xaxis = list(side = "bottom"), xaxis2 = list(overlaying = "x", side = "top"),
           margin = list(b = 100),
           barmode = 'group')

I cannot get the secondary axis to properly depict the range contained in ‘Secondary Product’ (yes, I’ve tried to add_trace for the secondary Product, but it gets stacked instead of grouped). Any ideas?

Thank you.