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 want to add units to my numbers to display them as the labels of my chart. I tried this:

sizes = {10718, 881, 395, 387, 375, 309, 297};
sizesStr = ToString[sizes] <> " kB";
BarChart[sizes, BarOrigin -> Left, ChartLabels -> Placed[sizesStr, Right]]

enter image description here

So instead of each bar displaying "xxx kB" I get the "kB" after the whole list. Should I blame StringJoin for not being listable? How do I get the numbers in each of the bars?

Related: for the short bars the text will go beyond the vertical axis. Can I get the labels at the right of the bars, instead of inside them?

share|improve this question
    
Yes it was the non Listable aspect of ToString that brought you undone. I've taken the liberty of modifying the title to make it more reflective of the question –  Mike Honeychurch Jul 14 at 8:41

1 Answer 1

This can be done in one of two ways. In both cases I am bypassing the creation of sizesStr:

sizes = {10718, 881, 395, 387, 375, 309, 297};
BarChart[sizes, BarOrigin -> Left, LabelingFunction -> (Placed[
     Row[{#1, " kB"}], {{1, 0.5}, {0, 0.5}}] &)]

enter image description here

For that example I am using Row to "join" the value with the unit. I am using the Placed syntax to be found at the bottom of Details in the docs.

enter image description here

Note that {pos, epos} are scaled coordinates therefore {{1, 0.5}, {0, 0.5}} means that I am placing the middle of the left edge of the label {0,0.5} at the middle of the right edge of the bar {1,0.5}. Adding a Spacer might make it more aesthetically pleasing: Row[{Spacer[5], #1, " kB"}]

Alternatively:

BarChart[sizes, BarOrigin -> Left, 
 ChartLabels -> 
  Placed[sizes, {{1, 0.5}, {0, 0.5}}, ToString[#] <> " kB" &]]

enter image description here

In this example the third argument to Placed is used, i.e. a function that converts the value to a string and joins " kB" to it.

share|improve this answer

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.