# Stacked Bar Chart with individual spacings to compare pairs of bars

I would like to plot a stacked BarChart

BarChart[RandomReal[1, {6, 4}], ChartLayout -> "Percentile"]


with modified BarSpacing, so I get 3 pairs of stacked bars being more closely together, so it is implied they belong together and can be compared more easily.

Update: Pre-processing input data into a form acceptable for RectangleChart we get finer control on the size of the gaps between groups of bars.

The function prep below takes two arguments: The first argument controls the size of gaps between groups and the second controls group size.

prep[w_: 1/4, n_:2] := Flatten[Riffle[Partition[Thread[{1, #}] & /@ #, n], {{{{w, 0}}}}],
1] &
RectangleChart[prep[]@data, ChartLayout -> "Percentile"]


RectangleChart[prep[1]@data, ChartLayout -> "Percentile"]


RectangleChart[prep[1, 3]@data, ChartLayout -> "Percentile"]


I don't think this can be achieved using BarSpacing. You can partition input data into groups of two and insert Missing[] between groups:

SeedRandom[1]
data = RandomReal[1, {6, 4}];
BarChart[Flatten[Riffle[Partition[data, 2], Missing[]], 1],
ChartLayout -> "Percentile"]


• Thx a lot :) I was searching for something this! Best regards Oct 8, 2018 at 20:35
• @Kilian, my pleasure. Thank you for the accept.
– kglr
Oct 8, 2018 at 20:35

Only in case you whish to have bigger space between pairs ...

SeedRandom[1]
data = RandomReal[1, {6, 4}];
BarChart[Flatten[
Riffle[Partition[data, 2], Missing[]] /.
Missing[] -> {Missing[], Missing[], Missing[]}, 1],
BarSpacing -> Medium, ChartLayout -> "Percentile"]

• Not a bad hack! But this can be done quite a bit more concisely: BarChart[Riffle[data, Unevaluated[{}, {}, {}], 3], BarSpacing -> Medium, ChartLayout -> "Percentile"] Oct 10, 2018 at 1:19