Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

ChartLabels for Stacked, Grouped BarCharts?

726 views
Skip to first unread message

Gordon Robertson

unread,
Dec 20, 2009, 6:54:19 AM12/20/09
to
Could I ask for help with ChartLabels for Stacked and Grouped BarCharts in
Mathematica 7? I've checked the MathGroup archives and Mathematica help docs.

I need to assess Stacked and Grouped BarCharts for a 2D dataset that
consists of 20 groups of three values. Labels for the groups are
{31,32,...,50}. From the help docs, it's easy to display the bars in either
mode for simple baseline cases. But I don't see how to put a *single* label
or tick *per stack or group*, below the X-axis.

The following simulates the 2D dataset that will be read in from a file:
tab2 = {{"a",1,2,3},{"b",2,3,4},{"c",3,4,5}}

labs2 = tab2[[All,1]]

data2 = tab2[[All,2;;4]]


I need "Stacked" and "Grouped" BarCharts that have "a","b" and "c",
respectively, as the labels for the three bar stacks or groups, below the
X-axis. The following commands do not give this:
BarChart[data2,ChartLayout->"Stacked",ChartLabels->Placed[labs2,Below]]
BarChart[data2,ChartLayout->"Grouped",ChartLabels->Placed[labs2,Below]]

The "Stacked" graph has a vertical stack of "a" "b" and "c" below the lower
edges of *each* block in *each* bar/block stack; the "Grouped" graph has a
label beneath *each* bar. Likely this makes sense in terms of general 'list '
rules, but it does not deliver what I need.

I've tried expanding the label list and using a list that includes None for
the second argument of Placed[], but have not quickly found a solution.

I can guess that Ticks may be the approach to use, but wonder if I could as k
more experienced folks for help. Could I also ask WRI to implement (or
document) these BarChart use cases.

Thanks in advance for your help in this.

Gordon
--
Gordon Robertson
BC Cancer Agency Genome Sciences Centre
Vancouver BC Canada
www.bcgsc.ca
604-707-5800 x675416


Tomas Garza

unread,
Dec 21, 2009, 3:32:25 AM12/21/09
to
I've had the exact same problem a number of times, and I think that presently the best approach is to fiddle with Epilog and place your labels exactly where you want. With a large number of groups it may require some extra work, but once you get familiar with it you'll get a nice result. For example,

BarChart[data2,ChartLayout->"Stacked",Epilog->{Style[Text["group a",{0.5,-0.4}],10],Style[Text["group b",{1.7,-0.4}],10],Style[Text["group c",{3,-0.4}],10]},PlotRange->{{0,3.5},{-0.5,12}}]

BarChart[data2,ChartLayout->"Grouped",

Epilog->{Style[Text["group a",{1.5,-0.2}],10],Style[Text["group b",{5,-0.2}],10],Style[Text["group c",{8.5,-0.2}],10]},PlotRange->{{0,10},{-0.3,5}}]

Tomas

> Date: Sun, 20 Dec 2009 06:54:38 -0500
> From: grobe...@bcgsc.ca
> Subject: ChartLabels for Stacked, Grouped BarCharts?
> To: math...@smc.vnet.net

Ariel Sepulveda

unread,
Dec 21, 2009, 3:32:46 AM12/21/09
to
Dear Gordon:

