5
$\begingroup$

I have:

data01 = {0.012844725279853909`, 0.023794357025920716`};
data02 = {0.01740189577795908`, 0.027023265047026828`};

and,

PairedBarChart[{Style[data01, LightBlue]}, {Style[data02, Directive[Lighter@Orange, HatchFilling["Diagonal", 2, 7]]]}, 
ChartLabels -> {Placed[Style[#, 24, FontFamily -> "Times"] & /@ {"Paris", 
  "Berlin"}, {0.22, 0.09}], None, Style[Subscript[a, #], 16] & /@ {"1", "2"}}, 
PlotTheme -> "Detailed", LabelStyle -> {20, GrayLevel[0]}, 
PlotLabel -> Style["Rates", 28, FontFamily -> "Times"], ImageSize -> Large]

which results in:

enter image description here

Now, I need to add errors, let's say:

Around[0.012844725279853909`, {0, 0.001}]
Around[0.023794357025920716`, {0.002, 0.003}]
Around[0.01740189577795908`, {0.001, 0.001}]
Around[0.027023265047026828`, {0.004, 0.004}]

My question is: how can I incorporate these errors directly in PairedBarChart?

$\endgroup$
4
$\begingroup$

It seems that PairedBarChart cannot directly produce error bars from Around, so you have to overlay them by using ListPlot.

The following code produces what you want, but needs a some manual tweaking to adjust for the correct horizontal position of error bars. (This can also be calculated automatically if you have many plots).

(* Manually tweak for correct x-position *)
xOffset = .002;

data01 = {Around[0.0128, {0, 0.001}], Around[0.0237, {0.002, 0.003}]};
data02 = {Around[0.0174, {0.001, 0.001}], Around[0.0270, {0.004, 0.004}]};

pbChart = 
  PairedBarChart[{Style[data01, LightBlue]}, {Style[data02, 
     Directive[Lighter@Orange, HatchFilling["Diagonal", 2, 7]]]}, 
   ChartLabels -> {Placed[
      Style[#, 18, FontFamily -> "Times"] & /@ {"Paris", 
        "Berlin"}, {0.22, 0.09}], None, 
     Style[Subscript[a, #], 16] & /@ {"1", "2"}}, 
   PlotTheme -> "Detailed", LabelStyle -> {12, GrayLevel[0]}, 
   PlotLabel -> Style["Rates", 18, FontFamily -> "Times"], 
   ImageSize -> Large];

errorsChart = ListPlot[{
    Transpose[{-data01 - xOffset, Range[Length[data01]]}],
    Transpose[{data02 + xOffset, Range[Length[data02]]}]
    }];

Show[pbChart, errorsChart]

Error bars on PairedBarChart

$\endgroup$
0
4
$\begingroup$

We can use a custom ChartElementFunction to add error markers to the bars:

ClearAll[cEF]
cEF = {ChartElementData["Rectangle"][##], 
       SurfaceAppearance[None],
       First @ ListPlot[{{(Min[Abs@#[[1]]] + #3[[1]] ) Sign[#[[1, 1]]], Mean @ #[[2]]}},
         PlotStyle -> Directive[Thick, Opacity[1], 
          If[FreeQ[_SurfaceAppearance|_HatchFilling|_PatternFilling] @ 
            Charting`ChartStyleInformation["Style"], Darker, Identity] @
             Charting`ChartStyleInformation["Color"]], 
         IntervalMarkersStyle -> <|"FenceWidth" -> .1|>]} &;

Examples:

data01 = {0.012844725279853909`, 0.023794357025920716`};
data02 = {0.01740189577795908`, 0.027023265047026828`};

errors = {{{0, .001}, {.002, .003}}, { {0.001, 0.001}, {0.004, 0.004}}};

datawitherrors = Apply[Around, MapThread[Thread@*List, {{data01, data02}, errors}], {2}]

enter image description here

We attach error information as metadata using Map[# -> #&] @ data for each dataset;

PairedBarChart[Style[Map[# -> #&] @ datawitherrors[[1]], LightBlue], 
  Style[Map[# -> #&] @ datawitherrors[[2]], 
     Directive[Lighter @ Orange, HatchFilling["Diagonal", 2, 7]]], 
  ChartLabels -> {Placed[Style[#, 24, FontFamily -> "Times"] & /@
       {"Paris", "Berlin"}, {0.22, 0.09}], None, 
    Style[Subscript[a, #], 16] & /@ {"1", "2"}}, 
  PlotTheme -> "Detailed", 
  LabelStyle -> {12, GrayLevel[0]}, 
  PlotLabel -> Style["Rates", 28, FontFamily -> "Times"], 
  ImageSize -> Large, 
  ChartElementFunction -> cEF]

enter image description here

SeedRandom[123];
data = RandomInteger[{1, 10}, {2, 2, 3}];
datawitherrors = data /. x_Integer :> Around[x, RandomReal[{-1, 2}, 2]];

labels = Map[Style[ToUpperCase@#, 16] &][RandomWord["Noun", #]] & /@ {2, 2, 3};

labels = MapAt[Rotate[#,90 Degree]& /@ #&, labels, {2}];

labels = MapAt[Placed[#, "RightAxis"]&, MapAt[Placed[#, Axis]&, labels,{2}], {3}];

PairedBarChart[## & @@ Map[# -> # &, datawitherrors, {3}], 
 BarSpacing -> {4, .5, .1}, 
 ChartElementFunction -> cEF, 
 ColorFunction -> "Rainbow", 
 ChartBaseStyle -> HatchFilling["Diagonal", 2, 7], 
 ChartLabels -> labels, 
 ImageSize -> 800,
 Frame -> True,
 FrameStyle -> {{Opacity[0], LineOpacity -> 0}, {Automatic, Opacity[0]}}] 

enter image description here

Replace {2} with {3} and {3} with {2} in the line labels = MapAt[...] and use BarSpacing -> {4, .5, .1} to get

enter image description here

$\endgroup$
1
  • 2
    $\begingroup$ I think Domen's answer is better (The only reason I went with ChartElementFunction approach is that simpler/more straightforward method was posted already:) $\endgroup$
    – kglr
    Dec 3 '21 at 21:02

Your Answer

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