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:

sample image

How can I do this in Mathematica?

  • 3
    $\begingroup$ 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". $\endgroup$
    – MarcoB
    Sep 10 '15 at 16:24
  • $\begingroup$ I think both BarChart and Histogram support this type of data visualization. $\endgroup$
    – march
    Sep 10 '15 at 16:24
  • 1
    $\begingroup$ Table[BarChart[RandomReal[1, {5, 5}], ChartLayout -> l], {l, {"Stacked", "Percentile"}}] $\endgroup$ Sep 10 '15 at 16:25
  • $\begingroup$ @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? $\endgroup$
    – becko
    Sep 10 '15 at 16:30
  • $\begingroup$ @MarcoB Stacked is not what I want. Percentile is more like it, but that's not in the documentation. $\endgroup$
    – becko
    Sep 10 '15 at 16:31

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,
     data[[All, Ordering[Variance /@ Transpose[data]]]],
     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

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

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.