5
$\begingroup$

This question is somewhat related to this one.

Let's say this is the BarChart i want to make:

BarChart[Range[5], ChartStyle -> "Rainbow", BarOrigin -> Left,
 ChartLegends -> Map[ToString, Range[5]]]

BarChart reflected

The BarChart runs from the bottom and the legend starts at the top. The related question (linked above) gives a solution to reverse the legend, but i'd rather have the axis (and thus the purple bar) on top. It should look like this (made with ImageReflect[]):

BarChart reflected

I tried AxesOrigin, but that doesn't work. My question seems pretty trivial, but i haven´t found a solution for it yet.

Question: is there a way to specify the origin of a BarChart?

$\endgroup$
4
  • $\begingroup$ @PlatoManiac: why did you delete your solution? I was about to accept it... $\endgroup$ Nov 16, 2012 at 13:27
  • $\begingroup$ @A.Goossens please consider waiting longer before Accepting an answer. I recommend 24 hours to allow people in all time zones a chance to answer. You'll almost always get better answer this way (versus the quick Accept). $\endgroup$
    – Mr.Wizard
    Nov 16, 2012 at 13:42
  • $\begingroup$ @Mr.Wizard: i will wait a bit longer the next time. On the other hand, encouraging quick answering by accepting a good one isn't bad either. ;) $\endgroup$ Nov 16, 2012 at 13:44
  • $\begingroup$ I think people already answer as quickly as they can. I urge you to reconsider your practice. At least be willing to change your Accept. $\endgroup$
    – Mr.Wizard
    Nov 16, 2012 at 13:48

2 Answers 2

4
$\begingroup$

Here are a couple of options:

data = Reverse @ Range[5];

BarChart[data,
  ChartStyle -> "Rainbow",
  BarOrigin -> Left, 
  Frame -> {False, False, True, False}, 
  FrameTicks -> {{None, None}, {None, All}},
  ChartLegends -> data
] /. x_Column :> Reverse /@ x

Mathematica graphics

Manual coloring for reverse gradient:

colors = ColorData["Rainbow"] /@ 
  Reverse @ FindDivisions[{0, 1}, Length@data - 1];

BarChart[data,
  ChartStyle -> colors,
  BarOrigin -> Left,
  Frame -> {False, False, True, False},
  FrameTicks -> {{None, None}, {None, All}},
  ChartLegends -> data
] /. x_Column :> Reverse /@ x

Mathematica graphics

$\endgroup$
4
$\begingroup$

Minor variation on @Mr Wizards answer:

  1. using {{l,r},{b,t}} syntax for Frame just to be consistent with FrameTicks

  2. using ColorFunction to alter the colour ordering

gives

BarChart[Reverse@Range[5],
 BarOrigin -> Left,
 ColorFunction -> ColorData["Rainbow"],
 Frame -> {{False, False}, {False, True}},
 FrameTicks -> {{None, None}, {None, All}}]

enter image description here

BarChart[Reverse@Range[5],
 BarOrigin -> Left,
 ColorFunction -> ColorData[{"Rainbow", "Reverse"}],
 Frame -> {{False, False}, {False, True}},
 FrameTicks -> {{None, None}, {None, All}}]

enter image description here

Note that you could have also used ColorFunction -> Function[{x},ColorData["Rainbow"][1-x]].

Now for the legends. Unfortunately the legends are based on ChartStyle so when you control the bar colours using ColorFunction the ChartLegends option doesn't work (in so far as no color swatches are produced).

enter image description here

This does not present a problem for this case -- which seems to be the specific example you are looking for:

BarChart[Reverse@Range[5],
 BarOrigin -> Left,
 ChartStyle -> "Rainbow",
 ColorFunction -> ColorData["Rainbow"],
 ChartLegends -> Map[ToString, Range[5]],
 Frame -> {{False, False}, {False, True}},
 FrameTicks -> {{None, None}, {None, All}}]

enter image description here

but for other variations this simple code doesn't produce ideal outcomes:

BarChart[Reverse@Range[5],
 BarOrigin -> Left,
 ChartStyle -> "Rainbow",
 ColorFunction -> ColorData[{"Rainbow", "Reverse"}],
 ChartLegends -> Map[ToString, Reverse@Range[5]],
 Frame -> {{False, False}, {False, True}},
 FrameTicks -> {{None, None}, {None, All}}]

enter image description here

It seems clear that the built in legend option should base its colour swatches on the actual colours used to render the bars (i.e. in this case controlled by ColorFunction). That it does not seems like an oversight. Most of us here don't like (understatement) the built in legends and legend options and tend to roll our own.

Edit -- making a simple legend for your bar chart

To reproduce the sort of legend generated with ChartLegends you can use MapIndexed

labels = Range[5];

Framed@Grid[
  MapIndexed[{Graphics[{ColorData[{"Rainbow", "Reverse"}][
        Rescale[First[#2], {1, Length[labels]}]], Rectangle[]}, 
      AspectRatio -> 1, ImageSize -> 15], #1} &, labels],
  Alignment -> {Center, Center}]

enter image description here

For the ColorData gradient to work you need to scale the argument. Within the BarChart ColorFunctionScaling by default is True so this happens automatically. But to get it to work here you have to do the scaling yourself -- which is what Rescale[First[#2], {1, Length[labels]}] is doing. You can add styling to this legend to suit whatever font choice and size you like. If you want a border around the color swatch add EdgeForm in the Graphics list. To position the legend try using Labeled.

Labeled[(*your bar chart*), (*your legend*), {{Right, Center}}]
$\endgroup$
5
  • $\begingroup$ Another "Why didn't I think of that?" moment. :-) +1 $\endgroup$
    – Mr.Wizard
    Nov 17, 2012 at 16:18
  • $\begingroup$ @Mike_Honeychurch: Thanks! Can you recommend an alternative legend package? $\endgroup$ Nov 17, 2012 at 21:31
  • $\begingroup$ I'll add an edit with a simple work around legend but @Jens has posted a nice legend package on here if you do a search you will find it. ...note that doing the x_Column :> Reverse /@ x rule replacement as per @Mr. Wizards answer fixes the legend in the final example. $\endgroup$ Nov 17, 2012 at 21:34
  • $\begingroup$ I was referring to your comment that most mma users tend to use other way to make a legend. I already fixed mine using your and @Mr.Wizard's answers! $\endgroup$ Nov 17, 2012 at 22:15
  • $\begingroup$ @A.Goossens have a look at Jens package ..also the legend code above. If you're like me you'll find that you always want to tweek legends so it is worth getting familiar with how to make your own. $\endgroup$ Nov 17, 2012 at 22:29

Your Answer

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

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