3
$\begingroup$

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?

$\endgroup$
  • 2
    $\begingroup$ 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)? $\endgroup$ – kglr Jun 2 '14 at 5:27
  • $\begingroup$ @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. $\endgroup$ – Verbeia Jun 2 '14 at 7:07
4
$\begingroup$

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
$\endgroup$
  • $\begingroup$ Ah, clever, kubanator! +1 $\endgroup$ – ciao Jun 2 '14 at 7:39
  • $\begingroup$ @rasher Hehe :) thanks. $\endgroup$ – Kuba Jun 2 '14 at 7:41
  • $\begingroup$ This is excellent! I'm just testing if it works with our crazy custom functions. $\endgroup$ – Verbeia Jun 2 '14 at 7:52
  • $\begingroup$ @Verbeia I'm glad I could help :) $\endgroup$ – Kuba Jun 2 '14 at 17:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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