# How to plot this group bar plot?

I want to generate a barplot with the following characteristics.

There are 5 users in the system. Each user has a demand. There are 2 suppliers. Each supplier tries to meet user demand as closely as possible. One supplier may perform better than the other supplier.

I want a grouped barplot.

There will be two bars for each user (demand and supply) The supply bar should combine the supplies from two supplies in such a way that the

For example,

Demand = {10, 15, 20, 17, 9};
Supply1 = {8, 13, 18, 14, 11};
Supply2 = {9, 14, 19, 16, 10};


I just want to combine Supply1 and Supply2 in one bar. So, there will only five supply bars. The demand bars and the supply bars are then grouped as shown in the attached figure.

It is like putting one supply bar above another but both should be visible. The shorter one will always on top so that both are visible.

But the problem I have with this plot is that for the last group, I cannot see the red bar, which is somehow hidden below the purple bar.

This one I obtained with MATLAB. I hope Mathematica can help me with what I need.

• What have you tried so far using BarChart? Commented Mar 8, 2019 at 14:02
• @kglr, this is not what I want. I have revised my question...
– MGK
Commented Mar 8, 2019 at 14:55
• try adding the option ChartBaseStyle -> Opacity[.5]?
– kglr
Commented Mar 8, 2019 at 15:18
• I'm voting to close this question as off-topic because the OP is asking for the impossible, despite reasonable suggestions. Furthermore, any solution will be extremely localized to their problem only. Commented Mar 8, 2019 at 15:35
• I'm voting to close this question as off-topic because overloading bar charts makes for unintelligible displays. If there are two objectives for displaying data, then (despite objections from publishers) maybe two separate displays are needed.
– JimB
Commented Mar 8, 2019 at 19:50

Update 2: A better approach to pre-processing is to use two separate BarCharts (as in m_goldberg's answer):

demand = {10, 15, 20, 17, 9};
supply1 = {8, 13, 18, 14, 11};
supply2 = {9, 14, 19, 16, 10};
s = Transpose[{Style[#, Blue] & /@ supply1, Style[#, Yellow] & /@ supply2}];
sb = SortBy[#, -First[#] &] & /@ s;

b1 = BarChart[Transpose[{Style[#, Red] & /@ demand, sb[[All,1]]}],
ChartLabels -> {Placed["Group " <> ToString[#] & /@ Range[5], Axis],
Placed[{"D", "S"}, Axis]},
ChartLegends -> SwatchLegend[{Red, Blue, Yellow}, {"D", "S1", "S2"}]];

b2 = BarChart[Transpose[{Style[#, Red] & /@ demand, sb[[All,2]]}]];

Show[b1, b2]


Update: An alternative, more convenient and better, approach is to pre-process data to reorder based on heights:

sb = SortBy[#, -First[#] &] & /@ s;
datab = Join @@ Thread[{0, 0, 0, 0, 0, d, sb}];

bcb = BarChart[datab, ChartLayout -> "Overlapped",
ChartLegends -> SwatchLegend[{Red, Blue, Yellow}, {"D", "S1", "S2"}],
BarSpacing -> {0, .1}]


With this approach we don't need to add PerformanceGoal -> "Speed", thus we retain all the tool-tips and and dynamic highlighting.

BarChart does not support mixed layouts (it does not allow combining Grouped and Overlapped layouts). So, we cheat by inserting fake data sets with 0 height betweeen groups to separate the five groups and, to make all rectangles visible, we post-process to re-order the yellow and blue rectangles based on their heights (I added PerformanceGoal -> "Speed" above to make this post-processing easier):

demand = {10, 15, 20, 17, 9};
supply1 = {8, 13, 18, 14, 11};
supply2 = {9, 14, 19, 16, 10};
s = Transpose[{Style[#, Blue] & /@ supply1, Style[#, Yellow] & /@ supply2}];
d = Thread[{Style[#, Red] & /@ demand, Style[0, Opacity[0]]}];
data = Join @@ Thread[{0, 0, 0, 0, 0, d, s}];

bc = BarChart[data,
ChartLayout -> "Overlapped",
ChartLegends -> SwatchLegend[{Red,  Blue, Yellow}, {"D", "S1", "S2"}],
BarSpacing -> {0, .1}, PerformanceGoal -> "Speed"];


bc /. pattern : {{_, Rectangle[{_, _}, {_, h1_}, ___]}, {_,
Rectangle[{_, _}, {_, h2_}, ___]}} :> If[h2 <= h1, pattern, Reverse[pattern]]


Fixing ticks and labels is an altogether different challenge.

• Yep, this is the kind of heroic effort that I was hinting at in my comments... Thank you for trying though. (+1) Commented Mar 8, 2019 at 16:01
• thank you @MarcoB.
– kglr
Commented Mar 8, 2019 at 16:05

This will reproduce your matlab generated chart.

Demand = {10, 15, 20, 17, 9};
Supply1 = {8, 13, 18, 14, 11};
Supply2 = {9, 14, 19, 16, 10};

b1 = BarChart[Transpose[{Demand, Supply2}],
ChartStyle -> {{Automatic}, {Automatic, Red}}];
b2 = BarChart[Transpose[{Demand, Supply1}]];

Show[b1, b2, Frame -> True]


It has the same defect as the matlab chart — the longer blue bar at the right end of chart hides the shorter red bar behind it.

• I suppose that, if OP's purpose is simply to get the plot, one could add {Supply1, Supply2} = Transpose[Sort /@ Transpose[{Supply1, Supply2}]] before the calls to BarChart to "swap" the offending last values from Supply1 and Supply2`. Commented Mar 8, 2019 at 23:06
• @MarcoB. Yes, something like that would work, but it would ruin the stark simplicity of this answer :-), which is mainly to point out that hoe easy it is to reproduce the defective matlab chart. I think kglr's answer is better than mine because it makes the chart that is really wanted in a simple way. Commented Mar 9, 2019 at 1:27