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

Vertical tick labels in BarChart

27 views
Skip to first unread message

John Jowett

unread,
Dec 6, 2007, 7:35:33 AM12/6/07
to
I would have thought this is a FAQ but it doesn't seem to be ...

Consider the following construction of simple Bar Charts where the bar
labels are strings. The first one looks OK. In the second one, where
there are more bars, it's a mess because all the labels overlap
(unless the graphics is increased a lot in size):

Needs["BarCharts`"]

labels = {"MQM.A7L2B1", "MQMC.9R2.B1", "MQTLI.11R2.B1"};
vals = {5, 6, 7};

BarChart[vals, BarLabels -> labels]

vals = Flatten[Table[vals, {5}]];
labels = Flatten[Table[labels, {5}]];

BarChart[vals, BarLabels -> labels]

This could be neatly solved if the labels were oriented vertically
instead of horizontally (it's easy to do this, e.g., in certain
spreadsheet applications). However I can't find any option that
allows it.
I suspect it might be possible using some special Ticks construction
but haven't taken the time to see. But this should be easy ... does
anyone know how ?

Thanks,
John Jowett

sdw

unread,
Dec 7, 2007, 3:16:41 AM12/7/07
to
bar chart uses Ticks to draw the labels. Ticks seems to have bugs (
Ticks are bugs, as you know. )

for example,

Needs["BarCharts`"]

labels = {"MQM.A7L2B1", "MQMC.9R2.B1", "MQTLI.11R2.B1"};
vals = {5, 6, 7};

vals = Flatten[Table[vals, {5}]];
labels = Flatten[Table[labels, {5}]];

(* create the kind of labels you want... *)

grlabels =
Map[ Graphics[Rotate[Text[#, {0, 0}, {-1, 0}], -\[Pi]/2, {0, 0}],
Frame -> False] &, labels];

lrng = Range[Length[labels]];

ListPlot[vals, Ticks -> {Transpose[{lrng, grlabels}], Automatic}]

which proves that if Ticks are graphics objects, things break.
further, you cannot specify Tics as a simple sequence of graphics
objects since the form of Ticks assumes that a list has position
information.

further, there is no concept of a Rotation style, so you cannot specify
a text direction without creating a graphics object so forget that too.

Just export it to Excel and save yourself for higher ROI activities.

DrMajorBob

unread,
Dec 7, 2007, 3:17:42 AM12/7/07
to
When the labels are wide, the sensible solution is

BarChart[vals, BarLabels -> labels, BarOrientation -> Horizontal]

Bobby

On Thu, 06 Dec 2007 06:27:38 -0600, John Jowett <John.M...@gmail.com >
wrote:

> I would have thought this is a FAQ but it doesn't seem to be ...
>
> Consider the following construction of simple Bar Charts where the bar
> labels are strings. The first one looks OK. In the second one, where
> there are more bars, it's a mess because all the labels overlap
> (unless the graphics is increased a lot in size):
>
> Needs["BarCharts`"]
>
> labels = {"MQM.A7L2B1", "MQMC.9R2.B1", "MQTLI.11R2.B1"};
> vals = {5, 6, 7};
>
> BarChart[vals, BarLabels -> labels]
>
> vals = Flatten[Table[vals, {5}]];
> labels = Flatten[Table[labels, {5}]];
>
> BarChart[vals, BarLabels -> labels]
>
> This could be neatly solved if the labels were oriented vertically
> instead of horizontally (it's easy to do this, e.g., in certain
> spreadsheet applications). However I can't find any option that
> allows it.
> I suspect it might be possible using some special Ticks construction
> but haven't taken the time to see. But this should be easy ... does
> anyone know how ?
>
> Thanks,
> John Jowett
>
>

-- =

DrMaj...@bigfoot.com

sdw

unread,
Dec 7, 2007, 3:18:43 AM12/7/07
to
ok - here you go....


(* create the bar chart with labels in the wrong orientation, then
convert the whole thing to primitives with FullGraphics to circumvent
all of the graphics ticks bugs*)

b = BarChart[vals, BarLabels -> labels] // FullGraphics ;

(* apply a transformation to all text primitives, being careful to only
modify the non-numeric ones since the numeric ones are the vertical axis
text marks.
The transformation replaces the offending label with a rotated and
translated version of the label. *)

b /. Text[ v_String, al_List, bl_List] -> Translate[Rotate[Text[v, {0,
0}, {-1, 0}], -\[Pi]/2, {0, 0}], al]


---

dh

unread,
Dec 7, 2007, 3:30:57 AM12/7/07
to

Hi John,

is the following acceptable?

BarChart[vals,BarLabels->labels,BarOrientation->Horizontal]

Daniel

John Jowett

unread,
Dec 10, 2007, 4:40:13 AM12/10/07
to
Thanks sdw,
This is a solution. I didn't think of using FullGraphics.
One can make the rule apply more specifically with

ReplacePart[b, {1, 2} -> b[[1, 2]] /.


Text[v_String, al_List, bl_List] ->

Translate[Rotate[Text[v, {0, 0}, {1, 0}], \[Pi]/2, {0, 0}], al]]

or even by building a list of rules for the specific text labels that
are being used.
However I hope that a standard method for this will appear in due
course ...

To everyone who suggested horizontal bars: Yes, in some cases
horizontal bars may look better but it's not really an option in my
application where vertical bars are traditional and people expect
them. Also, these plots are often combined with plots of other
functions in a vertical stack.

John

> > John Jowett- Hide quoted text -
>
> - Show quoted text -


John Jowett

unread,
Dec 11, 2007, 3:31:04 PM12/11/07
to
I can give another answer to my original question that was given to me
by Carl Woll of Wolfram Research. It uses an experimental feature of
Mathematica so is subject to the usual caveats that it may have some
bugs, is experimental and subject to change:

labelsr = (Experimental`Rotated[#1, Pi/2] &) /@ labels

BarChart[vals, BarLabels -> labelsr]

This is very nice and easy and provides a lot of flexibility. You can
rotate the labels by arbitrary angles, rotate only a subset, etc.

John

sdw

unread,
Dec 12, 2007, 1:20:40 AM12/12/07
to
this looked good at first glance, but then looked bad when I tried

BarLabels -> {Experimental`Rotated["alpha beta", \[Pi]/2],
Experimental`Rotated["b", Pi/2], "c"}]

here, instead of aligning the "top" of the boxes, it aligned the
centers.

this is close, but in my opinion not as nice as having the tops aligned.

Still think that they fundamentally need to address the ticks problem
and allow for graphics objects.

0 new messages