How to do this type of percentual stacked plot in Mathematica?

This does what you want:

BarChart[RandomReal[1, {5, 5}], ChartLayout -> "Percentile"]

Mathematica graphics


A bit more detail than in belisarius' answer:

BarChart[RandomReal[1, {21, 26}], 
 ChartLayout -> "Percentile", 
 ChartLegends -> RandomSample[CharacterRange["A", "Z"], 26], 
 ChartLabels -> {Rotate[#, 90 Degree] & /@ Range[105, 125], None}, 
 ChartStyle -> ColorData[54], 
 AxesLabel -> {Placed[
    Text[Style["aa position", 14, Bold, 
      FontFamily -> Helvetica]], {13, -12}], 
    Text[Style["Frequency", 14, Bold, FontFamily -> Helvetica]]}, 
 PlotLabel -> Text[Style["Young", 16, Bold, FontFamily -> Helvetica]],
 ImageSize -> 700]

enter image description here


I've been reading a few books on visualisation theory recently and wanted to add my two English pennies - the ordering of data in stacked bar charts matters. Particularly the variance of the lower elements.

To demonstrate this, here's a function that re-orders data according to the Variance or Mean of the datasets (EDIT: Changed to Transpose on Belisarius' recommendation and used https://mathematica.stackexchange.com/a/2810/1952 for code formatting):

sortedStackedBarChart[data_List, "Sorting" -> method_, 
  "StackLayout" -> stacking_, opts : OptionsPattern[]] :=
With[{orderedData = Switch[method,
     "Variance",
     data[[All, Ordering[Variance /@ Transpose[data]]]],
     "Mean",
     data[[All, Reverse[Ordering[Mean /@ Transpose[data]]]]]
     ]},
  BarChart[orderedData, ChartLayout -> stacking, 
   FilterRules[{opts}, Options[BarChart]]]]

Generate some data, of course I've selected some of my datasets to have lower variances than others:

SeedRandom[1111987]; allRandomData = 
 Table[{RandomReal[{1.9, 2.1}], RandomReal[{0.5, 1}], 
   RandomReal[{0.2, 6}], RandomReal[{0.2, 4}], 
   RandomReal[{4, 5}]}, {10}];

Comparing the "Mean" stack ordering and "Variance" stack ordering, it's easier to understand the relative differences between each stack with Variance ordering:

Grid[{{sortedStackedBarChart[allRandomData, "Sorting" -> "Mean", 
    "StackLayout" -> "Percentile", ImageSize -> 300], 
   sortedStackedBarChart[allRandomData, "Sorting" -> "Mean", 
    "StackLayout" -> "Stacked", ImageSize -> 300]}}]

enter image description here

Grid[{{sortedStackedBarChart[allRandomData, "Sorting" -> "Variance", 
    "StackLayout" -> "Percentile", ImageSize -> 300], 
   sortedStackedBarChart[allRandomData, "Sorting" -> "Variance", 
    "StackLayout" -> "Stacked", ImageSize -> 300]}}]

enter image description here

Tags:

Plotting