7
$\begingroup$

In How to | Add Error Bars to Charts and Plots in the Mathematica documentation, a function ErrorBar is given:

errorBar[type_: "Rectangle"][{{x0_, x1_}, {y0_, y1_}}, value_, meta_] := 
   Block[{error}, 
      error = Flatten[meta];
      error = If[error === {}, 0, Last[error]]; 
      {ChartElementData[type][{{x0, x1}, {y0, y1}}, value, meta], 
         {Black, Line[{
             {{(x0 + x1)/2, y1 - error}, {(x0 + x1)/2, y1 + error}}, 
             {{1/4 (3 x0 + x1), y1 + error}, {1/4 (x0 + 3 x1), y1 + error}}, 
             {{1/4 (3 x0 + x1), y1 - error}, {1/4 (x0 + 3 x1), y1 - error}}
            }]
       }}
      ]

This function adds error bars to BarChart. Here is an example using some random data with random errors:

chartData = MapThread[{#1 -> #2} &, {RandomReal[1, 10], RandomReal[0.1, 10]}]

Now plotting it, as per the tutorial:

BarChart[chartData, ChartElementFunction -> errorBar["Rectangle"]]

How can I adjust variable spacing between the bars, e.g. have two groups of five bars without any spacing between them, and then a spacing of, say, 1 between the two groups?

$\endgroup$
8
$\begingroup$

You can use the option setting BarSpacing -> {a,b} to set the spacing between individual bars to a and that between groups to b:

chartData = Flatten@MapThread[{#1 -> #2} &, {RandomReal[1, 10], RandomReal[0.1, 10]}];
chartData2 = MapAt[# + RandomReal[] &, chartData, {{;; , 1}}];

BarChart[{chartData, chartData2}, 
 BarSpacing -> {0, 1.5}, 
 ChartLabels -> {Placed[{"Group 1", "Group 2"}, Below], 
                 Placed[CharacterRange["a", "j"], Center]}, 
 ChartElementFunction -> errorBar["Rectangle"], ImageSize -> 500]

enter image description here

Update: A quick-and-dirty way to get the same result as in m_goldberg's method without changing errorBar function is to insert Missing[] in the positions where you want the gaps in the input data:

BarChart[{Insert[chartData, Missing[], {{3}, {7}}]}, 
 BarSpacing -> None, 
 ChartLabels -> {Placed[{"Group 1"}, Axis], 
   Placed[Insert[CharacterRange["a", "j"], "", {{3}, {7}}], Center]},
 ChartElementFunction -> (errorBar[]),
 ImageSize -> 500, PlotRange -> {-.2, 1}, AxesOrigin -> {0, -.2},
 ImagePadding -> 20]

enter image description here

$\endgroup$
2
$\begingroup$
Panel@Row[{
   BarChart[
    First@Partition[chartData, 5],
    ColorFunction -> "Rainbow",
    ImageSize -> 300,
    BarSpacing -> 0,
    PlotLabel -> "First Group",
    PlotRange -> {Automatic, {0, 1}},
    PlotRangePadding -> -0.002,
    ChartElementFunction -> errorBar["Rectangle"]],

   Spacer@5,

   BarChart[
    Last@Partition[chartData, 5],
    ColorFunction -> "Rainbow",
    ImageSize -> 300,
    BarSpacing -> 0,
    PlotLabel -> "Second Group",
    PlotRange -> {Automatic, {0, 1}},
    ChartElementFunction -> errorBar["Rectangle"],
    PlotRangePadding -> 0.01,
    Axes -> False]
   }]

enter image description here

$\endgroup$
2
$\begingroup$

Here is something that might work for you. The idea is to modify errorBar to plot an empty bar for data items of form _-> None and inject an item of that form at any point where a gap is to appear.

errorBar[type_: "Rectangle"][{{x0_, x1_}, {y0_, y1_}}, value_, meta_] := 
  Block[{error},
    If[meta[[1]] === None, 
      Return[{ChartElementData[type][{{x0, x1}, {y0, y1}}, value, meta], {}}]];
    error = Flatten[meta];
    error = If[error === {}, 0, Last[error]];
    {ChartElementData[type][{{x0, x1}, {y0, y1}}, value, meta], 
     {Black, Line[{{{(x0 + x1)/2, y1 - error}, 
                  {(x0 + x1)/2, y1 + error}}, 
                  {{1/4 (3 x0 + x1), y1 + error}, 
                  {1/4 (x0 + 3 x1), y1 + error}}, 
                  {{1/4 (3 x0 + x1), y1 - error}, 
                  {1/4 (x0 + 3 x1), y1 - error}}}]}}]

SeedRandom[42];
chartData = Insert[Thread[RandomReal[1, 10] -> RandomReal[0.1, 10]], 0 -> None, 6]
{
  0.425905 -> 0.0258796, 0.391023 -> 0.0550582, 0.347069 -> 0.0717287, 
  0.453741 -> 0.0754353, 0.555963 -> 0.0860349, 
  0 -> None, 
  0.289169 -> 0.0996966, 0.296848 -> 0.0739226, 0.206408 -> 0.00383646, 
  0.32517 -> 0.0376127, 0.973325 -> 0.0296199
}
BarChart[chartData,
  ChartElementFunction -> errorBar["Rectangle"],
  BarSpacing -> None]

chart

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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