2
$\begingroup$

Sorry, I know how to make error bars on normal bar charts, but how can I add vertical error bars to the top of my stacked bar charts, please?

BarChart[{{5, 5, 5}, {5, 5, 5}}, ChartLayout -> "Stacked"]

Something that would look like this:

Something that would look like this:

I managed to cheat using `Show, but I'd prefer a more elegant method.

enter image description here

$\endgroup$
2
  • 1
    $\begingroup$ I am a bit biased about this but...You should free yourself from the desire to use stacked bar charts (especially ones with error bars). Consider this: peltiertech.com/stacked-bar-chart-alternatives. $\endgroup$
    – JimB
    Commented Aug 7, 2018 at 21:58
  • $\begingroup$ Thanks Jim, interesting link. I know stacked bar charts are imperfect, especially since the error bars I am adding are for the whole bar and not just the last rectangle, but that's the way information has been represented in the field where I am trying to publish a paper. $\endgroup$
    – Tommy95
    Commented Aug 8, 2018 at 15:02

1 Answer 1

3
$\begingroup$

Update: In versions 12.0+, you can use Around:

BarChart[{{5, Around[5, 6], 5}, {2, Around[2, 3], 4}}, 
 ChartLayout -> "Stacked"] 

enter image description here

Original answer:

You can use a slightly modified version of the function errorBar from howto/AddErrorBarsToChartsAndPlots (see also this Q/A):

ClearAll[errorBar]
errorBar[type_: "Rectangle"]:= Module[{error = If[#3 === {}, 0, #3[[1]]], 
   x0 = #[[1, 1]], x1 = #[[1, 2]], y1 = #[[2, 2]]},   
 {ChartElementData[type][##],  {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}}}]}}]&

and add the error information as metadata to the last element in each data group

BarChart[{{5, 5, 5 -> 7}, {2, 3, 4 -> 2}}, ChartLayout -> "Stacked", 
 ChartElementFunction -> {Automatic, Automatic, errorBar[]}]

enter image description here

Update: What if I want to put the error bar on the second rectangle out of the three?

You can post-process the BarChart output to reorder groups of primitives so that the second rectangle (and the error lines associated with it) is at the end:

bc = BarChart[{{5, 5 -> 6, 5}, {2, 3 -> 2, 4}}, 
 ChartLayout -> "Stacked", 
 ChartElementFunction -> {Automatic, errorBar[], Automatic}]; 
bc /. x : {{_Directive, __} ..} :> RotateLeft[x, 2]

enter image description here

$\endgroup$
2
  • $\begingroup$ Thanks a lot! BarChart[{{5, 5 -> 6, 5}, {2, 3 -> 3, 4}}, ChartLayout -> "Stacked", ChartElementFunction -> {Automatic, errorBar[], Automatic}] What if I want to put the error bar on the second rectangle out of the three. The last rectangle is in front of the error bar. Would you please know how to solve this without playing with the opacity of the last rectangle? $\endgroup$
    – Tommy95
    Commented Aug 13, 2018 at 15:18
  • $\begingroup$ @Tommy95, my pleasure. A hack to put the error bar on the second rectangle : bc = BarChart[{{5, 5 -> 6, 5}, {2, 3 -> 3, 4}}, ChartLayout -> "Stacked", ChartElementFunction -> {Automatic, errorBar[], Automatic}]; bc /. x : {{_Directive, __} ..} :> x[[{1, 3, 2}]] $\endgroup$
    – kglr
    Commented Aug 13, 2018 at 16:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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