Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# Re: st: Multiple plots, by() option and reversed yaxis scales/labels

 From vwiggins@stata.com (Vince Wiggins, StataCorp) To statalist@hsphsun2.harvard.edu Subject Re: st: Multiple plots, by() option and reversed yaxis scales/labels Date Thu, 14 Oct 2010 17:21:59 -0500

```David Elliott <dcelliott@gmail.com> is drawing a graph with two
Y axes and is having difficulty controlling which side of the
graph -- left or right -- where each axis is placed.

David gives a nice example, which I am going to further simplify
by removing the by() option.  The by() has nothing to do with
the axis placement that is confounding David.

Here is an example that exhibits the problem,

. sysuse auto
. collapse (mean) mpg trunk, by(length foreign)

. twoway bar    mpg length, yaxis(2) ||
line trunk length, yaxis(1)

David wants yaxis(1) to be on the left of the graph and yaxis(2)
to be on the right of the graph, but -twoway- insists on putting
yaxis(2) on the left and yaxis(1) on the right.  He also notes
that he could achieve this by reversing the order of his two
plots, but the the bars then occlude his lines, and who would
want that.

It might be surprising, but the number assigned to an axis has nothing
to do with its placement on the graph.  -twoway- places the axes in the
order in which it encounters them, with no consideration of their
assigned number.  "What?," you cry.  How authoritarian.  But, consider
-twoway-'s problem, when it sees -yaxis(2)-, it cannot be sure that it
will ever see an -yaxis(1)-.  Moreover, -twoway- will let you create
more than two Y axes, and, in that case just stacks them up on the left
of the graph like cord wood.

Not to worry.  Although you may not like -twoway-'s rules, you can
alter them.  If there are any axes that you do not like the position
of, just tell -twoway- to move them to the alternate (other) side of
the graph using the -yscale(alt)- option.  Because David does not like
the position of either Y axis, we will need to tell both of them to
switch to the other side.

. twoway bar    mpg length, yaxis(2) ||
line trunk length, yaxis(1) yscale(alt) yscale(alt axis(2))

We typed just -yscale(alt)- rather than the more explicit, but still
valid, -yscale(alt axis(1))- because -axis(1)- is the default whenever
we do not specify an axis.  To alter the side of axis(2), we had to be
explicit about the axis number and type -yscale(alt axis(2))-.

These options will also work for the more complicated by() graph that
David wants to draw.

David also noted that a Y axis placed on the right of a by() graph does
not include its axis title.  We will look into this problem.  Until a
fix is made, David can explicitly place that title onto the graph using
the -r1title()- suboption of by().  This is exactly how the title on
the left axis is handled on by() graphs.  Continuing with David's
example, the full command is

. twoway bar    mpg length, yaxis(2) ||
line trunk length, yaxis(1) yscale(alt) yscale(alt axis(2))
by(foreign, r1title("(mean) mpg"))

-- Vince
vwiggins@stata.com

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```