plotly.js icon indicating copy to clipboard operation
plotly.js copied to clipboard

Grouped bar charts do not work with multiple Y axes

Open iustin opened this issue 10 years ago • 35 comments

Hi,

it seems that having a graph with group bar charts and multiple Y axis doesn't work. I've created the trivial graph https://plot.ly/~iustin/4/grouped-bar-chart-with-multiple-axes/ and tested that switching Col3 between yaxis and yaxis2 also toggles between grouped and non-grouped.

Is this intended? Logically speaking, it makes sense to be able to do this, so I guess it's only a technical limitation.

iustin avatar Dec 05 '15 13:12 iustin

Note: I used the above link just to show the issue; what I'm interested is to have this working in the JavaScript library.

iustin avatar Dec 05 '15 13:12 iustin

Thanks @iustin - yes, we should be able to support that.

The rule (I think) should be: group bar traces that are on the same position axis, and on the same or overlaying the same size axis.

alexcjohnson avatar Dec 07 '15 10:12 alexcjohnson

+1

Right now I am just using overlayed charts with a high alpha on each bar so that I can see through the overlay.

dprice60 avatar Jan 04 '16 17:01 dprice60

@dprice60 (or anyone else) could you upload an example of this workaround? Thanks

mbonaci avatar Jan 26 '16 16:01 mbonaci

@mbonaci Here is a simple one using the original example: https://plot.ly/~dprice60/0/grouped-bar-chart-with-multiple-axes/

That's all I have time to do right now, if you need me to do more I can try and put together something better later (like in a couple of days). The interface doesn't allow me to do everything that I normally have been doing but it let me get something up quick. If you click the code tab, I usually would replace the rgb(r,g,b) values with rgba(r,g,b,a). For the main color I've been using an alpha of .5 and for the stroke lines an alpha of 1. That helps make it more obvious that they are overlaying charts instead of stacked charts.

Hope that helps.

dprice60 avatar Jan 26 '16 20:01 dprice60

Thanks for that @dprice60, but that looks to me like the stacked type.
I expected from the grouped type that bars that have the same x value are positioned at y=0 and that there's a gap between such groups of different x value.

Something like this: image

Which is exactly what happens when I remove the second, y2 axis: https://plot.ly/~mbonaci/24/grouped-bar-chart-with-multiple-axes

mbonaci avatar Jan 26 '16 21:01 mbonaci

I don't have a way to do grouped bar charts with multiple y axes. That is why I +1 one'd the issue and said what I'm doing in the meantime in case it helped anyone until a workaround is found. Sorry for the confusion @mbonaci. Like I said my intent was just to show how I am still making bar charts with multiple axes useful until the issue is resolved.

dprice60 avatar Jan 26 '16 22:01 dprice60

No, that's ok, I misunderstood your comment, or maybe was just hoping that you have a workaround :) :+1:

mbonaci avatar Jan 26 '16 22:01 mbonaci

A hack that can get the effect you want is to make dummy traces with one point each and zero value, in place of each trace on the other y axis. So for example, if you want the first trace on y and the second on y2, make 4 traces:

Plotly.newPlot(div, [
    // real trace on the left y axis
    {x:[1,2,3,4], y:[1,2,3,4], type: 'bar'},
    // invisible second trace in the first group
    {x:[1], y: [0], type: 'bar', hoverinfo: 'none', showlegend: false},
    // invisible first trace in the second group
    {x:[1], y: [0], type: 'bar', yaxis: 'y2', hoverinfo: 'none', showlegend: false},
    // real trace on the right y axis
    {x:[1,2,3,4], y:[40,30,20,10], type: 'bar', yaxis: 'y2'}
],
{
    yaxis2: {side: 'right', overlaying: 'y'}
});

Be warned though that once this issue is fixed, plots using this hack will break - this plot would then show up as a single group of 4 traces, two of which are invisible but still get space allotted to them.

alexcjohnson avatar Jan 27 '16 00:01 alexcjohnson

Thanks @alexcjohnson, that was really helpful. Just to clarify for others, since it's not trivial to deduce from only 2 traces, here's an example with 3:

Plotly.newPlot(placeholder, [
    // real trace on the left y axis
    {x:[1,2,3,4], y:[1,2,3,4], type: 'bar'},
    // invisible to make space for trace aligned to y2
    {x:[1], y: [0], type: 'bar', hoverinfo: 'none', showlegend: false, yaxis: 'y2'},

    // real trace on the left y axis
    {x:[1,2,3,4], y:[4,3,2,1], type: 'bar'},
    // invisible to make space for trace aligned to y2
    {x:[1], y: [0], type: 'bar', hoverinfo: 'none', showlegend: false, yaxis: 'y2'},

    // invisible to make space for trace aligned to y
    {x:[1], y: [0], type: 'bar', hoverinfo: 'none', showlegend: false},
    // real trace on the right, y2 axis
    {x:[1,2,3,4], y:[140,100,100,160], type: 'bar', yaxis: 'y2'}
  ], 
  {
    yaxis2: {side: 'right', overlaying: 'y'}
  }
);

Codepen: https://codepen.io/etpinard/pen/yGrEKp

mbonaci avatar Jan 28 '16 05:01 mbonaci

+1

ngarcial avatar Jun 11 '16 17:06 ngarcial

is there similar trick to have multiple xaxis? I need the grey bar below be positioned independently from green bars. image

eagor avatar Jun 15 '16 07:06 eagor

Not sure if this is the same issue or not, but adding a simple 2nd access to a multiple chart layout seems to cause overlay issues; can anyone suggest how to get the scatter to appear on top of the bar chart with the 2nd axis? Codepen here showing how it appears with one yaxis and with 2::

http://codepen.io/robroy72/pen/VjgRdA

allanelder avatar Aug 15 '16 17:08 allanelder

@allanelder Here's how to fix it: http://codepen.io/etpinard/pen/pbGYGv

plotly.js axes are plotted in order starting from y, y2, y3, ... so in order to make the bar trace appear below the scatter trace, you need to place the bar trace on y and the scatter trace on y2.

etpinard avatar Aug 15 '16 17:08 etpinard

Hi,

I tried @mbonaci and @alexcjohnson suggestion and it works for vertical chart. I want to try to make it work with horizontal bar charts and replaced x with y and vise versa, but it does not work. Any thoughts on this?

daftster avatar Oct 21 '16 16:10 daftster

@alexcjohnson is this issue fixed or should we still use the workaround/hack you commented on 27 Jan 2016?

SirExpie avatar Apr 21 '17 10:04 SirExpie

is this issue fixed

No.

we still use the workaround/hack you commented on 27 Jan 2016?

Yes.

etpinard avatar Apr 21 '17 13:04 etpinard

Hi, any update in this issue. It would be really nice to have possibility to group bars from different axes. I used to use jquery flot plugin to create barcharts, and it supports that, but unfortunatelly it's no longer suported (project died I think).

marcinwojciechowski avatar Nov 14 '17 20:11 marcinwojciechowski

This is my workaround with 2 series and 2 categories. Adjusting opacity only made it look like stacked so I changed the width as well.

screen shot 2018-02-01 at 23 31 20

For me, it really depends on the data to come up with the workaround though. I'd change those narrow bars to lines or some subtle marks if I feel it would represent the relationship between 2 series better.

screen shot 2018-02-01 at 23 42 38

Having said, I still need to have the grouped bars with 2 axes in some cases.

real-np avatar Feb 01 '18 15:02 real-np

@real-np could you explain how you got the bars to appear side-by-side instead of overlayed directly on top of eachother?

seriph avatar Apr 19 '18 22:04 seriph