8
$\begingroup$

I would like to generate a stacked BarChart using following sudo dataset.

data = {
   {2, 2, 2.5, 2.5, 3, 1, 3, 4},
   {3, 1, 2.5, 2.5},
   {3, 2}
   };

In essence, I want to get the BarChart to display the Tally of the sublists in a logically consistent way.

enter image description here

This is the code I am experimenting with

BarChart[
 (* data *)
 , PlotTheme -> "Classic"
 , BarOrigin -> Left
 , Joined -> True
 , LabelingFunction -> Center
 , ChartLayout -> "Stacked"
 , ChartLabels -> {Map[Style[#, 12] &, {"x", "y", "z", "e", "d"}], 
   None}
 , ChartLegends -> {1, 2, 2.5, 3, 4}
 ]

Note, I have tried doing it with RectangleChart using following code as example...

RectangleChart[
 Tally /@  data
 , ChartLayout -> "Stacked"
]

However, I couldn't get the rectangles to be same size on 'x' axes.

$\endgroup$
  • $\begingroup$ You mean that the boxes should all be of the same size, or what is the problem with your example bar chart? $\endgroup$ – C. E. Jul 2 at 12:37
  • $\begingroup$ @C.E. I want to Tally the sublists and have the bars in the BarChart display the resuls of the tally. RactangleChart almost does it all for me. Except, I would like all boxes visually to be the same length across 'x' axes. $\endgroup$ – e.doroskevic Jul 2 at 12:45
  • $\begingroup$ @C.E, I have uploaded a visual of what I want to achieve $\endgroup$ – e.doroskevic Jul 2 at 13:02
  • 1
    $\begingroup$ thank you, it's very clear now. $\endgroup$ – C. E. Jul 2 at 13:05
6
$\begingroup$
minmax = MinMax[data];

labeleddata = (Labeled[Style[#2, ColorData[{"Rainbow", minmax}]@#], 
        Row[{#, " (", #2, ")"}], Center] & @@@ SortBy[First]@#) & /@ 
   Tally /@ data;

BarChart[labeleddata, ChartLayout -> "Stacked", BarOrigin -> Left]

enter image description here

Update: Using the "Classic" color scheme:

union = Union[Flatten[data]];

cF = Blend[Transpose[{union, Charting`CommonDump`rogerStyles[Length@union]}], #] &;

labeleddata2 = (Labeled[Style[#2, cF @ #], Row[{#, " (", #2, ")"}], 
        Center] & @@@ SortBy[First]@#) & /@ Tally /@ data;

BarChart[labeleddata2, ChartLayout -> "Stacked", BarOrigin -> Left]

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ thank you for getting involved. How could I achieve consistency in color? For example, how could I get 1 to be purple, 2 green, 2.5 yellow, 3 orange, and 4 red? $\endgroup$ – e.doroskevic Jul 2 at 13:18
  • $\begingroup$ @e.doroskevic, please see the updated version. $\endgroup$ – kglr Jul 2 at 13:37
  • $\begingroup$ this is what I was looking for :D I just need to now figure out how to get the same color schema as in "classic" PlotTheme. Thank you! $\endgroup$ – e.doroskevic Jul 2 at 13:45
  • 1
    $\begingroup$ Honestly, there should be a button to give extra points, or send $ for coffee as a form of gratitude, here on StackExchange. I am grateful for your help. Thank you! $\endgroup$ – e.doroskevic Jul 2 at 13:50

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.