Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Suppose I have some data for a bar chart:

testdata = Map[{1, #} &, RandomReal[{-0.5, 4}, {3, 2}], {2}]
(* {{{1, 3.30023}, {1, 3.814}}, {{1, -0.353959}, {1, -0.364447}}, {{1, 
     1.82514}, {1, 2.56824}}} *)

RectangleChart[testdata, BarOrigin -> Left, Frame -> True, 
  ChartStyle -> {Red, Blue}, BarSpacing -> {-0.2, 0.6}]

enter image description here

But what I really want is for the shorter, red bars to overlap on top of the blue bars, not underneath.

RectangleChart[Reverse /@ testdata, BarOrigin -> Left, Frame -> True, 
  ChartStyle -> {Blue, Red}, BarSpacing -> {-0.2, 0.6}]

enter image description here

That works, but the red bars only overlap if they are above the blue bars. If I had set BarOrigin->Bottom, the red bars would then be on the right.

RectangleChart[Reverse /@ testdata, Frame -> True, 
  ChartStyle -> {Blue, Red}, BarSpacing -> {-0.2, 0.6}]

enter image description here

Conceptually this is because later data prints after earlier data, so groups of bars on the right or at the top are "later" and print on top of the "earlier" bars on the left or below.

This makes sense to me, but not to the person I am drawing the graph for. Is there any way to reverse the bar ordering such that the bars at the bottom of the group overlap on top of the upper bars when one has negative BarSpacing?

share|improve this question
2  
Does this work MapAt[Rotate[#, -90 Degree] &, RectangleChart[testdata, Frame -> True, ChartStyle -> {Blue, Red}, BarSpacing -> {-0.2, 0.6}, Axes -> False, BarOrigin -> Bottom], {1}] (with some extra work on tick labels)? –  kguler Jun 2 at 5:27
    
@kguler - It would need to be RectangleChart[Reverse/@testdata.... That is inspired, but how I'd then get that into our crazy custom charting functions is a challenge. –  Verbeia Jun 2 at 7:07

1 Answer 1

up vote 4 down vote accepted

I like kugler approach, this is another one. Not sure if it is going to work for general case but it should.

RectangleChart[testdata, BarOrigin -> Left, Frame -> True, ChartStyle -> {Red, Blue}, 
                         BarSpacing -> {-0.2, 0.6}
              ] /. x : {{__, _Tooltip} ..} :> Reverse@x

enter image description here

Of course for PerformanceGoal:>"Speed" there are no Tooltips but it is easy to handle too.

RectangleChart[testdata, BarOrigin -> Left, Frame -> True, ChartStyle -> {Red, Blue}, 
                         BarSpacing -> {-0.2, 0.6}, PerformanceGoal :> "Speed"
              ] /. x : {{__, _Rectangle} ..} :> Reverse@x
share|improve this answer
    
Ah, clever, kubanator! +1 –  rasher Jun 2 at 7:39
    
@rasher Hehe :) thanks. –  Kuba Jun 2 at 7:41
    
This is excellent! I'm just testing if it works with our crazy custom functions. –  Verbeia Jun 2 at 7:52
    
@Verbeia I'm glad I could help :) –  Kuba Jun 2 at 17:31

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.