help spineplot (SJ16-2: gr0031_1)

Title

spineplot -- Spineplots for two-way categorical data

Syntax

spineplot
yvar xvar
[if] [in]
[weight]
[,
bar1(twoway_bar_options) ...
bar20(twoway_bar_options)
barall(twoway_bar_options)
missing
percent
text(textvar [, marker_label_options])
twoway_options]

fweights and aweights may be specified; see weight.

Description

spineplot produces a spineplot for two-way categorical data. The
fractional breakdown of the categories of the first-named variable
yvar is shown for each category of the second-named variable
xvar. Stacked bars are drawn with vertical extent showing
fraction in each yvar category given each xvar category and
horizontal extent showing fraction in xvar category. Thus the areas of
tiles formed represent the frequencies, or more generally totals, for each
cross-combination of yvar and xvar.

Options

bar1(twoway_bar_options) ...
bar20(twoway_bar_options) allow specification of the
appearance of the bars for each category of yvar using options of
twoway bar.

barall(twoway_bar_options) allows specification of the
appearance of the bars for all categories of yvar using options of
twoway bar.

missing specifies that any missing values of either of the
variables specified should also be included within their own categories.
The default is to omit them.

percent specifies labeling as percentages. The default is labeling as
fractions.

text(textvar [, marker_label_options])
specifies a variable to be shown as text at the center of
each tile. textvar may be a numeric or string variable. It should
contain identical values for all observations in each
cross-combination of yvar and xvar. A simple example is the
frequency of each cross-combination. To show nothing in
particular tiles, use a variable with missing values (either numeric
missing or empty strings) for those tiles.
A numeric variable with fractional part will typically look best converted
to string as, for example, string(residual, "%4.3f").
The user is responsible for choice of tile colors so that text is readable.
text() may also include
marker_label_options for tuning the display.

twoway_options refers to options of twoway. By
default there are two x axes, axis(1) on top and axis(2) on
bottom, and two y axes, axis(1) on right and axis(2) on left.

Remarks

The name "spineplot" is credited to Hummel (1996). The term is gaining in
popularity but already appears to be differently understood. In the strictest
definition, spineplots are one-dimensional, horizontal stacked bar charts, but
many discussions and implementations allow vertical subdivision (e.g., by
highlighting) into two or possibly more categories. Some literature treats
spineplots, as understood here, under the heading of mosaic plots, variously
with and without also using the term spineplot. This Stata implementation under
the name spineplot thus implies a broad interpretation of the term.
Conversely, the implementation here does not purport to be a general mosaic
plot program.

Textbooks and monographs with examples of spineplots and
related plots include
Schmid (1954),
Cole (1959),
Edwards (1972, 1992),
Ehrenberg (1975),
Lockwood (1979),
Schmid and Schmid (1979),
Altman (1991),
Friendly (2000),
Venables and Ripley (2002),
Gotelli and Ellison (2004, 2013),
Robbins (2005, 2013),
Unwin, Theus, and Hofmann (2006),
Young, Valero-Mora, and Friendly (2006),
Cook and Swayne (2007),
Unwin (2015) and
Friendly and Meyer (2016).
Among several papers, Hofmann's
(2000) discussion is clear, concise, and well illustrated.

Mosaic plots have been reinvented several times under different names.
Hartigan and Kleiner (1981, 1984) introduced, or reintroduced, them into
mainstream statistics. Friendly (2002) cites earlier examples, including the
work of Georg von Mayr (1877), Karl G. Karsten (1923), Erwin J. Raisz
(1934) and Thomas W. Birch (1949).
Hofmann (2007) discusses a mosaic by Francis A. Walker (1874). Other
early examples are those of Willard C. Brinton (1914, quoting earlier work; 1939),
Berend G. Escher (1924) and Hans Zeisel (1947, 1985).

Most implementations of mosaic plots in other software omit axes and numerical
scales and convey a recursive subdivision according to what may be several
categorical variables by a hierarchy of gaps of various sizes. As the graphs
produced by spineplot are restricted to two variables, this Stata
implementation keeps axes and numerical scales as defaults. The distinction
between categories is conveyed by bar boundaries rather than explicit gaps.

A key principle behind any kind of mosaic plot is that a categorical
classification of independent variables would yield tiles that align
consistently. Thus departures from independence, or relationships between
variables, will be shown by failure of alignment.

The restriction to two variables is more apparent than real. Composite
variables may be created by cross-combination of two or more categorical
variables. The egen functions group() and axis() may
be useful for this purpose. axis() is in the egenmore
package from the Statistical Software Components archive and must have been
installed previously. Compare also what Hofmann (2001) calls
"double-decker plots"
(for binary responses) and what Wilkinson (2005) calls
"region trees".

