4

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.

I am very happy about your help :) Thx in advance Kilian

kglr
  • 394,356
  • 18
  • 477
  • 896
Kilian
  • 145
  • 4

2 Answers2

5

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"]

enter image description here

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

enter image description here

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

enter image description here

Original answer:

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"]

enter image description here

kglr
  • 394,356
  • 18
  • 477
  • 896
2

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"]
Anxon Pués
  • 907
  • 5
  • 13
  • 1
    Not a bad hack! But this can be done quite a bit more concisely: BarChart[Riffle[data, Unevaluated[{}, {}, {}], 3], BarSpacing -> Medium, ChartLayout -> "Percentile"] – Mr.Wizard Oct 10 '18 at 01:19