Graphs: Axes

Prologue: Note that there is a difference between twoway graphs (the most common variety) and univariate (or oneway) graphs. Whereas the former have an x and a y axis, the latter have only a y axis, even though they may sometimes "look like" twoway graphs. For instance, a box-and-whisker plot showing the distribution of a variable for several groups is still considered a univariate graph; hence you cannot use options that refer to the x axis. The final subsection of this section gives some hints that refer to this case.


Range of axes

You can determine the range of the axes via xsc and ysc. Note that you cannot restrict display of values to a smaller set of values than are present in the data; all you can do is to expand the axes beyond the smallest and / or largest values.

xsc(r(0 1)) ysc(r(0 50))

will set the minimum of both axes to 0, the maximum of the x axis to 1 and the maximum of the y axis to 50. If, say, the minimum value of y is 0, you may omit this value in ysc, mentioning only the upper value of 50 within the parenthesis.

Some graphs, such as bar charts, begin the y axis at 0 (zero) by default, and this cannot be overridden solely with ysc, even if zero is not the smallest value in the data. In such cases, try adding exclude0 to the list of options.


Grid lines

Often, the default display of a Stata graph includes grid lines, typically on the y axis. If the graph you wish to produce does not contain grid lines by default, this can be amended via the grid option, as in:

ylabel(0(2000)10000, angle(0) grid)

Conversely, if you wish to suppress grid lines that are displayed by default, you may use the nogrid option.


Axis values (labels)

You can influence which values are displayed (and ticked) on each axis. For instance, if the x axis ranges from 0 to 10,000, you may wish to display values at 0, 2000, 4000 and so forth. The command to achieve this is:

xlabel(0(2000)10000)

The same rules apply to the ylabel command.

The values on the y axis by default are displayed vertically. If you wish to display them horizontally, you have to add a (sub-)option to the ylabel command. If this sub-option is used alone, the command will look like this:

ylabel(,angle(0))

In combination with a definition of the values to be labeled it would look like this:

ylabel(0(2000)10000, angle(0))


Customizing labels

In most cases, Stata will use the numerical values (representing the different categories) of a variable to label the axes of a graph. If the categories have been labeled, Stata will use those labels only if a variable is used to represent different groups, such as when a box plot is created together with the over option.

In other cases, you have to request Stata explicitly if value labels are to be used as axis labels. The way to achieve this is the valuelabels suboption, as in

histogram isced, discrete gap(25) xlabel(, valuelabel)

But you may also submit the labels to be used in the graph explicitly, which is particularly useful if the existing value labels are somewhat longish, such as in:

histogram isced, discrete gap(25) xlabel(1 "Low" 2 "Intermediate" 3 "High")

Normally, labels are displayed side by side on the x axis. This may sometimes cause trouble if there are many labels or labels are long. You may use the alternate (or alt) option which will create an offset between labels:

xlabel(1 "Low income" 2 "Intermediate income" 3 "High income", alt)


Sizes of axis values (labels)

The size of the axis labels can be changed with the labsize sub-option within the xlabel or the ylabel option. So,

ylabel(0(2000)10000, axis(0) labsize(medlarge))

will render the labels somewhat larger.

The information available via help textsizestyle, which was introduced above, is also valid for the size of labels.


What if there is no x axis?

A typical case of a plot with only an apparent x axis is a boxplot showing the distributions of a number of groups. As is shown elsewhere, such a plot is produced using the over option, as in

graph box income, over(status)

Therefore, any options that refer to the labeling of the pseudo-x-axis have to be included as sub-options to the over option. For instance, to display the values (or labels) that denote the different groups in a larger font, you may include the label sub-option:

graph box income, over(status, label(labsize(medlarge))

© W. Ludwig-Mayerhofer, Stata Guide | Last update: 18 Apr 2017