nicelabels(SJ22-?: gr00??)

Title

nicelabels -- Nice axis labels for general scales

Syntax

nicelabels
varname
[if] [in],
local(macname)
[
tight
nvals(#)
]

nicelabels
#1 #2,
local(macname)
[
tight
nvals(#)
]

Description

nicelabels suggests axis labels that would look nice on a graph
using a general scale. It can help when, for example, you want to
choose the same labels for a series of graphs. Results are put in a
local macro for later use.

"Nice" is a little hard to define but easier to recognize. This command
follows common practice in general and Heckbert (1990) in particular in
selecting power-of-ten multiples of 1, 2 or 5 that are equally spaced.

See Hardin (1995) for an earlier implementation in Stata of such ideas.
Hardin (1995) implements what Heckbert calls a "loose" definition of
labels, while this command allows also a "tight" definition.

There are two syntaxes. In the first, the name of a numeric variable
must be given. In the second, two numeric values are given, which will
be interpreted as indicating minimum and maximum of an axis range.
Those two values can be given in any order.

Options

local(macname) inserts the specification of labels in
local macro macname within the calling program's space. Hence,
that macro will be accessible after nicelabels has finished. This
is helpful for later use with graph twoway or other graphics
commands. local() is required.

Anyone new to the idea and use of local macros should study the examples
carefully. nicelabels creates a local macro, which is a kind of
bag holding the text to be inserted in a graph command. The local
macro is referred to in that graph command using the punctuation
` ' around the macro name. Note that the opening (left) single
quote and the closing (right) single quote are different. Other single
quotation marks will not work. Do not be troubled by the closing single
quote (') appearing as upright in many fonts.

tight tends to pull in as compared with the default and so not
suggest labels beyond the minimum or maximum. This option may often be
combined with the nvals() option (see below).

nvals() suggests a number of values to be labelled. The default is
5. Most commonly, you may wish to suggest more values, say
nvals(10). As said, this is a suggestion, not an instruction.
Note the suggestion by Cleveland (1985, 39; 1994, 39) of 3-10 labels on any
axis.

Remarks

Likely uses of this command are in deciding in advance on a set of
labels to be used in a series of graphs, either directly or on a
transformed scale. For example, labels may be determined from a summary
of all data and then used consistently for various graphs of subsets of
the data.

The aim is not to repeat, still less to replace, what is already easy in
Stata. Thus if quantiles are being shown, it may be that labels 0.5,
0.25, 0.75, 0.125, 0.375, 0.625, 0.875, ... appeal for a probability
scale as corresponding to median, quartiles, octiles, and so forth.
These labels are easily produced either by spelling them out or by using
step sizes such as 0.25 or 0.125. The same issue arises with a percent
scale from 0 to 100 whenever labels like 25, 50, 75 seem desirable.

The examples include extensions of the basic idea, as explained now.

Suppose you have a variable that is always positive but you prefer that
a graph axis starts at zero. Then use summarize to find the
maximum and feed that and 0 to the command. Note that using the maximum
from summarize with options such as yla(0(10)`max') or
yla(0(20)`max') or ysc(0 `max') yla(#6) may be as or more
effective. Here local max = r(max) should be assigned just after
summarize.

Suppose you want to insist that the minimum and maximum of a variable
be shown as labels. Compare Tufte's illustration (Tufte 1983: 149;
Tufte 2001: 149). Then again the results of summarize can be used
with results from this command with the tight option.

Suppose you want to insist on at least so many labels. Then count the
labels suggested and re-run the command using the nvals() option.

Suppose you want to show "%" explicitly by each label. Precedents
include Robbins (2013, 188, 250, 278, 318), Knaflic (2015, 1, 48f,
51, 59, 81f, 156, 209f, 228ff, 238f), Koponen and Hildén (2019,
22, 64, 77, 84. 94, 101, 107, 185, 190, 193ff, 206, 209, 214ff),
Wilke (2019, 29, 34, 69, 101f, 104, 107, 139ff, 179, 184f, 234ff,
258f, 261), Tufte (2020, 107), and Gelman, Hill and Vehtari (2021,
4, 29f, 94, 96, 114, 126f, 166, 292, 467f). mylabels will let
you do this. So, use nicelabels to get the labels and then
mylabels to add a "%" suffix. The same point arises with any other
prefix or suffix such as a currency symbol.

The references include some other papers of the author on axis labels and
ticks.

This implementation focuses on the ideas covered by Heckbert (1990).
There is a wider literature both before and after that date. Wilkinson
(1999, 217-218; 2005, 95-97) and Talbot, Lin and Hanrahan (2010)
have spelled out how fully automated choice entails a delicate tradeoff
between several desiderata, chiefly simplicity, coverage, granularity,
and legibility. Talbot, Lin and Hanrahan (2010) give a full survey of
the problem.

Examples

. set scheme s1color

. nicelabels 142 233, local(foo)

(shows 140 160 180 200 220 240)

. nicelabels 142 233, local(foo) tight

(shows 160 180 200 220)

. nicelabels 142 233, local(foo) nvals(10)

(shows 140 150 160 170 180 190 200 210 220 230 240)

. nicelabels 142 233, local(foo) tight

(shows 150 160 170 180 190 200 210 220 230)

. sysuse census, clear

. summarize medage

(shows minimum 24.2, maximum 34.7)

. nicelabels medage, local(agela)

(shows 20 25 30 35)

. nicelabels medage, local(agela) tight

(shows 25 30)

. nicelabels medage, local(agela) nvals(10)

(shows 24 26 28 30 32 34 36)

. nicelabels medage, local(agela) tight nvals(10)

(shows 26 28 30 32 34)

. gen pc_older = 100 * pop65p / pop

. nicelabels pc_older, local(yla)

. mylabels `yla', suffix(%) local(yla)

. scatter pc_older medage, yla(`yla', ang(h)) xla(, format(%2.0f)) ytitle(% 65 and older) ms(none) mlabel(state2) mlabpos(0)

. sysuse auto, clear

. summarize mpg, meanonly

. nicelabels 0 `r(max)', local(foo)

(shows 0 10 20 30 40 50)

. nicelabels mpg, tight local(yla)

. summarize mpg, meanonly

. local yla `yla' `r(min)' `r(max)'

. nicelabels weight, tight local(xla)

. summarize weight, meanonly

. local xla `xla' `r(min)' `r(max)'

. scatter mpg weight, xla(`xla') yla(`yla', ang(h)) ms(Oh)

. nicelabels mpg, tight local(yla)

(shows 20 30 40)

. if wordcount("`yla'") < 5 nicelabels mpg, tight local(yla) nvals(10)

(shows 15 20 25 30 35 40)

Author

Nicholas J. Cox

Department of Geography

Durham University

Durham, UK

n.j.cox@durham.ac.uk

References

Cleveland, W.S. 1985.
The Elements of Graphing Data.
Monterey, CA: Wadsworth.

Cleveland, W.S. 1994.
The Elements of Graphing Data. Rev. ed.
Summit, NJ: Hobart.

Cox, N.J. 2005.
Stata tip 24: Axis labels on two or more levels.
Stata Journal 5: 469.

Cox, N.J. 2007.
Stata tip 55: Better axis labeling for time points and time intervals.
Stata Journal 7: 590-592.

Cox, N.J. 2008.
Stata tip 59: Plotting on any transformed scale.
Stata Journal 8: 142-145.

Cox, N.J. 2012.
Speaking Stata: Transforming the time axis.
Stata Journal 12: 332-341.

Cox, N.J. 2018.
Speaking Stata: Logarithmic binning and labeling.
Stata Journal 18: 262-286. (Update: 2020. 20: 1028)

Cox, N.J. 2021.
Stata tip 141: Adding marginal spike histograms to
quantile and cumulative distribution plots.
Stata Journal 21: 838-846.

Cox, N.J. and V. Wiggins. 2019.
Stata tip 132: Tiny tricks and tips on ticks.
Stata Journal 19: 741-747.

Gelman, A., J. Hill and A. Vehtari. 2021.
Regression and Other Stories.
Cambridge: Cambridge University Press.

Hardin, J.W. 1995.
dm28: Calculate nice numbers for labeling or drawing grid lines.
Stata Technical Bulletin 25: 2-3.
(STB Reprints Vol 5, pp.19-20)
https://www.stata.com/products/stb/journals/stb25.pdf

Heckbert, P.S. 1990. Nice numbers for graph labels.
In A.S. Glassner (Ed.) Graphics Gems.
San Diego, CA: Academic Press,
61-63 and 657-659.

Knaflic, C.N. 2015.
Storytelling with Data: A Data VIsualization Guide for Business Professionals.
Hoboken, NJ: John Wiley.

Koponen, J. and J. Hildén, J. 2019.
The Data Visualization Handbook.
Espoo: Aalto ARTS Books.

Robbins, N.B. 2013.
Creating More Effective Graphs.
Wayne, NJ: Chart House.

Talbot, J., S. Lin, and P. Hanrahan. 2010.
An extension of Wilkinson's algorithm for positioning tick labels on axes.
IEEE Transactions on Visualization and Computer Graphics 16: 1036-1043.

Tufte, E.R. 1983.
The Visual Display of Quantitative Information.
Cheshire, CT: Graphics Press. (2nd edition 2001)

Tufte, E.R. 2020.
Seeing with Fresh Eyes: Meaning, Space, Data, Truth.
Cheshire, CT: Graphics Press.

Wilke, C.O. 2019.
Fundamentals of Data Visualization: A Primer on Making Informative and Compelling Figures.
Sebastopol, CA: O'Reilly.

Wilkinson, L. 1999.
The Grammar of Graphics.
New York: Springer.

Wilkinson, L. 2005.
The Grammar of Graphics.
New York: Springer.

Also see

help: [G-3] axis_label_options

help: niceloglabels (if installed)

help: mylabels (if installed)