1
$\begingroup$

See below:

The problem is that the "leftist" labels are being counted twice on the right stacked bar. How to fix this?

$\endgroup$
1
  • 1
    $\begingroup$ Please, please, please do not post images of code. $\endgroup$
    – ktm
    Aug 26, 2019 at 16:57

1 Answer 1

2
$\begingroup$

Looks like a bug.

Fake data to replicate the issue:

data = {<|"a" -> 1, "b" -> 2, "c" -> 3|>, <|"x" -> 5, "y" -> 2, "z" -> 3|>};

BarChart[data, ChartLayout -> "Stacked", ChartLabels -> Callout[Automatic, Automatic]]

enter image description here

Same issue with default layout:

BarChart[data, ChartLabels -> Callout[Automatic, Automatic]]

enter image description here

A work-around: wrap Callout on input data using KeyValueMap[Callout[#2, #]&]:

BarChart[KeyValueMap[Callout[#2, #]&] /@ data, ChartLayout -> "Stacked"]

enter image description here

BarChart[KeyValueMap[Callout[#2, #]&] /@ data]

enter image description here

Update: It gets weirder with additional data sets:

data2 = {<|"a" -> 1, "b" -> 2, "c" -> 3|>, 
   <|"x" -> 5, "y" -> 2, "z" -> 3|>,  <|"u" -> 5, "v" -> 2|>, <||>};

BarChart[data2, ChartLayout -> "Stacked", 
 ChartLabels -> Callout[Automatic, Automatic]]

enter image description here

We need to delete empty Associations before applying the work-around:

BarChart[KeyValueMap[Callout[#2, #] &] /@ 
  DeleteCases[data2, Association[]], ChartLayout -> "Stacked"]

enter image description here

It seems that the issue is not limited to Callouts either:

BarChart[data2, ChartLabels -> Automatic]

enter image description here

BarChart[KeyValueMap[Labeled[#2, #] &] /@ DeleteCases[data2, Association[]]]

enter image description here

$\endgroup$
4
  • $\begingroup$ I assume these are bugs in Mathematica? $\endgroup$
    – George
    Aug 26, 2019 at 18:35
  • 1
    $\begingroup$ @George, they sure are. You might want to report to WRI. $\endgroup$
    – kglr
    Aug 26, 2019 at 18:47
  • $\begingroup$ Note that it's assumed in BarChart that all the bars of a particular color are the "same" in some way. In general with associations, the later ones are taken to have all the keys seen previously, even if they're missing. $\endgroup$ Aug 26, 2019 at 20:40
  • $\begingroup$ Brett, is there a reason for accumulation of keys? $\endgroup$
    – kglr
    Aug 26, 2019 at 21:19

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.