# How to construct a stacked barchart

UPDATED to clearify

I want to construct a stacked barchart from a list with three elements:

list = Table[{i, x, y}, {i, 10}]


where x has a value between 0 and 100, and y has a value between 0 and 100 so it looks like this:

llist = {{1, 6, 611}, {2, 57, 732}, {3, 5, 526}, {4, 91, 72}, {5, 45,
583}, {6, 98, 552}, {7, 16, 981}, {8, 54, 439}, {9, 80, 804}, {10,
9, 633}};


I want to stack y based on brackets of x (eg. brackets of size 10). Based on the list above this gives the following brackets.

• Bracket 0 - 10 consists of $y$ value (s) 611, 526, 633
• Bracket 10 - 20 consists of $y$ value (s) 981
• Bracket 20 - 30 consists of $y$ value (s) empty
• Bracket 30 - 40 consists of $y$ value (s) empty
• Bracket 40 - 50 consists of $y$ value (s) 583
• Bracket 50 - 60 consists of $y$ value (s) 732, 439
• Bracket 70 - 80 consists of $y$ value (s) empty
• Bracket 80 - 90 consists of $y$ value (s) 804
• Bracket 90 - 100 consists of $y$ value (s) 72, 552

The result should look something like this:

Some context: this is a plot I want to make to show how much influence is exerted (y values) on certain policy positions (x values). The first elements are actors. So Actor 1 exerts 611 influence on policy position 6 (first bracket 0-10). For each bracket I want to visually see how much influence is exerted by the actors and how this looked stacked

• No I don't want stacked counts but I want to stack the values themselves (e.g. for the first bracket 611, 526 and 633 stacked on top of each other). Some context: this is a plot I want to make to show how much influence is exerted (y values) on certain policy positions (x values). The first elements are actors. So Actor 1 exerts 611 influence on policy position 6 (first bracket 0-10). For each bracket I want to visually see how much influence is exerted by the actors and how this looked stacked. – sanderws Aug 9 '13 at 7:39
• My suggestion: Module[{data, brackets}, data = GatherBy[Append[#, #[[2]]/10 // IntegerPart] & /@ llist, Last]; brackets = data[[All, 1, 4]]; If[MemberQ[brackets, #], , AppendTo[data, {{0, 0, 0, #}}]] & /@ Range[Max[brackets]]; BarChart[Sort[data, #1[[1, -1]] < #2[[1, -1]] &][[All, All, 3]], ChartLayout -> "Stacked"] ] (quick and dirty & different to @anon's, is this what you want?) – Pinguin Dirk Aug 9 '13 at 8:07
• @PinguinDirk I made a mistake, I meant to build one like yours :) – C. E. Aug 9 '13 at 8:09
• @PinguinDirk yes this is exactly what I am looking for. I updated the question with a screenshot of the output of your solution. How can we get the question off hold so you can answer the question as an actual answer? – sanderws Aug 9 '13 at 8:22
• Thanks for your answers @Anon and PinguinDirk. I'm still learning my way around stackexchange (especially how to state clear questions) and you've been a great help! – sanderws Aug 9 '13 at 13:43

Here's an alternative way to create the chart that Pinguin Dirk created:

rules = #[[2]] -> #[[3]] & /@ {{1, 6, 611}, {2, 57, 732}, {3, 5,
526}, {4, 91, 72}, {5, 45, 583}, {6, 98, 552}, {7, 16, 981}, {8,
54, 439}, {9, 80, 804}, {10, 9, 633}};
bins = BinLists[rules[[All, 1]], 10] /. rules /. {} -> {0}
BarChart[bins, ChartLayout -> "Stacked"]


Another way:

bins = Table[Select[list, i - 10 < #[[2]] <= i &], {i, 10, 100, 10}];
BarChart[bins /. {y : {_, _, x_} /; Depth[y] == 2 :> x, {} -> {0}}, ChartLayout -> "Stacked"]

• I like BinLists! +1 – Pinguin Dirk Aug 9 '13 at 9:49

You can use the ChartLayout option with value "Stacked".

d1 = Range@10;
d2 = 4 + Range@10;
d3 = 6 + Range@10;

BarChart[Transpose[{d1, d2, d3}], ChartLayout -> "Stacked"]

• Can you explain how it is related to the question and the chart OP has posted, I'm still confused :/ – Kuba Aug 8 '13 at 8:44
• @Kuba I agree that the OP would get a more complete answer if they clarified better how they wish to partition their data. But in the absence of that clarification, my answer is offered in the hope that if a stacked bar chart is what they need, then they will find this a useful element to their solution. – image_doctor Aug 8 '13 at 12:43
• Ah, I thought you have a good interpretationto share :) but I find this answer useful. The problem is I really doubt if OP is looking for stacked bar chart. It rather looks like some sort of ColorFunction is what he need. We will see... maybe ;) – Kuba Aug 8 '13 at 12:51

Here's my idea from the comments, certainly with room for improvement.

Module[{data, brackets},
data = GatherBy[Append[#, #[[2]]/10 // IntegerPart] & /@ llist, Last];
brackets = data[[All, 1, 4]];
If[MemberQ[brackets, #], , AppendTo[data, {{0, 0, 0, #}}]] & /@ Range[Max[brackets]];
BarChart[Sort[data, #1[[1, -1]] < #2[[1, -1]] &][[All, All, 3]],
ChartLayout -> "Stacked"]]


I am using GatherBy to get the bins/brackets, opposed to BinLists in Anon's answer. I chose this approach as at first, I didn't notice you wanted to show the "empty" bins as well. In that case (not showing empty bins), the code can be simplified substantially, to something like:

BarChart[
Sort[
GatherBy[Append[#, #[[2]]/10 // IntegerPart] & /@ llist, Last],
#1[[1, -1]] < #2[[1, -1]] &][[All, All, 3]],
ChartLayout -> "Stacked"]