7
$\begingroup$

I have the following BarChart, that uses a custom ErrorBar function to plot error bars:

errorBar[type_: "Rectangle"][{{x0_, x1_}, {y0_, y1_}}, value_, 
  meta_] := 
 Block[{error, mags = QuantityMagnitude[value]}, 
  error = Flatten[QuantityMagnitude[meta]];
  error = 
   If[error === {}, 0, 
    Last[error]]; {ChartElementData[type][{{x0, x1}, {y0, y1}}, mags, 
    meta], {Black, Thick, 
    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}}}]}}]
BarChart[{{-0.08` -> 0.07`, -0.06` -> 0.07`}, {0.27` -> 0.08`, 
   0.12` -> 0.08`}}, ChartLegends -> {{"A", "B"}, None}, 
 ChartLabels -> {{"Group 1", "Group 2"}, None}, 
 ChartStyle -> {"Pastel", None}, 
 ChartElementFunction -> errorBar["Rectangle"]]

enter image description here

As can be seen in the picture, the ChartLabels are plotted directly under the bars, but the error bars overlap. The ChartLabel function does not consider the length of the error bars.

I have the following question:

How can I adjust the distance between the ChartLabels and the error bars? In other words, I want the labels "Group 1", "Group 2" (and the associated lines) to be below the end of the error bars of "Group 1" and not the bars themselves (basically at the position where the y-axes ends).

$\endgroup$
  • 2
    $\begingroup$ Add AxesOrigin -> {0, -0.2} in BarChart $\endgroup$ – OkkesDulgerci Aug 6 at 14:29
  • $\begingroup$ Well, that was easy and works like a charm, thank you! $\endgroup$ – holistic Aug 6 at 14:31
5
$\begingroup$

In case you want to keep the axes origin at {0, 0}, you can post-process the BarChart output to move the labels lower:

data = {{-0.08 -> 0.07, -0.06 -> 0.03}, {0.27 -> 0.08, 0.12 -> 0.05}};

bc1 = BarChart[data, 
    AxesOrigin -> {0, 0},
    ChartLegends -> {{"A", "B"}, None}, 
    ChartLabels -> {Style[#, 16] & /@ {"Group 1", "Group 2"},  None}, 
    ChartStyle -> {"Pastel", None}, 
    ChartElementFunction -> errorBar["Rectangle"]];

bc1 /. Text[t_, Offset[o_, {a_, b_}], c___] :>  Text[t, Offset[o, {a, -.17}], c]

enter image description here

If you have version 12, you can modify input data (replace Rule with Around) to create a chart with error bars without the need for a custom ChartElementFunction:

data2 = data /. Rule -> Around;
bc2 = BarChart[data2, 
    AxesOrigin -> {0, 0},
    ChartLegends -> {{"A", "B"}, None}, 
    ChartLabels -> {Style[#, 16] & /@ {"Group 1", "Group 2"}, None}, 
    ChartStyle -> { "Pastel", None}, 
    IntervalMarkers -> "Fences", 
    IntervalMarkersStyle -> <|"FenceWidth" -> 0.2, 
        "WhiskerStyle" -> Thick,  "FenceStyle" -> Thick|>] ;

bc2 /.  Text[t_, Offset[o_, {a_, b_}], c___] :>  Text[t, Offset[o, {a, -.17}], c]

enter image description here

$\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.