3

I am trying to do a grouped bar chart with two different Y axis for measure two different columns. Here is the data:

SUCURSAL         EXISTENCIA         COSTO_MEDIO
0 MELENARA        0.128                643.2
1 LAS TORRES      1.872                659.2
2 GALDAR          0.304                659.2
3 SEBADAL         0.851                623.8
4 ARINAGA         0.176                620.4
5 LANZAROTE       0.232                686.4
6 FUERTEVENTURA   0.480                680.2

and here is my code:

ay <- list(
          tickfont = list(color = "red"),
          overlaying = "y1", 
          side = "right"
        )

p <- plot_ly(dfplot, x = dfplot$SUCURSAL, y = dfplot$EXISTENCIA, type = "bar", opacity = 0.4, name = 'EXISTENCIA', yaxis = "y1") %>%
          add_trace(x = dfplot$SUCURSAL, y = dfplot$COSTO_MEDIO, type = "bar", name = 'COSTO', yaxis = "y2") %>%
          layout( xaxis = list(title = ""), yaxis = list(title = "", tickfont= list(size = 10, color = "black"), showticklabels = TRUE), yaxis2= ay, barmode = 'group' )  %>%
          config(displayModeBar = F)


p

The problem I have is that the two groups of bars are overlayed. It is impossible for me to see it grouped, aside each other. How can I represent it with the two groups of bars?

Any idea?

MLavoie
  • 9,671
  • 41
  • 36
  • 56
  • it does not look possible. But as an alternative, if you really want to keep both y axis, you could use a bar and line plots as in: plot_ly(data=dfplot, x = ~SUCURSAL) %>% add_bars(y = ~EXISTENCIA, opacity = 0.4, name = 'EXISTENCIA') %>% add_lines(y = ~COSTOMEDIO, name = 'COSTO', yaxis = "y2") %>% layout(yaxis2= ay) – MLavoie Dec 07 '16 at 20:04

0 Answers0