help for cellgraph (Johannes F. Schmieder)

Title

cellgraph -- Command to collapse data to cell level and graph the results.

Syntax

cellgraph varlist [if] [in] , by(byvar1 byvar2) [, options]

where varlist is a variable or a list of variables. byvar1 and byvar2 are the variables that define the cells.
The data is collapsed to cell level, where each cell is defined by byvar1 if only one variable is provided or the combination of the values of byvar1 and byvar2 if two variables are provided.

options description

Main
name(graphname) provide a graph name (just like the name option in other graph commands).
stat(statistics) the cell statistic to be used. If not specified "mean" is assumed.
Other possibilities: min, max, sum, sd, var, p10, p25, p50, p75, p90, etc.
list list collapsed data at the end of the command.
saving(filename, replace): save collapsed data to a file.
baseline(string): normalize series to this baseline observation (subtraction).

Graph options
lpattern: specify line pattern.
lpatterns(string): specify multiple line patterns.
scatter: create a scatter plot.
line: create a line plot.
gradient: apply a color gradient as the gradient for the second by variable.
colors(col1; col2; ...) provide a list of colors to replace standard palette.
Separate colors with semicolons. E.g. colors(dkgreen; cranberry; dknavy) or colors(234 40 100; 128 0 128).
lwidth(string): specify line width.
* provide any twoway options to pass through to the call of the twoway command
see the example for why this might be useful. Can also be used to
overwrite options that are given as standard, for example title(My Title)
would overwrite the standard title with "My Title"

Marker options
msymbols(symbol1 symbol2 ...) Change marker symbol where symbol1 etc is of symbolstyle.
nomsymbol: do not use marker symbols.
msize(string): specify marker size.
mcounts: display observation counts next to markers.

Binning options
binscatter(integer): create a binned scatter plot with the specified number of bins.
bin(real) bin the data by the specified real number as bin width.
lfit: add a linear fit line for each plotted statistic.
coef: display regression coefficients.
45deg: add a 45-degree reference line.

Confidence intervals
noci don't display confidence intervals.
cipattern(string): specify confidence interval pattern, either 'shaded' or 'lines'.
ciopacity(integer): specify the opacity for confidence intervals (0-100).

Controlling for covariates
controls(varlist): partial out specified covariates before collapsing.
Internally runs a regression of each outcome on varlist with
fixed effects for the first by variable, then subtracts the
fitted component due to the controls (mean-centered) so the plot
reflects variation net of controls. Requires reghdfe.

Legend options
addnotes: Add notes with sample sizes to the legend.
samplenotes(string): add sample notes to the plot.
nonotes don't display any notes in legend.
nodate don't display date in notes.

Computational Tools
gtools: use gtools for data processing.
ftools: use ftools for data processing.

Category Ordering
xorder(varname): order categories of first by-variable by mean of varname.
Only works with categorical by-variables (string or with value labels).
Suboptions:
descending - sort in descending order (highest first)
stat(statname) - use specified statistic instead of mean
Example: xorder(wage, descending stat(median))

Axis Label Options
xlabel(suboptions): additional x-axis label suboptions (e.g., ang(45), labsize(small)).
These are merged with the auto-generated category labels.
ylabel(suboptions): additional y-axis label suboptions (e.g., format(%9.2f), labsize(small)).

Description

Data is collapsed to cell level, where cells are defined by one or two categorical variables (byvar1 and byvar2) and cell means (or other statistics) of a third variable (varname) are graphed.

Requirements

cellgraph requires Stata 18 or newer. The base command has no
user-written dependencies. Option controls() requires reghdfe;
option gtools requires gtools; and option ftools requires
ftools.

Examples

Example 1 - Basic Plot: One outcome variable one by variable

sysuse nlsw88, clear
keep if grade>=8
cellgraph wage, by(grade)

(click to run)

Example 2 - One outcome variable, two by variables, marker counts

sysuse nlsw88, clear
gen logwage = log(wage) if grade>=8
label var logwage "Log Wage"
cellgraph logwage, by(grade union) mcounts

(click to run)

Example 3 - One outcome variable, two by variables, manual RGBcolors

sysuse nlsw88, clear
gen logwage = log(wage) if grade>=8
label var logwage "Log Wage"
cellgraph logwage, by(grade union) colors(128 0 128; 0 128 128)

(click to run)

Example 4 - Multiple Statistics one by variable, color gradient

sysuse nlsw88, clear
gen logwage = log(wage) if grade>=8
label var logwage "Log Wage"
cellgraph logwage, by(grade) stat(p10 p25 p50 p75 p90) gradient

(click to run)

Example 5 - Two Outcomes, two statistics

sysuse nlsw88, clear
cellgraph wage hours if grade>=8 , by(grade) stat (mean median) mcounts ciopacity(20)

(click to run)

Example 6 - Binned Scatterplot with Linear Fit

sysuse auto , clear
cellgraph mpg, by(weight) binscatter(20) scatter noci lfit coef legend(off)

(click to run)

Example 7 - Binned Scatterplot with 2 Groups

sysuse auto , clear
cellgraph mpg, by(weight foreign) binscatter(20) scatter noci lfit coef

(click to run)

Example 8 - Ordering Categories by Outcome Variable

sysuse nlsw88, clear
cellgraph wage, by(occupation) xorder(wage, descending)

(click to run)

Example 9 - Rotated X-axis Labels

sysuse nlsw88, clear
cellgraph wage, by(occupation) xorder(wage, descending) xlabel(ang(45))

(click to run)

Example 10 - Custom Axis Label Formatting

sysuse nlsw88, clear
cellgraph wage, by(occupation) xlabel(ang(45) labsize(small)) ylabel(format(%9.2f))

(click to run)

Author

Johannes F. Schmieder, Boston University, USA

Email: johannes@bu.edu

Comments welcome!

Also see

On-line: help for collapse, tabstat