You can find a solution to your problem in the package BESTViewpoints (
http://www.wolfram.com/products/applications/bestviewpoints/). Screenshots
with examples related to your problem are on page 6 and 9 of this link
http://www.wolfram.com/products/applications/bestviewpoints/examples.pdf.
Additionally, documentation for the function that creates the former images
can be found in the *Viewpoints* link at the bottom of this web page
http://www.prontoanalytics.com/products/mxlplus/documentation.htm.

Assuming that your data looks like this
data2={{"response1","response2","group1","group2","group3"},
{3.25, 1.76, "A1", "B3", "C2"},
{2.56, 0.54, "A3", "B7", "C1"},
...};
A script to solve your problem using Viewpoints would be:
Viewpoints[data2, {"response1"}, "By" -> {"group1", "group2"}, "PlotID" ->
"StackedBarChart", "VerticalBars" -> True];

Viewpoints supports any number of groups and more than one response variable
for chart types like PercentileBarChart, Pareto, BoxPlots, PieChart, and
Line, among other. BEST Viewpoints can be used for simplifying the creation
of charts by easily setting the right image size, aspect ratio, bar
annotations, and many other plot options. However, if you need to implement
a solution as part of a Mathematica program you can
use BESTViewpoints interface to create the plot the first time and get from
the interface the script with all 36 Viewpoints options set. The output
script can then be used in a program.

Finally, I'm working on Charts Explorer, a low-cost product that will only
include a spreadsheet-like interface for data importing and manipulation
plus an interface for the function Viewpoints. To request additional
information about this application please go to
http://www.prontoanalytics.com/contactus.php and leave a message regarding
the Charts Explorer such that I can communicate when the product is ready.

Ariel Sepulveda

Bob Hanlon

unread,
Dec 21, 2009, 3:32:57 AM12/21/09
to

tab2 = {{"a", 1, 4, 2}, {"b", 3, 5, 1}, {"c", 3, 2, 4}};

labs2 = First /@ tab2;

data2 = Rest /@ tab2;

GraphicsRow[
BarChart[data2,
ChartLayout -> #,
ChartLabels ->
{Placed[labs2, Axis],
None}] & /@
{"Stacked", "Grouped"},
ImageSize -> 500]


Bob Hanlon

---- Gordon Robertson <grobe...@bcgsc.ca> wrote:

=============

Brett Champion

unread,
Dec 21, 2009, 3:33:08 AM12/21/09
to

It sounds like you want:

BarChart[data2, ChartLayout -> "Stacked", ChartLabels ->
{Placed[labs2, Below], None}]
BarChart[data2, ChartLayout -> "Grouped", ChartLabels ->
{Placed[labs2, Below], None}]

Examples with this behavior in the documentation (not a comprehensive
list):

BarChart > Examples > Scope > Labeling and Legending > "For rows of
data"
BarChart > Examples > Options > ChartLabels > "Associate labels with
rows or datasets:"
BarChart > Examples > Applications > (Portfolio example)
ChartLabels > Examples > Basic Examples > (third example)

and is generally covered by

"ChartLabels->{spec1,spec2,=85} uses the successive speci to specify
labels for successive dimensions in nested lists of datasets." in the
ChartLabels documentation.

Note that ChartLabels, ChartLegends, ChartElements have similar
behavior.

Brett Champion
Wolfram Research


Gordon Robertson

unread,
Dec 21, 2009, 3:33:51 AM12/21/09
to
Brett

Thank you for your note. As you indicate, the cases I asked about are described in the Examples, Options and Applications.

Gordon


On 09-12-20 9:00 PM, "Brett Champion" <bre...@wolfram.com> wrote:

On Dec 20, 2009, at 5:54 AM, Gordon Robertson wrote:

> Could I ask for help with ChartLabels for Stacked and Grouped
> BarCharts in
> Mathematica 7? I've checked the MathGroup archives and Mathematica
> help docs.

<snip>

> I can guess that Ticks may be the approach to use, but wonder if I
> could as k
> more experienced folks for help. Could I also ask WRI to implement (or
> document) these BarChart use cases.
>

It sounds like you want:

BarChart[data2, ChartLayout -> "Stacked", ChartLabels ->
{Placed[labs2, Below], None}]
BarChart[data2, ChartLayout -> "Grouped", ChartLabels ->
{Placed[labs2, Below], None}]

Examples with this behavior in the documentation (not a comprehensive
list):

BarChart > Examples > Scope > Labeling and Legending > "For rows of
data"
BarChart > Examples > Options > ChartLabels > "Associate labels with
rows or datasets:"
BarChart > Examples > Applications > (Portfolio example)
ChartLabels > Examples > Basic Examples > (third example)

and is generally covered by

"ChartLabels->{spec1,spec2,...} uses the successive speci to specify


labels for successive dimensions in nested lists of datasets." in the
ChartLabels documentation.

Note that ChartLabels, ChartLegends, ChartElements have similar
behavior.

Brett Champion
Wolfram Research

--
Gordon Robertson
Canada's Michael Smith Genome Sciences Centre
Vancouver BC Canada
www.bcgsc.ca
604-707-5900 x5416

0 new messages