Tell me more ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

In a stacked BarChart or Histogram the bars are stacked from bottom to top, while the legend entries are listed from top to bottom. Is there a way to make the legend entries go the other way round to improve the visual correspondence between the chart and the legend?

E.g.

{bottom,middle,top}=RandomReal[NormalDistribution[0,1],{3,200}];
Histogram[{bottom,middle,top},10,ChartLayout->"Stacked",ChartLegends->{"Bottom","Middle","Top"}]

histogram

share|improve this question
related stackoverflow.com/q/7786778/353410 – belisarius Apr 20 '12 at 21:57
also related? – TomD Apr 21 '12 at 7:23
This question might also be of interest – TomD Apr 21 '12 at 8:06

4 Answers

up vote 11 down vote accepted
Histogram[{bottom, middle, top}, 10, 
          ChartLayout -> "Stacked", 
          ChartLegends -> {"Bottom", "Middle", "Top"}] /. 
Column[l_List] :> Column[Reverse@l]

enter image description here

share|improve this answer
Slick answer! +1 – Eli Lansey Apr 20 '12 at 22:04
I just checked out that linked question. That's also a really slick answer! – Eli Lansey Apr 20 '12 at 22:10
This is great, thanks. It seems so obvious now! – Simon Woods Apr 21 '12 at 11:08
1  
@SimonWoods It is not obvious at all if you don't run Histogram[...]//FullForm first :) – belisarius Apr 21 '12 at 15:19

Using belisarius' method, if you want to reverse the order for all vertical legends you could use Legending`LegendContainer like this:

{bottom, middle, top} = RandomReal[NormalDistribution[0, 1], {3, 200}];

SetOptions[Legending`GridLegend, 
  Legending`LegendContainer -> (Framed[# /. Column[l_List] :> Column[Reverse@l]] &)
];

Histogram[{bottom, middle, top}, 10,
  ChartLayout -> "Stacked",
  ChartLegends -> {"Bottom", "Middle", "Top"}
]

Mathematica graphics

share|improve this answer
Legending sounds like a pejorative adjective for the works of people like JK Rowling – belisarius Apr 21 '12 at 2:14
+1 BTW (and n more charsssss) – belisarius Apr 21 '12 at 2:15
1  
@belisarius you always find something to make me laugh. :D – Mr.Wizard Apr 21 '12 at 2:15

belisarius beat me to the Column replacement rule method. An alternative method would be to use ShowLegend to construct the legend manually, taking the code for the layout from the FullForm of your original graphic. The ridiculously complex lokking color specifications happen to be the Mathematica defaults for bar charts.

ShowLegend[ Histogram[{bottom, middle, top}, 10,  ChartLayout -> "Stacked"], 
 {{{Graphics[{GrayLevel[0.9], 
      Directive[GrayLevel[0.85], EdgeForm[Opacity[0.7]], Opacity[0.5],
        RGBColor[0.6, 0.5470136627990908, 0.24]], 
      Rectangle[{0, 0}, {1, 1}]}, {ImageSize -> 10}], 
    "Top"}, {Graphics[{GrayLevel[0.9], 
      Directive[GrayLevel[0.85], EdgeForm[Opacity[0.7]], Opacity[0.5],
        RGBColor[0.6, 0.24, 0.4428931686004542]], 
      Rectangle[{0, 0}, {1, 1}]}, {ImageSize -> 10}], 
    "Middle"}, {Graphics[{GrayLevel[0.9], 
      Directive[GrayLevel[0.85], EdgeForm[Opacity[0.7]], Opacity[0.5],
        RGBColor[0.2472, 0.24, 0.6]], 
      Rectangle[{0, 0}, {1, 1}]}, {ImageSize -> 10}], "Bottom"}}, 
  LegendPosition -> {0.25, 0.2}, LegendBackground -> White, 
  LegendBorder -> None, LegendShadow -> None, 
  LegendSize -> {0.6, 0.35}, LegendBorder -> None, 
  LegendTextSpace -> 3}]

enter image description here

share|improve this answer

This solution will no longer work in Mathematica 9 because they now use SwatchLegend in FullForm. Instead, apply this:

Histogram[{bottom, middle, top}, 10, ChartLayout -> "Stacked", 
ChartLegends -> {"Bottom", "Middle", "Top"}] /. 
SwatchLegend[l1_List, l2_List, r1_Rule, r2_Rule] :> 
SwatchLegend[Reverse[l1, 1], Reverse[l2, 1], r1, r2]

The integer parameter in Reverse[l1,1] and Reverse[l2,1] might have to be tweaked; in some cases I had to reverse within the second level instead of the first. //FullForm should give you enough hints.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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