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

I have data of letter occurrences in different positions. I want to plot the frequency of each letter in each position.

The type of plot I have in mind is as follows:

How can I do this in Mathematica?

• What have you tried? Take a look at the documentation for BarChart: there is a ChartLayout called "Stacked"... Have you searched this site? Quite a few results pop up for "stacked barchart". – MarcoB Sep 10 '15 at 16:24
• I think both BarChart and Histogram support this type of data visualization. – march Sep 10 '15 at 16:24
• Table[BarChart[RandomReal[1, {5, 5}], ChartLayout -> l], {l, {"Stacked", "Percentile"}}] – Dr. belisarius Sep 10 '15 at 16:25
• @belisarius Thanks. I did not find the option "Percentile" (which is what I want) in the Options section of the documentation, under ChartLayout, where I would expect to find it. Can you post an answer so I can accept it? – becko Sep 10 '15 at 16:30
• @MarcoB Stacked is not what I want. Percentile is more like it, but that's not in the documentation. – becko Sep 10 '15 at 16:31

This does what you want:

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

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]

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]}}]

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

• really impressive +1 – Dr. belisarius Sep 11 '15 at 11:54
• Instead of Table[Variance[data[[All, i]]], {i, dims[[2]]}] you can use Variance /@ Transpose@data. The same with Mean – Dr. belisarius Sep 11 '15 at 12:14
• @belisarius eurgh, how embarrassing. Thanks for suggestion, allows the removal of the Module[{..},With[...]] mess. Cheers – Charlie Joey Hadley Sep 11 '15 at 13:04
• Glad to help :) – Dr. belisarius Sep 11 '15 at 13:20