The program works by calculating cumulative frequencies. The plot is then
produced by overlaying distinct graphs, each being a call to
twoway bar, bartype(spanning) for one category of yvar. By
default, each bar is shown with blcolor(bg) blw(medium), which
should be sufficient to outline each bar distinctly but delicately. By
default also, the categories of yvar will be distinguished
according to the graph scheme you are using. With the default
s2color scheme, the effect is reminiscent of canned fruit salad
(which may be fine for exploratory work). For a publishable graph, you
might want to use something more subdued, such as various gray
scales or different intensities.

Options bar1() to bar20() are provided to allow overriding
the defaults on up to 20 categories, the first, second, etc., shown.
The limit of 20 is plucked out of the air as more than any user should
really want. The option barall() is available to override the
defaults for all bars. Any bar#() option always overrides
barall(). Thus, if you wanted thicker blwidth() on all bars,
you could specify barall(blwidth(thick)). If you wanted to
highlight the first category only, you could specify
bar1(blwidth(thick)) or a particular color.

Other defaults include legend(col(1) pos(3)). At least with
s2color, a legend on the right implies an approximately square plot
region, which can look quite good. A legend is supplied partly because
there is no guarantee that all yvar categories will be represented
for extreme categories of xvar. However, it will often be possible
and tasteful to omit the legend and show categories as axis label text.
An example is given below.

Note the possibility of using plotregion(margin(zero)) to
place axes alongside the plot region.

As with scatterplots, a response variable is usually better shown on
the y axis. If one variable is binary, it is often better to plot that
on the y axis. Naturally, there can be some tension between these
suggestions.
For example, in the auto data, foreign is arguably a predictor
of rep78 rather than vice versa, but I suggest that
spineplot foreign rep78 is more congenial than
spineplot rep78 foreign.

You may need to experiment with different sort orders
for the categorical variables. egen, axis() may
be useful here.

The x axis labels on the bottom axis (axis(2)) are placed
below the middle of each column. As a convenience to users wishing to
override the defaults, the specification is saved as r(catlabels),
so that the command may be repeated with revised positions and/or text.
Type return list to see the specification.

Examples

. sysuse auto

. spineplot foreign rep78

. spineplot foreign rep78, xti(frequency, axis(1)) xla(0(10)60, axis(1)) xmti(1/69, axis(1))

. spineplot rep78 foreign

. set scheme s1color

. bysort foreign rep78: gen freq = _N

. spineplot foreign rep78, text(freq, mlabsize(*1.4)) bar1(color(gs14)) bar2(color(gs10))

. spineplot foreign rep78, text(freq, mlabsize(*1.4)) bar1(color(gs14)) bar2(color(gs10)) legend(off) yla(0.1 "Domestic" 0.9 "Foreign", noticks axis(1))

References

Altman, D. G. 1991.
Practical Statistics for Medical Research.
London: Chapman & Hall.

Anderson, M. J. 2001.
Francis Amasa Walker.
In Statisticians of the Centuries, ed. C. C. Heyde and E. Seneta,
216-218. New York: Springer.

Anonymous. 1967.
In memoriam: Prof. Dr. B. G. Escher.
Geologie en Mijnbouw 46: 417-422.

Birch, T. W. 1949.
Maps: Topographical and Statistical.
London: Oxford University Press.

Brinton, W. C. 1914.
Graphic Methods for Presenting Facts.
New York: Engineering Magazine Company.

Brinton, W. C. 1939.
Graphic Presentation.
New York: Brinton Associates.
http://www.archive.org/stream/graphicpresentat00brinrich

Cole, J. P. 1959.
Geography of World Affairs.
Harmondsworth: Penguin.

Cook, D. and D. F. Swayne. 2007.
Interactive and Dynamic Graphics for Data Analysis: With R and GGobi.
New York: Springer.

Edwards, A. W. F. 1972, revised 1992.
Likelihood: An Account of the Statistical Concept of Likelihood and its Application to Statistical Inference.
London: Cambridge University Press; Baltimore: Johns Hopkins University Press.

Ehrenberg, A. S. C. 1975.
Data Reduction: Analysing and Interpreting Statistical Data.
London: John Wiley.

Escher, B. G. 1924.
De Methodes der Grafische Voorstelling.
Amsterdam: Wereldbibliotheek.

Escher, B. G. 1934.
De Methodes der Grafische Voorstelling. 2nd ed.
Amsterdam: Wereldbibliotheek.

Friendly, M. 2000.
Visualizing Categorical Data.
Cary, NC: SAS Institute.

