Message Boards Message Boards

6 Replies
4 Total Likes
View groups...
Share this post:

Grouped and stacked bar charts

Posted 12 years ago
Hi all,

What's the easiest way to get grouped, stacked bar charts? Has someone written a function that does this? I've tried through BarChart, but it doesn't seem to support dimensionality of the data (looked at BarSpacing option as well, nothing that I just need to visually separate groups, the list of stacked bars can be flat).

6 Replies
The BarChart function has an option called ChartLayout. You can see examples of it used in the documentation, like this example:
BarChart[RandomReal[1, {5, 4}], ChartLayout -> "Stacked"]
Is this what you are looking for? If not, maybe a quick picture will help us understand better what kind of bar chart you would like to make.
POSTED BY: Sean Clarke
One way is to create a custom ChartElementFunction that takes the extra dimension of data as metadata. Here's an example in 3D that should illustrate the idea (and should be easily modifiable for your purposes.)

In[1]:= ChartElementData[BarChart3D]
Out[1]= {"Cone", "Cube", "Cylinder", "DoubleProfileCube", "FadingCube", "GradientScaleCube", "ProfileCube", "SegmentScaleCube", "SquareWaveCube", "TriangleWaveCube"}

 StackedBar3D[scheme_, shape_String: "ProfileCube"][{{x0_, x1_}, {y0_, y1_}, {z0_, z1_}},
   val_, meta_]:=Block[{colorlist, n, offsets, heights},
   heights = Flatten[meta];
   offsets = Prepend[Accumulate[heights], 0];
   colorlist = Switch[scheme,_Integer, ColorData[scheme, "ColorList"],_List, scheme,_String, ColorData[scheme, #] & /@ Range[0, 1,1/10]];
   n = Length[colorlist];
   Table[{colorlist[[Mod[i, n, 1]]],
     Tooltip[ChartElementData[shape][{{x0, x1}, {y0, y1}, {offsets[[i]], offsets[[i + 1]]}}, val, meta], heights[i]]}, {i, Length[offsets] - 1}]

d1 = RandomReal[1, {4, 4}];
d2 = RandomReal[1, {4, 4}];
d3 = RandomReal[1, {4, 4}];
d4 = RandomReal[1, {4, 4}];
data = MapThread[Plus[##] -> {##} &, {d1, d2, d3, d4}, 2];

BarChart3D[data, ChartElementFunction -> StackedBar3D["Rainbow"], ChartLayout -> "Grid"]

POSTED BY: Brett Champion
Posted 4 years ago

I take a more extreme view compared to many here: One should avoid stack bar charts at all costs and double stacked bar charts sounds like real bad dream. Not making double stacked bar charts easy to do I'm sure is not by design but I'm glad it's hard to do.

I believe that the incentive to have stacked bar charts is to attempt to display several aspects of the data in a single figure. But usually the resulting display does not convey any of the multiple aspects very well. Unless you have some limitation on pages, one should have separate figures for each objective. Maybe in the olden days where things were printed, there was the need to cut down on the number of figures. But not any more.

Do a Google search for "alternatives to stacked bar charts" and you'll see alternatives and why stacked bar charts (and bar charts for that matter) have undesirable properties.

POSTED BY: Jim Baldwin

I have the same question, but the answers don´t address the original question. I want to make graphics like the one shown below. These types of graphs have multiple applications and should be easy to do. I found some complicated ways to do them.

enter image description here

But how can you have double stacked chartbars? For example, how can you show supply and demand for multiple locations? How can you show, for different regions, the supply and demand of water (e.g. on one side runoff and infiltration and on the other side use in agriculture, use in industry, use for public water supply)?

Thank you Jim.
I still think sometimes stacked bars may be useful to illustrate some phenomena. Please see the following application I did:

Even though it is in Spanish, you can see that there are a lot of stacked bar charts showing a lot of information about each water utility. You can compare the performance of the different water utilities by looking at the total production of water ("P"), the amount billed ("F") and the amount actually collected (revenues) ("C"). Non revenue water is a very important problem all over the world. The stacked bars show how much water (or money) is lost in each concept.

The concepts are here: the illustration is a sort of stacked bar chart.

Any ideas on how to show the balance in another way.

Regards, Ricardo

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract