13
$\begingroup$

My code:

numbers = {117, 128, 171, 189, 202, 260, 267, 339, 427, 1302};  
languages = {"Lahnda", "Japanese", "Russian", "Bengali",   
   "Portuguese", "Hindi", "Arabic", "English", "Spanish", "Chinese" };  
font = "Verdana";

BarChart[numbers,   
BaseStyle -> Directive[FontFamily -> font, FontSize -> 13],  
ChartStyle -> 10, ImageSize -> 560, BarOrigin -> Left,   
ChartLabels ->   
Placed[{numbers, languages}, {{{0.5, 0.5}, {0.5, 0.5}}, {{1, 0.5}, {-0.2, 0.5}}}, # &],  
AxesLabel -> {"", "millions\nof speakers"}]

which produces the following plot: enter image description here

The black text is hard or even impossible (e.g. first bar) to read on dark colors. Is there a way to automagically the text color for best readability?

For extra credit:
It seems I used the pos parameters for Placed wrong/horribly, since the left margin is a fraction of the word length. The documentation for Placed doesn't seem to say how to use pos. How can I get an equal left margin, independent of text length?

using version 9.0.1

$\endgroup$
  • $\begingroup$ How about panels? $\endgroup$ – Feyre Sep 3 '16 at 11:09
10
$\begingroup$
BarChart[numbers, BaseStyle -> Directive[FontFamily -> font, FontSize -> 13], 
 ChartStyle -> 10, ImageSize -> 560, BarOrigin -> Left, 
 ChartLabels -> Placed[languages, After], 
 LabelingFunction -> (Placed[
  Style[#, If[Plus @@ ColorData[10, Last[#2]] < 1.5, White, Black]], Center] &), 
 AxesLabel -> {"", "millions\nof speakers"}]

Out0


An alternative placement of the numbers:

BarChart[numbers, BaseStyle -> Directive[FontFamily -> font, FontSize -> 13], 
 ChartStyle -> 10, ImageSize -> 560, BarOrigin -> Left, 
 ChartLabels -> Placed[languages, After],
 LabelingFunction -> (Placed[
  Style[#, If[Plus @@ ColorData[10, Last[#2]] < 1.5, White, Black]], Left] &), 
 AxesLabel -> {"", "millions\nof speakers"}]

Out

BarChart[numbers, BaseStyle -> Directive[FontFamily -> font, FontSize -> 13], 
 ChartStyle -> 10, ImageSize -> 560, BarOrigin -> Left, 
 ChartLabels -> Placed[languages, After],
 LabelingFunction -> (Placed[
     Style[#, If[Plus @@ ColorData[10, Last[#2]] < 1.5, White, Black]], 
       {0 + 2/3*Min[numbers]/#, 0.5}] &), 
 AxesLabel -> {"", "millions\nof speakers"}]

Out2

BarChart[numbers, BaseStyle -> Directive[FontFamily -> font, FontSize -> 13], 
 ChartStyle -> 10, ImageSize -> 560, BarOrigin -> Left, 
 ChartLabels -> Placed[languages, After],
 LabelingFunction -> (Placed[
     Row[{Spacer[10], 
       Style[#, If[Plus @@ ColorData[10, Last[#2]] < 1.5, White, Black]]}], Left] &), 
 AxesLabel -> {"", "millions\nof speakers"}]

Out3

$\endgroup$
  • $\begingroup$ Your first solution is exactly what I wanted. Thanks a bunch! $\endgroup$ – stevenvh Sep 3 '16 at 11:51
  • $\begingroup$ Yes. No worries, I do have the right code now, but maybe other readers might expect the same layout as in my question. Though it will be obvious to most readers you can always make a note that changing "Left" to "Center" would create that layout. $\endgroup$ – stevenvh Sep 3 '16 at 12:06
  • $\begingroup$ @stevenvh OK, I added it anyway, because I wanted to see how a different placement changes the impression. $\endgroup$ – Karsten 7. Sep 3 '16 at 12:11
4
$\begingroup$
BarChart[numbers, 
 BaseStyle -> Directive[FontFamily -> font, FontSize -> 13], 
 ChartStyle -> 10, ImageSize -> 560, BarOrigin -> Left, 
 ChartLabels -> Placed[{languages}, {{{1, 0.5}, {-0.2, 0.5}}}, # &], 
 AxesLabel -> {"", "millions\nof speakers"}, 
 LabelingFunction -> (Placed[Panel[#1, FrameMargins -> 0], Center] &)]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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