Friendly, M. 2002.
A brief history of the mosaic display.
Journal of Computational and Graphical Statistics
11: 89-107.

Friendly, M. and D. Meyer. 2016.
Discrete Data Analysis with R: Visualization and Modeling Techniques for Categorical and Count Data.
Boca Raton, FL: CRC Press.

Gotelli, N. J. and A. M. Ellison. 2004 (2nd edition 2013).
A Primer of Ecological Statistics.
Sunderland, MA: Sinauer.

Hartigan, J. A. and B. Kleiner. 1981.
Mosaics for contingency tables.
In Computer Science and Statistics: Proceedings of the 13th Symposium
on the Interface, ed. W. F. Eddy, 268-273. New York: Springer.

Hartigan, J. A. and B. Kleiner. 1984.
A mosaic of television ratings.
American Statistician 38: 32-35.

Hertz, S. 2001.
Georg von Mayr.
In Statisticians of the Centuries, ed. C. C. Heyde and E. Seneta,
219-222. New York: Springer.

Hofmann, H. 2000.
Exploring categorical data: Interactive mosaic plots.
Metrika 51: 11-26.

Hofmann, H. 2001.
Generalized odds ratios for visual modeling.
Journal of Computational and Graphical Statistics 10: 628-640.

Hofmann, H. 2007.
Interview with a centennial chart.
Chance 20(2): 26-35.

Hummel, J. 1996.
Linked bar charts: Analyzing categorical data graphically.
Computational Statistics 11: 23-33.

Karsten, K. G. 1923.
Charts and Graphs: An Introduction to Graphic Methods in the Control and Analysis of Statistics.
New York: Prentice-Hall.

Lockwood, A. 1969.
Diagrams: A Visual Survey of Graphs, Maps, Charts and Diagrams for the Graphic Designer.
London: Studio Vista.

Raisz, E. J. 1934.
The rectangular statistical cartogram.
Geographical Review 24: 292-296.

Robbins, N. B. 2005 (reissued 2013).
Creating More Effective Graphs.
Hoboken, NJ: Wiley; Wayne, NJ: Chart House.

Robinson, A. H. 1970.
Erwin Josephus Raisz, 1893-1968.
Annals of the Association of American Geographers 60: 189-193.

Schmid, C. F. 1954.
Handbook of Graphic Presentation.
New York: Ronald Press.

Schmid, C. F. and Schmid, S. E. 1979.
Handbook of Graphic Presentation.
New York: John Wiley.

Sills, D. L. 1992.
In Memoriam: Hans Zeisel, 1905-1992.
Public Opinion Quarterly 56: 536-537.

Unwin, A. 2015.
Graphical Data Analysis with R.
Boca Raton, FL: CRC Press.

Unwin, A., M. Theus, and H. Hofmann. 2006.
Graphics of Large Datasets: Visualizing a Million.
New York: Springer.

Venables, W. N. and B. D. Ripley. 2002.
Modern Applied Statistics with S.
New York: Springer.

von Mayr, G. 1877.
Die Gesetzmässigkeit im Gesellschaftsleben.
München: Oldenbourg.

Walker, F. A. 1874.
Statistical Atlas of the United States Based on the Results of the Ninth Census 1870.
New York: Census Office.

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

Young, F. W., P. M. Valero-Mora, and M. Friendly. 2006.
Visual Statistics: Seeing Data with Dynamic Interactive Graphics.
Hoboken, NJ: Wiley.

Zeisel, H. 1947.
Say It with Figures.
New York: Harper.

Zeisel, H. 1985.
Say It with Figures. 6th ed.
New York: Harper & Row.

Stored results

Macros

r(catlabels) specification of x axis labels, axis 2

Acknowledgments

Matthias Schonlau, Scott Merryman, and Maarten Buis provoked the writing of this
program through challenging Statalist postings, which reawakened a
long-standing thought that someone, perhaps me, should implement spineplots in
Stata. A suggestion from Peter Jepsen led to the text() option. Private
emails from Matthias Schonlau and Antony Unwin highlighted different senses of
spineplots and the importance of sort order. Antony suggested standardizing on
"spineplot" rather than "spine plot". Maarten verified for me that the
spineplot in my copy of Escher (1934) also appears in Escher (1924). Vince
Wiggins originally told me about the undocumented bartype(spanning)
option.

Dimitriy V. Masterov prompted revision in 2016 to improve handling of x axis labels.

Author

Nicholas J. Cox, Durham University

n.j.cox@durham.ac.uk

Also see

Article: Stata Journal, volume 8, number 1: gr0031

Online: [R] histogram,
catplot (if installed),
tabplot (if installed),
egenmore (if installed),
vreverse (if installed)