7
$\begingroup$

In the bar chart (with negative values)

BarChart[{{1, 2, 3}, -{4, 5, 6}}, 
 ChartLabels -> {Placed[{"r1", "r2"}, Above], 
   Placed[{"c1", "c2", "c2"}, Below]}]

enter image description here

the bar and group labels can be placed above and below the bars.

How can I place the bar and group labels on the upper and lower frame axes like in the paired bar chart

PairedBarChart[{Range[4], Range[4]/2}, {Range[4], Range[4]/2}, 
 ChartLabels -> {Placed[{"pair1", "pair2"}, Above], 
   Placed[{"g1", "g2"}, "LeftAxis"], 
   Placed[{"a", "b", "c", "d"}, "RightAxis"]}]

enter image description here

where the labels are placed to the left and right axis by using Placed[{"g1", "g2"}, "LeftAxis"] and Placed[{"a", "b", "c", "d"}, "RightAxis"]? Unfortunately, this doesn't work with BarChart and "TopAxis" nor "BottomAxis".

$\endgroup$

3 Answers 3

4
$\begingroup$

Update: In versions 11.3+, using Callout instead of Placed gives the desired result:

BarChart[{{1, 2, 3}, -{4, 5, 6}}, 
 ChartLabels -> {Callout[{"r1", "r2"}, Above], 
   Placed[{"c1", "c2", "c3"}, Axis]}]

enter image description here

Use Callout[{"r1", "r2"}, Above, Appearance -> None] to get

enter image description here

Note: In version 12.1 we get the same output with an error message. You can wrap BarChart with Quiet to suppress the message.

Original answer:

Post-processing to account for BarSpacing settings when placing bar labels on frameticks (as in @bensheperd's answer),

dta = {{1, 2, 3}, -{4, 5, 6}};
bc = BarChart[dta, BarSpacing -> {.3, .6}, ImageSize -> 450,
ChartLabels -> {Placed[{"r1", "r2"}, Axis], Placed[{"c1", "c2", "c3"}, Above]}];
tks = Riffle[GatherBy[ Cases[bc, 
  Text[t_, Offset[{_, _}, {x_, y_}] | {x_, y_}, ___] :> {x, ToString@t}, {0, Infinity}],
  StringTake[Last[#], 1] &],
  {Automatic, Automatic}];
bc2 = BarChart[dta, BarSpacing-> {.3, .6}, ImageSize ->450,Frame-> True, FrameTicks -> tks];
Row[{bc, bc2}]

enter image description here

$\endgroup$
1
  • $\begingroup$ In the above Barchart, Placed[{"c1", "c2", "c3"}, Axis]}] is used to give the same labels for elements under both r1 and r2. How do you give different labels for the group called r2 like {"z1", "z2", "z3"}? $\endgroup$ Commented May 8, 2021 at 16:42
8
$\begingroup$

This first solution is not exactly what you're looking for, but it gets close:

BarChart[{{1, 2, 3}, -{4, 5, 6}}, 
  ChartLabels -> {Placed[{"r1", "r2"}, {.5, 2}], 
  Placed[{"c1", "c2", "c2"}, Axis]}]

enter image description here

To get the correct representation you need to tweak a bit some values:

BarChart[{{1, 2, 3}, -{4, 5, 6}}, 
  ChartLabels -> {Placed[{"r1", "r2"}, {.5, 2}], 
  Placed[{"c1", "c2", "c2"}, {{0, 0}, {-2.2, 11}}]}]

enter image description here

Update

If you also need the axes on top and on bottom of the figure, you might try this approach (similar to the solution provided by benshepherd):

BarChart[{{1, 2, 3}, -{4, 5, 6}}, 
  FrameTicks -> {Transpose[{Range[.7, 6.3, (6.3 - .7)/5], {"c1", "c2", 
  "c3", "c1", "c2", "c3"}}], 
  Automatic, {{.7 + (6.3 - .7)/5, "r1"}, {6.3 - (6.3 - .7)/5, "r2"}},
  Automatic}, Frame -> {{True, False}, {True, True}}]

enter image description here

$\endgroup$
3
  • $\begingroup$ Is there a way to print axes like in the PairedBarChart? $\endgroup$
    – Robinaut
    Commented Mar 7, 2013 at 11:12
  • $\begingroup$ Please, see update. $\endgroup$
    – VLC
    Commented Mar 7, 2013 at 11:27
  • $\begingroup$ Note, that this won't work for BoxWhiskerChart, as it seems to ignore FrameTicks specifications. $\endgroup$ Commented Dec 16, 2014 at 11:25
5
$\begingroup$

No, there doesn't appear to be a valid argument for Placed - Mathematica wants to place your label relative to the bar.

To get labels on the axes, I think you'd have to use FrameTicks:

BarChart[{{1, 2, 3}, -{4, 5, 6}}, 
  FrameTicks -> {Automatic, Automatic, 
   Transpose@{Range@6, {"c1", "c2", "c3", "c1", "c2", "c3"}}, 
   Automatic}, Frame -> True]
$\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.