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$
  • 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 Aug 7 '18 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 Aug 8 '18 at 15:02
2
$\begingroup$

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$
  • $\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 Aug 13 '18 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 Aug 13 '18 at 16:32

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.