*! version 1.0.0 MLB 20Sep2018
help twby

Title

twby -- Creates a cross-tabulation of graphs

Syntax

twby
rowvar
colvar
, [options]
: twoway graph

left specifies that the row labels appear on the left of the graph

xoffset(#) specifies that the rowlabels move # to the left
(negative numbers) or right (positive numbers)

by options All the options normally used in a
by option, except total, rows(), cols(), holes(),
and colfirst, are allowed.

The twoway graph should not contain a by() option. Anything one would
want to specify there, should be entered in the twby prefix instead.

Description

twby repeats a twoway graph for each combination of values
from two variables; it creates a table of graphs. By aligning the graphs
appropriately, one does not need to label each individual graph, so instead
twby labels the columns at the top and the rows on the side. It automates
the process discussed in (Buis and Weiss 2009)

In its simplest form it requires the two variables representing the rows and
columns of the table of graphs followed by a colon followed by the graph that
is to appear in each cell of the table.

. preserve
. webuse auto2, clear

. twby foreign rep78 : scatter price weight

. restore

(click to run)

Within the twby prefix one can specify options that one would normally
specify in the by() option, except for options that would
intervere with the allignment of graphs ( total, rows(),
cols(), holes(), and colfirst), as twby needs to control
that. We can use those options to make the basic example above a bit more pretty.

. preserve
. // open example data
. webuse auto2, clear

. // use some more sensible units
. replace weight = 0.00045359237*weight
. label variable weight "Weight (tonnes)"
. replace price = price / 1000
. label variable price "Price (1000s $)"

. // the graph
. twby foreign rep78, compact : ///
> scatter price weight, ///
> ylab(,angle(0)) xlab(1(.5)2)

. restore

(click to run)

We can also use twby to visualize a three-way cross-tabulation in a way
similar to tabplot (Cox 2016).

. preserve
. // open example data
. sysuse nlsw88, clear

. // create the necessary categorical variables
. gen byte urban = c_city + smsa if !missing(c_city,smsa)
. label define urban 2 "central city" ///
> 1 "suburban" ///
> 0 "rural"
. label value urban urban
. label variable urban "urbanicity"

. gen byte marst = !never_married + married if !missing(never_married,married)
. label define marst 0 "never married" ///
> 1 "widowed/divorced" ///
> 2 "married"
. label value marst marst
. label var marst "marital status"

. gen byte edcat = cond(grade < 12, 1, ///
> cond(grade == 12, 2, ///
> cond(grade < 16, 3,4))) ///
> if !missing(grade)
. label variable edcat "education"
. label define edcat 1 "< highschool" ///
> 2 "highschool" ///
> 3 "some college" ///
> 4 "college"
. label value edcat edcat

. // the three way table we want to visualize
. bys edcat: tab urban marst, row nofreq

. // recreate that table as variables
. contract edcat marst urban, zero nomiss
. egen tot = total(_freq), by(urban edcat)
. gen perc = _freq / tot *100

. // variables that helps display the numbers in the graph
. gen lab = strofreal(perc, "%5.0f")
. gen y = -5

. // the graph
. twby urban marst , ///
> compact left xoffset(0.5) legend(off) ///
> title("Percentage in each marital status" ///
> "given education and urbanicity") : ///
> twoway bar perc edcat , ///
> xlab(1/4, val alt) yscale(range(0 75)) ///
> ylab(none) ytitle("") barw(.5) || ///
> scatter y edcat , ///
> msymbol(none) mlab(lab) mlabpos(0) mlabcolor(black)

. restore

(click to run)

Options

left specifies that the row labels appear on the left side the table of
graphs. The default is the right.

xoffset(#) Moves the row labels # to the left (negative number) or
right (positive number).

by options All the options normally used in a by option,
except total, rows(), cols(), holes(), and
colfirst, are allowed.

Author

Maarten L. Buis,
University of Konstanz,
maarten.buis@uni.kn

References

Buis, M.L and Weiss, M. (2009) Stata tip 81: A table of graphs,
The Stata Journal, 9(4): 643-647.

Cox, N.J. (2016) Speaking Stata: Multiple bar charts in tableform,
The Stata Journal, 16(2): 491-510.