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
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.
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
>
>
-- =
(* 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]
---
Hi John,
is the following acceptable?
BarChart[vals,BarLabels->labels,BarOrientation->Horizontal]
Daniel
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 -
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
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.