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?
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communityI 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?
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]}}]
Table[Variance[data[[All, i]]], {i, dims[[2]]}]
you can use Variance /@ Transpose@data
. The same with Mean
$\endgroup$
– Dr. belisarius
Sep 11 '15 at 12:14
Module[{..},With[...]]
mess. Cheers
$\endgroup$
– Charlie Joey Hadley
Sep 11 '15 at 13:04
BarChart
: there is aChartLayout
called "Stacked"... Have you searched this site? Quite a few results pop up for "stacked barchart". $\endgroup$ – MarcoB Sep 10 '15 at 16:24BarChart
andHistogram
support this type of data visualization. $\endgroup$ – march Sep 10 '15 at 16:24Table[BarChart[RandomReal[1, {5, 5}], ChartLayout -> l], {l, {"Stacked", "Percentile"}}]
$\endgroup$ – Dr. belisarius Sep 10 '15 at 16:25"Percentile"
(which is what I want) in the Options section of the documentation, underChartLayout
, where I would expect to find it. Can you post an answer so I can accept it? $\endgroup$ – becko Sep 10 '15 at 16:30Stacked
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