Abstract

1 by() option of graph or graph combine for paneled figures?
Stata users produce graphs (often called figures) for their presentations and publications. Many such figures are composites with two or more separate panels, so that figure 1 is a composite of figure 1a, 1b, 1c, and 1d. There are two main ways to create such composites in Stata: using a
A
to get separate scatterplots of
Using
The aim of this tip is to push a little at this distinction, as less clear-cut than it seems. Sometimes, a graph will be improved by seeking a different data layout in which a
2 Example 1: Confidence intervals for means of different variables
A standard plot in many fields compares sample means of a variable for two or more conditions within a framework of confidence intervals or other indications of uncertainty about those means. The word confidence here is, as every good text or course should explain, a term of art, rather than an expression of the conviction or intensity of belief a researcher is entitled or expected to hold about variability. A personal suspicion that diffidence intervals would be a better term is far from original. Almost a century ago, Fisher (1925, 10) wrote of “our mental confidence or diffidence” in making inferences, although he was not writing about confidence intervals avant la lettre; he was flagging the concept of likelihood.
A natural extension when there are several variables of interest is to show various such plots side by side.
An excellent way to get such plots is through
Let us suppose that—using the auto data again, and now for real—we want to show, side by side, plots with means and confidence intervals for price, miles per gallon, weight, and length for those domestic and foreign cars.
The plan here is to just loop over four variables, each time reading in the data, producing a reduced dataset and drawing and saving each graph for later combination. For an introduction to loops, and their use of local macros, see Cox (2020).
The big idea is to loop over variables, producing a graph for each variable to be put together by the final
Know that
The trickiest detail here is the use of nested macro references, specifically
Some of the details here are specific to the example. It seems to me good practice to show the units of price, here U.S. dollars, especially for an international readership. There are two distinct values of
The other details are more general. Each graph shows what it is about in a subtitle. For that subtitle, my preference is to use a variable label. All the variables here do in fact have variable labels, but the code also shows technique for using the variable name, rather than the variable label, when the latter is not defined.
Why did I use the
It is likely that you would prefer a graph scheme different from
It would have been entirely possible to write that code directly using variable names. Let’s give the code as rewritten that way.
Some of the choices here are just conventional, whether widespread (many people do it) or personal (it could just be me). Thus, names like
Some of that may have been new to you, or else you skimmed and skipped familiar constructs. To focus now on the main theme of this tip, let us see the result of

Means and confidence intervals for price, miles per gallon, weight, and length for domestic and foreign cars from the auto dataset. So far, so good, but the y axes stand out as using different spacing.
That does look quite good to me, but it could still be better. The different variables come with different units of measurement and—more crucially—different magnitudes, and the y axes are not exactly aligned. That is the niggling detail that people often spot quickly. Note also that specifying the
To solve this problem of unaligned axes, we turn instead to combining the reduced datasets and then drawing a graph just once using the
The results of
We need to store information on each variable for later use as we loop through them. We want the variable labels of the four variables concerned (or if they do not exist, the variable names) to become value labels for the grouping variable we are going to use. Naturally, for that to be possible, the grouping variable, here called
For this version of the problem, assigning integers 1 to 4 in the order in which we want the panels is definitely a good idea. Otherwise, if the variable names
Figure 2 is the result. Isn’t it better? What it shows is perhaps banal, but here we go: foreign cars typically are more expensive but have better mileage than domestic cars, and they are lighter and shorter on average too.

Means and confidence intervals for price, miles per gallon, weight, and length for domestic and foreign cars. It is not obvious from the graph, but a
3 Example 2: Slicing cyclic time series
Effective graphical representation of intensely cyclical time series can be a challenge, especially if they are long. Many time series from several fields, including economics, epidemiology, and environmental science, are strongly seasonal. Cycles in those and other fields can also be identified with lengths both shorter and longer than a year. Cycles vary from fairly predictable to highly unpredictable, but the same graphical challenge is common to many: do justice to the structure of cycles, which may itself be changing over time, and also reveal any trends or other long-term changes. Stata-based discussion in Cox (2006, 2009) covers several of the graphical ideas that have been floated.
Sunspot numbers are a popular sandbox for enthusiasts in statistical graphics (for example, Cleveland [1993, 1994]; Wilkinson [2005]). Here I use annual averages downloaded on 3 June 2020 from http://www.sidc.be/silso/home with grateful acknowledgment to WDC-SILSO, Royal Observatory of Belgium, Brussels. The data are available with the files for this tip.
A standard line plot is not crazy but not especially helpful either (figure 3).

A common or garden line chart for mean sunspot numbers, 1700–2019. Strongly cyclical behavior is evident, but the fine structure of cycles is not especially clear from the roller-coaster display.
Common advice is to change the aspect ratio (ratio of graph height to graph width), which often can work well enough (Cox 2004). Changing the aspect ratio is sometimes discussed as if novel, but it was evidently familiar to Fisher (1925, 31) and doubtless earlier yet. Here the effect is a little disappointing. You may have found low aspect ratios used in long, concertina like graphs inserted in books, but changes in printing have made those much less common.

Changing the aspect ratio does not help much here
A twist on the same idea is to slice the series and use several panels for the different slices. Cleveland (1993, 1994) called these “cut-and-stack plots”. Cox (2006) reported a Stata command

Slicing a long cyclical time-series plot makes the structure of the data clearer. Note the common asymmetry of cycles in which rising limbs are often steeper than falling limbs. A
We create a slice variable that runs from 1 to 4, but naturally you should use whatever different value of 4 makes sense for your problem. (That recycles a mild joke associated with the great probabilist, William Feller, 1906–1970.)
To spell it out, the presumption is that the time series is already sorted on a time variable. The inbuilt observation number
That variable is just a means to an end, and so we suppress all evidence that it exists at all, blanking out both the
The payoff is not just in terms of a clearer graph. It becomes evident that cycles are often asymmetric, with steeper rising limbs than falling limbs. Explaining why is a problem for researchers in solar physics.
4 The art that conceals art
A duty to be clear is a higher virtue than using clever tricks, but clever tricks are always worth knowing about. The trick here implies more work by the user, and the payoff has to be judged case by case. It can be explained as art concealing art, in using a
Further uses of the same device can be found in the community-contributed commands
Supplemental Material
Supplemental Material, gr0085 - Stata tip 139: The by() option of graph can work better than graph combine
Supplemental Material, gr0085 for Stata tip 139: The by() option of graph can work better than graph combine by Nicholas J. Cox in The Stata Journal
