Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I'm trying to label a bar chart with rotated labels

see this simple example:

BarChart[RandomReal[1, 10], BarSpacing -> Large, 
 ChartLabels -> 
  Placed[ToString /@ RandomReal[100000, 10], Axis, Rotate[#, Pi/4] &]]

In this case the center of the label is aligned with the center of the bar.

Mathematica graphics

I want the right/top of the label to be aligned with the center of the bar instead, basically shift the labels over a bit to the left. How can this be done?

Also, what if all the labels have different lengths:

Mathematica graphics

share|improve this question
    
You could try including an AdjustmentBox in your Placed function. I haven't time to write this up as a full answer just now but someone else might like to try. –  Verbeia Mar 6 '12 at 22:08

3 Answers 3

up vote 10 down vote accepted

Something like this could also do:

BarChart[RandomReal[1, 10], BarSpacing -> Large, 
  ChartLabels -> 
   Placed[ToString /@ 
     RandomChoice[ExampleData[{"Text", "DeclarationOfIndependence"}, "Words"], 10], 
     Axis, Rotate[#, Pi/4] &]] /. 
 ImageScaled[{1/2, 1}] -> ImageScaled[{0.9, 1}]

enter image description here

share|improve this answer
    
Nice to see you at the site. Btw, I wasn't able to get your code to run (even after changing EampleData to ExampleData. –  David Carraher Mar 6 '12 at 23:56
    
@DavidCarraher: Please try now. –  István Zachar Mar 7 '12 at 0:07
    
It works great! –  David Carraher Mar 7 '12 at 1:23
    
@IstvánZachar Thanks for the edit! –  belisarius Mar 7 '12 at 13:31

See if this works.

b = BarChart[RandomReal[1, 10], BarSpacing -> Large, 
 ChartLabels ->  Placed[ToString /@ RandomReal[100000, 10], Axis, 
     Rotate[#, Pi/4] &]];

b /. {Text[p_, Offset[ _, pos_], is_] -> Text[p, Offset[{-10, -2}, pos], is]}

chart

I used AbsoluteOptions[b // InputForm] to inspect the code and identify the Text commands that place the chart labels. I then offset the labels leftward by 10 printer's points.

share|improve this answer

It can be done using the pos parameter of Placed:

BarChart[RandomReal[1, 10], BarSpacing -> Large, 
 ChartLabels -> 
  Placed[(ToString /@ 10^RandomInteger[10, 10]), 
         {{1, 0}, {1, 1}} 
         (* pos: bar position: lower right corner (1,0), 
                 text box:     upper right corner (1,1)  *), 
         Rotate[#, Pi/4] &
  ]
]

Mathematica graphics

Use {0.5,0} instead of {1,0} to align with the bottom center of the bar (which I find slightly less esthetical pleasing, but YMMV):

Mathematica graphics

share|improve this answer
    
NIce approach. I find {0.75, 0} more pleasing to the eye than {0.5, 0}. –  David Carraher Mar 6 '12 at 23:02
    
I also like {0.75,0} however, the labels are slightly closer to the Axis here then they are with Axis, can this be solved? edit: {{0.75, 0}, {1, 1.1}} looks a bit better but I think it makes the top uneven. –  s0rce Mar 7 '12 at 2:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.