# Horizontal grouped bar chart with two x axes

**URL:** <https://community.plotly.com/t/horizontal-grouped-bar-chart-with-two-x-axes/8958>\
**Category:** Plotly R\
**Created:** [March 14, 2018, 3:48pm UTC](https://community.plotly.com/t/horizontal-grouped-bar-chart-with-two-x-axes/8958 "2018-03-14T15:48:19Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![pedro](https://avatars.discourse-cdn.com/v4/letter/p/919ad9/32.png) [@pedro](https://community.plotly.com/u/pedro)\
**Post date:** [March 14, 2018, 3:48pm UTC](https://community.plotly.com/t/horizontal-grouped-bar-chart-with-two-x-axes/8958/1 "2018-03-14T15:48:19Z")

</div>

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.
