4
$\begingroup$

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

$\endgroup$
4
$\begingroup$

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

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

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

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.