4
$\begingroup$

I want to create a BarChart with two different y-axis, one on the left and another one on the right. For that, I did two different BarChart I tried to overlay them

a= {{0.355556, 0.555556, 0}, {23.3115, 19.7992, 0}, {0.395476, 0.482037, 0}, {3.34386, 2.48343, 0}, {0.0400976, 0.0698065, 0}, {0.911938, 0.840873, 0}, {0.182678, 0.132822, 0}, {0.184268, 0.379897, 0}, {26.9781, 35.3149, 0}, {0.302316, 0.337643, 0}, {2.78961, 2.52604, 0}}

b={{0, 0, 0.36}, {0, 0, -0.177399}, {0, 0, 0.179574}, {0, 0, -0.346465}, {0, 0, 0.425589}, {0, 0, -0.0845136}, {0, 0, -0.375361}, {0, 0, 0.514952}, {0, 0, 0.23607}, {0, 0, 0.104629}, {0, 0, -0.10434}}

Plot1 = BarChart[a, BarSpacing -> {0, 1}, BaseStyle -> $TextStyle, ImageMargins -> 5, ImageSize -> 500, ChartStyle -> {Darker[Red], Orange}, PlotRange -> {Automatic, {-1, 35}}, Frame -> Left, FrameTicks -> {{Automatic, Automatic}, {None, Automatic}}, Background -> None]

Plot2 = BarChart[b, BarSpacing -> {0, 1}, Frame -> Right, FrameStyle -> Darker[Blue], ChartStyle -> {Darker[Blue]}, FrameTicks -> All, PlotRange -> {Automatic, {-0.5, 2.5}}, BaseStyle -> $TextStyle, ImageMargins -> 5, ImageSize -> 500]
Overlay[{Plot1, Plot2}, Alignment -> Bottom, BaselinePosition -> Bottom]

However, I am not able to properly align them. How can I align them at y = 0?

$\endgroup$
3
$\begingroup$

Edit 2 : Improved Version

Firstly I'm renaming your data set b as b1.

Create a new data c such that

c = a + (10*b1);

I'm scaling the data set b1 according to the axis you wanted.

Create a barchart for c

 Plot3 = BarChart[c, BarSpacing -> {0, 1}, Frame -> Left, 
  FrameTicks -> All, BarOrigin -> Bottom, 
  ChartStyle -> {Darker[Red], Orange, Darker[Blue]}, 
  PlotRange -> {Automatic, {0, 35}}, BaseStyle -> $TextStyle, 
  ImageMargins -> 5, ImageSize -> 500];

Create the right axis separately

axis = BarChart[{}, FrameTicks -> All, Frame -> Right, 
    Axes -> {False, False}, ImageSize -> 500, 
   PlotRange -> {{0, 0}, {-0.5, 2.5}}, FrameStyle -> Darker[Blue]]

Now we can Overlay Plot3 and axes as before:

p1 = Overlay[{Plot3, 
Graphics[{}, AspectRatio -> (h + b + t)/(w + l + r), 
ImageSize -> {w + l + r, h + b + t}, ImagePadding -> 10, 
Epilog -> {Dynamic[Locator[Dynamic[pt], axis]]}]}, All, 2]

Finally we get after

 p2 = p1 /. Locator[x_, y_] :> Inset[y, x] /. Dynamic :> Identity

enter image description here


Edit 1

First run the below for initializing certain parameters;

 {w, h} = {500, 300};
 pt = Scaled[{0.5, 0.5}];
 {{l, r}, {b, t}} = {{80, 40}, {60, 10}};

Now you generate your two barchart named Plot1 and Plot2. Then we will overlay the second on the first dynamically so that you can move the second plot freely on top of the first one.

  p1 = Overlay[{Plot1, 
  Graphics[{}, AspectRatio -> (h + b + t)/(w + l + r), 
 ImageSize -> {w + l + r, h + b + t}, ImagePadding -> 10,
 Epilog -> {Dynamic[Locator[Dynamic[pt], Plot2]]}]}, All, 2]

After you have placed the second plot where you want it to be, you can then make the shift permanent as follows:

 p2 = p1 /. Locator[x_, y_] :> Inset[y, x] /. Dynamic :> Identity

Is this the final result you wanted:

enter image description here

$\endgroup$
  • $\begingroup$ Thanks for the reply. There is, however, one problem. At the moment, the blue bar is over the red and orange bars and I want the to have the blue bar just side with the red and orange bars {red,orange,blue}, without barspacings, or at least with the same barspacing. Do you know how to have the x-axis of both plots exactly the same size? $\endgroup$ – Sofia Aug 25 '15 at 14:13
  • $\begingroup$ Check the edited answer $\endgroup$ – Hubble07 Aug 25 '15 at 17:04

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.