1
$\begingroup$

I have the following BarChart with labels on the bars and labels at the bottom:

data = {0.55`, 0.19`, 0.21`, 0.94`, 0.79`, 0.07`, 0.82`, 0.12`, 0.15`,
    1.`, 0.15`, 0.12`, 0.76`, 0.8`, 0.069`, 0.71`};
labelsBar = {0.305`, -0.151`, 0.162`, 
   0.62`, -0.363`, -0.054`, -0.323`, 0.12`, 0.024`, -0.509`, -0.027`, 
   0.016`, 0.403`, 0.314`, -0.005`, -0.221`};
labelsBottom = {"ABC1", "ABC2", "ABC3", "ABC4", "ABC5", "ABC6", 
   "ABC7", "ABC8", "ABC9", "ABC10", "ABC11", "ABC12", "ABC13", 
   "ABC14", "ABC15", "ABC16"};
BarChart[data, 
 ChartLabels -> Placed[{labelsBar, labelsBottom}, {Top, Axis}], 
 LabelStyle -> {Black}, 
 ChartStyle -> 
  Table[If[labelsBar[[x]] < 0, Red, Blue], {x, 1, Length@labelsBar}], 
 ImageSize -> 600]

I'm trying to format the labels differently, i.e. the labels on the bottom need to be rotated 90 degrees and should have a different style than the labels on the bars. Moreover, I want the labels on the bars be above them.

I tried using Directive to also rotate the bottom labels, but that gives me an error:

BarChart[data, 
 ChartLabels -> 
  Placed[{labelsBar, labelsBottom}, {Top, 
    Directive[Axis, Rotate[#, Pi/2] &]}], LabelStyle -> {Black}, 
 ChartStyle -> 
  Table[If[labelsBar[[x]] < 0, Red, Blue], {x, 1, Length@labelsBar}], 
 ImageSize -> 600]

Also coloring the labels differently with Labelstyle seems not to be the correct approach, since now both labels are green and none is black:

BarChart[data, 
 ChartLabels -> Placed[{labelsBar, labelsBottom}, {Top, Axis}], 
 LabelStyle -> {Black, Green}, 
 ChartStyle -> 
  Table[If[labelsBar[[x]] < 0, Red, Blue], {x, 1, Length@labelsBar}], 
 ImageSize -> 600]

I must be missing something and hope for some help.

$\endgroup$
1
$\begingroup$

I don't think you get multiple labels per bar using ChartLabels only. In cases where you need more flexibility for styling and labeling of plot elements, you can often use wrappers around the data to specify what you need.

Below, I use two Labeled wrappers and one Style wrapper to specify the necessary information. The labels themselves are themselves wrapped in Style/Rotate to achieve the look you're after.

BarChart[
 MapThread[
  Labeled[
    Labeled[
     Style[#, If[#2 < 0, Red, Blue]],
     Style[#2, Green],
     Top],
    Rotate[Style[#3, Black], 90°],
    Axis
    ] &,
  {data, labelsBar, labelsBottom}
  ],
 ImageSize -> 600
 ]

enter image description here

Note that you could also add one set of labels using ChartLabels, and specify the bar colors using ChartStyle (as you did in the question). I just wanted to show how it can be done using just wrappers, to give you a better idea.

$\endgroup$
  • $\begingroup$ Thanks Lukas! Not sure if this is an easy or complicated solution, but would not have figured that out by myself :). $\endgroup$ – holistic Apr 25 at 9:39

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.