6
$\begingroup$

How can I make better labels for the following simple BarChart?

onset1 = {8870, 73730};
peak1 = {1.3 10^8, 2 10^8};
onset2 = {2425, 5695};
peak2 = {1123, 2326};
onset3 = {88, 141};
BarChart[{onset3, onset2, peak2, onset1, peak1}, 
 ScalingFunctions -> "Log", 
 ChartLabels -> {Placed[{"Very long label", "Very long label", 
     "Very long label", "Very long label", "Very long label"}, {0.5, 
     1.25}], Placed[{"data1", "data2"}, Below, 
    Rotate[#, 45 Degree] &]}, PlotLabel -> "plot", Frame -> True]

The result is:

enter image description here

Labels are too close or too far from the bars and also they are long and partly superimpose to the bars themselves. How can I improve the layout of the labels?

$\endgroup$
4
  • 2
    $\begingroup$ You can use "Very\nlong\nlabel". $\endgroup$
    – JimB
    Oct 28, 2021 at 14:53
  • $\begingroup$ @JimB I tried that but the labels superimpose to the smaller bars and are outside the plot for the larger ones $\endgroup$
    – mattiav27
    Oct 28, 2021 at 14:54
  • 2
    $\begingroup$ Use extra \n to get the desired position: "\n\nVery\nlong\nlabel" for the high bars and "Very\nlong\nlabel\n\n" for the very low bars. $\endgroup$
    – JimB
    Oct 28, 2021 at 14:57
  • $\begingroup$ @JimB I didn't think about that thanks! $\endgroup$
    – mattiav27
    Oct 28, 2021 at 14:59

2 Answers 2

6
$\begingroup$

I think instead of scaling you can use Above like this Placed[{"Very long label", "Very long label", "Very long label","Very long label", "Very long label"}, Above]

enter image description here

or with a different style like this

Callout[{"Very long label", "Very long label", "Very long label", 
  "Very long label", "Very long label"}, Automatic]

  

enter image description here

$\endgroup$
3
$\begingroup$

A possible solution:

peak1 = {1.3 10^8, 2 10^9};
onset2 = {2425, 5695};
peak2 = {1123, 2326};
onset3 = {88, 141};
labels = {"Very long label", "Very long label", "Very long label", 
   "Very long label", "Very long label"};
nlabels = 
  Framed[#, Background -> Blend[{Yellow, Lighter@Gray}]] &@
     Column@StringSplit[#] & /@ labels;
BarChart[{onset3, onset2, peak2, onset1, peak1}, 
 ScalingFunctions -> "Log",
 PlotRangePadding -> {{0, 0}, {0, 7}},
 ChartLabels -> {Placed[#, Above] &@nlabels, 
   Placed[{"data1", "data2"}, Below, Rotate[#, 45 Degree] &]}, 
 PlotLabel -> "plot", Frame -> True]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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