Stata 15 help for twoway rarea

[G-2] graph twoway rarea -- Range plot with area shading


twoway rarea y1var y2var xvar [if] [in] [, options]

options Description ------------------------------------------------------------------------- vertical vertical area plot; the default horizontal horizontal area plot cmissing(y|n) missing values do not force gaps in area; default is cmissing(y) sort sort by xvar; recommended

area_options change look of shaded areas

axis_choice_options associate plot with alternative axis

twoway_options titles, legends, axes, added lines and text, by, regions, name, aspect ratio, etc. ------------------------------------------------------------------------- All explicit options are unique; see repeated options.


Graphics > Twoway graph (scatter, line, etc.)


A range plot has two y variables, such as high and low daily stock prices or upper and lower 95% confidence limits.

twoway rarea plots range as a shaded area.

Also see [G-2] graph twoway area for area plots filled to the axis.


vertical and horizontal specify whether the high and low y values are to be presented vertically (the default) or horizontally.

In the default vertical case, y1var and y2var record the minimum and maximum (or maximum and minimum) y values to be graphed against each xvar value.

If horizontal is specified, the values recorded in y1var and y2var are plotted in the x direction and xvar is treated as the y value.

cmissing(y|n) specifies whether missing values are to be ignored when drawing the area or if they are to create breaks in the area. The default is cmissing(y), meaning that they are ignored. Consider the following data:

+----------+ | y x | |----------| 1. | 1 1 | 2. | 3 2 | 3. | 5 3 | 4. | . . | 5. | 6 5 | |----------| 6. | 11 8 | +----------+

Say that you graph these data by using twoway rarea y x. Do you want a break in the area between 3 and 5? If so, you type

. twoway rarea y x, cmissing(n)

and two areas will be drawn, one for the observations before the missing values at observation 4 and one for the observations after the missing values.

If you omit the option (or type cmissing(y)), the data are treated as if they contained

+----------+ | y x | |----------| 1. | 1 1 | 2. | 3 2 | 3. | 5 3 | 4. | 6 5 | 5. | 11 8 | +----------+

meaning that one contiguous area will be drawn over the range (1,8).

sort specifies that the data be sorted by xvar before plotting.

area_options set the look of the shaded areas. The most important of these options is color(colorstyle), which specifies the color and opacity of both the area and its outline; see [G-4] colorstyle for a list of color choices. See [G-3] area_options for information on the other area_options.

axis_choice_options associate the plot with a particular y or x axis on the graph; see [G-3] axis_choice_options.

twoway_options are a set of common options supported by all twoway graphs. These options allow you to title graphs, name graphs, control axes and legends, add lines and text, set aspect ratios, create graphs over by() groups, and change some advanced settings. See [G-3] twoway_options.


Remarks are presented under the following headings:

Typical use Advanced use Cautions

Typical use

We have daily data recording the values for the S&P 500 in 2001:

. sysuse sp500

. list date high low close in 1/5 +-----------------------------------------+ | date high low close | |-----------------------------------------| 1. | 02jan2001 1320.28 1276.05 1283.27 | 2. | 03jan2001 1347.76 1274.62 1347.56 | 3. | 04jan2001 1350.24 1329.14 1333.34 | 4. | 05jan2001 1334.77 1294.95 1298.35 | 5. | 08jan2001 1298.35 1276.29 1295.86 | +-----------------------------------------+

We will use the first 57 observations from these data:

. twoway rarea high low date in 1/57 (click to run)

Advanced use

rarea works particularly well when the upper and lower limits are smooth functions and when the area is merely shaded rather than given an eye-catching color:

. sysuse auto, clear

. quietly regress mpg weight

. predict hat

. predict s, stdf

. generate low = hat - 1.96*s

. generate hi = hat + 1.96*s

. twoway rarea low hi weight, sort color(gs14) || scatter mpg weight (click to run)

Notice the use of option color() to change the color of the shaded area. Also, we graphed the shaded area first and then the scatter. Typing

. twoway scatter ... || rarea ...

would not have produced the desired result because the shaded area would have covered up the scatterplot.

Also see [G-2] graph twoway lfitci.


Be sure that the data are in the order of xvar, or specify rarea's sort option. If you do neither, you will get something that looks like modern art; see Cautions in [G-2] graph twoway area for an example.

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index