# Re: st: Graph twoway: Multiple axis and order of plots

 From vwiggins@stata.com (Vince Wiggins, StataCorp) To statalist@hsphsun2.harvard.edu Subject Re: st: Graph twoway: Multiple axis and order of plots Date Fri, 07 Jul 2006 10:05:03 -0500

```Peter Uhrmacher <peter.uhrmacher@gmx.net> wants to overlay a line plot atop a
bar plot using -graph twoway-.  That is easy, but Peter also wants two
separately scaled axes for the plots and he wants the axis for the line plot
on the left.  The two axes are also easy, but getting the axis for the line
plot on the left is trickier.

Why is it tricky?  It is the convergence of two rules:

1) -graph- renders plots in the order that they are specified on
the command line.  (That gives us control of what is on top.)

2) When separate axes are used, the axis for the first plot goes by
default on the left and the axis for the second plot on the right.

It is rule (2) that is giving Peter problems.  He needs the line plot on top
of the bar plot (meaning is must be the second plot), but he also wants the
line plot's axis on the left (meaning it must be the first plot).  So, it
seems Peter can get the the line plot on top, or its axis on the left, but not
both.

Several suggestions have been made using hand-labeled axes that, I believe,
solve Peter's problem.  There is a little-used option that will make it even
easier:  the -alt- suboption of option -yscale()-.  -alt- instructs -graph- to
switch an axis from whatever side of the graph it would normally be drawn on
to the opposite side of the graph.

A mock up of what Peter is trying to do can be drawn using the auto dataset.
We will use an observation index variable for the x-variable.

. sysuse auto
. gen x = _n

Peter's original command will then look something like,

. twoway bar trunk x ||
line gear_ratio x, yaxis(2)

Where, the -yaxis(2)- options specifies that we want the line graph plotted on
its own separate scale and the axis for that plot added to the graph (with the
default position for a second y-axis being the right side of the graph).

We can instruct any axis to "flop" to the opposite side of the graph with the
-yscale(alt)- option.  We will tell both y-axes to do this.

. twoway bar trunk x, yscale(alt) ||
line gear_ratio x, yaxis(2) yscale(alt axis(2))

The -yscale()- option on the line graph includes the suboption -axis(2)- to
specify that we are talking about the second y-axis, the first -yscale()- did
not need to specify -axis(1)- because that is the default target.

I expect no one thought of this solution because the documentation for the
option -alt- is one sided,

"move axis from left to right or from top to bottom"

That is how almost everyone will use the -alt- option.  To be complete, it
should read "move axis to opposite side from where it would normally be".
While more accurate, the latter description is not easy to understand.

<Warning, shameless plug to follow>

Interestingly, we could also have controlled the axis position using a custom
scheme.  If we wrote such a scheme, we could make the default position for
first y-axes be the right-side of the graph with any second y-axes defaulted
to the left.

But, creating schemes is another story.  If you happen to be in London on
September 11 and 12th that story will be told as part of the 2006 UK Stata
Users Group meeting, along with many stories of a more technical and
statistical nature.

-- Vince
vwiggins@stata.com

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```