Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

The Stata Journal

Editor Editor
H. Joseph Newton Nicholas J. Cox
Department of Statistics Department of Geography
Texas A&M University Durham University
College Station, Texas 77843 South Road
979-845-8817; fax 979-845-6077 Durham DH1 3LE UK
jnewton@stata-journal.com n.j.cox@stata-journal.com

Associate Editors
Christopher F. Baum Peter A. Lachenbruch
Boston College Oregon State University
Nathaniel Beck Jens Lauritsen
New York University Odense University Hospital
Rino Bellocco Stanley Lemeshow
Karolinska Institutet, Sweden, and Ohio State University
University of Milano-Bicocca, Italy
Maarten L. Buis J. Scott Long
Tübingen University, Germany Indiana University
A. Colin Cameron Roger Newson
University of California–Davis Imperial College, London
Mario A. Cleves Austin Nichols
Univ. of Arkansas for Medical Sciences Urban Institute, Washington DC
William D. Dupont Marcello Pagano
Vanderbilt University Harvard School of Public Health
David Epstein Sophia Rabe-Hesketh
Columbia University University of California–Berkeley
Allan Gregory J. Patrick Royston
Queen’s University MRC Clinical Trials Unit, London
James Hardin Philip Ryan
University of South Carolina University of Adelaide
Ben Jann Mark E. Schaffer
University of Bern, Switzerland Heriot-Watt University, Edinburgh
Stephen Jenkins Jeroen Weesie
University of Essex Utrecht University
Ulrich Kohler Nicholas J. G. Winter
WZB, Berlin University of Virginia
Frauke Kreuter Jeffrey Wooldridge
University of Maryland–College Park Michigan State University

Stata Press Editorial Manager Lisa Gilmore


Stata Press Copy Editors Deirdre Patterson and Erin Roberson
The Stata Journal publishes reviewed papers together with shorter notes or comments,
regular columns, book reviews, and other material of interest to Stata users. Examples
of the types of papers include 1) expository papers that link the use of Stata commands
or programs to associated principles, such as those that will serve as tutorials for users
first encountering a new field of statistics or a major new technique; 2) papers that go
“beyond the Stata manual” in explaining key features or uses of Stata that are of interest
to intermediate or advanced users of Stata; 3) papers that discuss new commands or
Stata programs of interest either to a wide spectrum of users (e.g., in data management
or graphics) or to some large segment of Stata users (e.g., in survey statistics, survival
analysis, panel analysis, or limited dependent variable modeling); 4) papers analyzing
the statistical properties of new or existing estimators and tests in Stata; 5) papers
that could be of interest or usefulness to researchers, especially in fields that are of
practical importance but are not often included in texts or other journals, such as the
use of Stata in managing datasets, especially large datasets, with advice from hard-won
experience; and 6) papers of interest to those who teach, including Stata with topics
such as extended examples of techniques and interpretation of results, simulations of
statistical concepts, and overviews of subject areas.
For more information on the Stata Journal, including information for authors, see the
webpage
http://www.stata-journal.com

The Stata Journal is indexed and abstracted in the following:


• CompuMath Citation Index R

• Current Contents/Social and Behavioral Sciences


R

• RePEc: Research Papers in Economics


• Science Citation Index Expanded (also known as SciSearch
R
)
TM
• Scopus
• Social Sciences Citation Index
R

Copyright Statement: The Stata Journal and the contents of the supporting files (programs, datasets, and
c by StataCorp LP. The contents of the supporting files (programs, datasets, and
help files) are copyright
help files) may be copied or reproduced by any means whatsoever, in whole or in part, as long as any copy
or reproduction includes attribution to both (1) the author and (2) the Stata Journal.
The articles appearing in the Stata Journal may be copied or reproduced as printed copies, in whole or in part,
as long as any copy or reproduction includes attribution to both (1) the author and (2) the Stata Journal.
Written permission must be obtained from StataCorp if you wish to make electronic copies of the insertions.
This precludes placing electronic copies of the Stata Journal, in whole or in part, on publicly accessible websites,
fileservers, or other locations where the copy may be accessed by anyone other than the subscriber.
Users of any of the software, ideas, data, or other materials published in the Stata Journal or the supporting
files understand that such use is made without warranty of any kind, by either the Stata Journal, the author,
or StataCorp. In particular, there is no warranty of fitness of purpose or merchantability, nor for special,
incidental, or consequential damages such as loss of profits. The purpose of the Stata Journal is to promote
free communication among Stata users.
The Stata Journal (ISSN 1536-867X) is a publication of Stata Press. Stata, Mata, NetCourse, and Stata Press
are registered trademarks of StataCorp LP.
The Stata Journal (2010)
10, Number 4, pp. 689–690

Stata tip 93: Handling multiple y axes on twoway graphs


Vince Wiggins
StataCorp
College Station, TX
vwiggins@stata.com

Sometimes users find it difficult to handle multiple y axes on their twoway graphs.
The main issue is controlling the side of the graph—left or right—where each axis is
placed.
Here is a contrived example that exhibits the issue:
. sysuse auto
(1978 Automobile Data)
. collapse (mean) mpg trunk, by(length foreign)
. twoway bar mpg length, yaxis(2) ||
line trunk length, yaxis(1)
35

25
30

20
(mean) trunk
(mean) mpg
25

15
20

10
15
10

140 160 180 200 220 240


Length (in.)

(mean) mpg (mean) trunk

We might want 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. We could achieve what we want by reversing the order of the two plots,
but the bars then occlude the lines, and who wants 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. How authoritarian! Consider
twoway’s problem: when it sees yaxis(2), it cannot be sure that it will ever see a
yaxis(1). Moreover, twoway will let you create more than two y axes, and in that case
it just stacks them up on the left of the graph like cordwood.

c 2010 StataCorp LP
gr0047
690 Stata tip 93

Do not worry. Although we may not like twoway’s rules, we can alter them. If we
want any axes to appear in a different position, we just tell twoway to move them to
the alternate (other) side of the graph using the yscale(alt) option. In this example,
if we do not like the position of either y axis, we will need to tell each 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))
25

35
30
20
(mean) trunk

(mean) mpg
25
15

20
10

15
10
5

140 160 180 200 220 240


Length (in.)

(mean) mpg (mean) trunk

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 where axis(2) appears, we had to be explicit about the axis number and type
yscale(alt axis(2)).
If your axis is not where you want it, tell it to alter itself.1

1 Acknowledgment
I would like to thank Stata Journal editor Nicholas Cox for the initial adaptation of
this tip from a Statalist posting, though Nick bears no responsibility for any remaining
errors or puns.

1. The Stata Journal editors, against much stiff opposition, declare this to be the worst pun so far in
the history of the Stata Journal.

You might also like