11
$\begingroup$

Bug introduced in 7.0 or earlier and fixed in 10.0.0


When BarChart layout is "Stacked", setting BarOrigin -> Top makes the y-axis tick marks negative. BarChart documentation shows an example - but not "Stacked" - with correct positive y-axis tick marks.

For example,

data = Table[RandomInteger[{1, 10}], {3}, {4}];

BarChart[MapThread[Labeled[#1, #2, Above] &, {data, {1, 2, 3}}], 
 ChartLayout -> "Stacked",
 BarOrigin -> Top, 
 ChartStyle -> 
  Reverse@{Lighter@Blue, Blue, Darker@Blue, Darker@Darker@Blue}, 
 ChartLabels -> {{"a", "b", "c"}, None}, 
 Frame -> {False, True, False, False}, 
 FrameLabel -> {"x axis", "y axis"},
 FrameTicks -> {None, Automatic}, 
 BaseStyle -> {FontFamily -> "Helvetica", FontSize -> 14}]

Gives:

enter image description here

Is there an option to control this behavior without explicitly setting FrameTicks -> {None, Table[{-i, i}, {i, 0, 25, 5}]}, which works but is a hack.

$\endgroup$
2
  • $\begingroup$ The ticks are positive if without the Frame -> {False, True, False, False} option. I guess this setting overrides the internal procedure of BarChart? $\endgroup$
    – Silvia
    Feb 25, 2013 at 19:17
  • $\begingroup$ I would say this is a bug and should probably be reported. $\endgroup$
    – s0rce
    Feb 25, 2013 at 19:59

3 Answers 3

9
$\begingroup$

By looking at the InputForm of the graphics in original post, I found a function Charting`FindTicks. It looks like what's needed.

Change FrameTicks -> {None, Automatic} to FrameTicks -> {None, Charting`FindTicks[{-1, 0}, {1, 0}]}:

Mathematica graphics

$\endgroup$
6
  • $\begingroup$ Where did the x-axis frame label go? I can't seem to find it even if I adjust the PlotRangePadding $\endgroup$
    – s0rce
    Feb 25, 2013 at 20:00
  • 1
    $\begingroup$ @s0rce Because OP sets Frame -> {False, True, False, False}. Try Frame -> {False, True, True, False}, FrameLabel -> {{"y axis", None}, {None, "x axis"}} etc. $\endgroup$
    – Silvia
    Feb 25, 2013 at 20:05
  • $\begingroup$ +1 looks like you can also rescale ticks that way ChartingFindTicks[{1, 0}, {0.5, 0}]` $\endgroup$ Feb 25, 2013 at 22:59
  • $\begingroup$ @MikeHoneychurch It seems charts functions use a lot of special methods internally. Have seen many of them when tracing BarChart. $\endgroup$
    – Silvia
    Feb 26, 2013 at 5:16
  • $\begingroup$ Yes I've used Trace on Barcharts before but the output was so large I didn't bother trying to figure it out! $\endgroup$ Feb 26, 2013 at 5:21
5
$\begingroup$

One possible, although probably not ideal solution is to remove the Frame. Since you are not using most of the Frame anyways this mostly changes where the Labels are put.

data = Table[RandomInteger[{1, 10}], {3}, {4}];

BarChart[MapThread[Labeled[#1, #2, Above] &, {data, {1, 2, 3}}], 
 ChartLayout -> "Stacked", BarOrigin -> Top, 
 ChartStyle -> 
  Reverse@{Lighter@Blue, Blue, Darker@Blue, Darker@Darker@Blue}, 
 ChartLabels -> {{"a", "b", "c"}, None}, Frame -> False, 
 AxesLabel -> {"x axis", "y axis"}, FrameTicks -> {None, Automatic}, 
 BaseStyle -> {FontFamily -> "Helvetica", FontSize -> 14}]

Mathematica graphic

$\endgroup$
5
$\begingroup$

This seems like a bug in BarChart in that the non-negative ticks are not applied to the Frame.
One can fix this by applying the frame afterward and porting the Ticks spec:

BarChart[MapThread[Labeled[#1, #2, Above] &, {data, {1, 2, 3}}], BarOrigin -> Top, 
  ChartLayout -> "Stacked", 
  ChartStyle -> Reverse@{Lighter@Blue, Blue, Darker@Blue, Darker@Darker@Blue}, 
  ChartLabels -> {{"a", "b", "c"}, None}, FrameLabel -> {"x axis", "y axis"}];

Show[%, Frame -> {0, 1, 0, 0}, FrameTicks -> Options[%, Ticks][[1, 2]]]

Mathematica graphics

Examination of the content of that option reveals the Charting`FindTicks function that Silvia posted, which is a more direct way in this case. This answer however may illustrate a way of approaching other such problems which appear from time to time.

$\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.