Many thanks to KitBaum , a new version of statplot is now available from SSC.
Co-authored with Nick Cox , -statplot- plots summary statistics for a varlist, labelling the categories along the axis rather than in a legend.
Version 1.3.0 adds seven new types/groups of options (with earlier options retained and unchanged to ensure backward compatibility), these new options include: ci, level() and ciopts() for confidence-interval error bars; percent and share base() for rescaling a statistic to 0-100; sort, descending, order(), first() and last() for the order of the plotted variables; headings() and groups() for section titles and bracketed spans of bars; savedata(), listdata and frame() for handing back the collapsed resultsset that is graphed; wrap() for breaking long axis labels over several lines; and r() returns, statplot now being rclass.
A few worked examples below for those that are interested or long-time users of the original -statplot- (more in the help file).
Co-authored with Nick Cox , -statplot- plots summary statistics for a varlist, labelling the categories along the axis rather than in a legend.
Version 1.3.0 adds seven new types/groups of options (with earlier options retained and unchanged to ensure backward compatibility), these new options include: ci, level() and ciopts() for confidence-interval error bars; percent and share base() for rescaling a statistic to 0-100; sort, descending, order(), first() and last() for the order of the plotted variables; headings() and groups() for section titles and bracketed spans of bars; savedata(), listdata and frame() for handing back the collapsed resultsset that is graphed; wrap() for breaking long axis labels over several lines; and r() returns, statplot now being rclass.
Code:
ssc install statplot, replace
Code:
*--- 1a. Confidence intervals on group means --*
sysuse nlsw88, clear
statplot wage, over(race) ci ///
title("Mean hourly wage by race, with 95% CI")
graph export "01_ci_hbar.png", replace width(900)
* several variables -> small multiples, one panel per variable.
* Each panel is titled with its variable label (via graph -by()-), so leave the
* overall title off here; a user title() would repeat once per panel.
statplot wage ttl_exp tenure, over(race) ci
graph export "02_ci_panels.png", replace width(1000)
* recast as vertical bars or as a dot/interval plot.
* baropts() styles the CI bars; ciopts() styles the whiskers.
statplot wage, over(race) ci recast(bar) ///
baropts(fcolor(navy) lcolor(navy)) ciopts(lcolor(gs6))
graph export "03_ci_bar.png", replace width(900)
statplot wage, over(race) ci recast(dot) level(90) ciopts(lcolor(gs6))
graph export "04_ci_dot.png", replace width(900)
*--- 1b. Percentages of a 0/1 indicator, with a % sign on each bar -----*
* (blabel suffix() needs Stata 19+; drop it on earlier Stata)
statplot union, over(race) percent ///
blabel(bar, format(%2.0f) suffix(%)) ///
title("Union membership rate by race")
graph export "05_percent.png", replace width(900)
*--- 1c. Shares of a total -------------------------------------------*
sysuse census, clear
statplot marriage divorce, over(region) s(sum) share base(var) ///
title(`"Marriages and divorces: "' `"share across regions (each var = 100%)"')
graph export "06_share.png", replace width(900)
*--- 1d. Wrapping long labels ----------------------------------------*
* wrap() breaks labels longer than # characters onto several lines. With a
* handful of bars the lines have room; a chart with many tall over() categories
* will also want a larger ysize() and/or a smaller label size.
sysuse nlsw88, clear
statplot ttl_exp tenure grade hours, wrap(18) ///
title("Wrapped variable labels")
graph export "07_wrap.png", replace width(900)
*--- 1e. Ordering: rank by value, pin a category last ----------------*
sysuse citytemp, clear
statplot tempjan tempjuly heatdd cooldd, sort descending ///
title("Variables ordered high-to-low by mean")
graph export "08_sort.png", replace width(900)
*--- 1e2. Headings and groups among the bars (coefplot-style) ---------*
* headings() = section-title rows (bars shift down); groups() = side brackets.
sysuse auto, clear
statplot price mpg weight length headroom trunk, ///
headings(price = "{bf:Cost}" mpg = "{bf:Efficiency}" weight = "{bf:Size}") ///
title("headings(): section titles among the bars")
graph export "10_headings.png", replace width(900)
statplot price mpg weight length headroom trunk, ci ///
groups(price mpg = "{bf:Performance}" weight length headroom trunk = "{bf:Dimensions}") ///
title("groups(): bracket labels beside spans")
graph export "11_groups.png", replace width(900)
*--- 1f. Keep / view the numbers behind the bars ---------------------*
sysuse citytemp, clear
statplot heatdd cooldd, over(region) listdata // to the screen
statplot heatdd cooldd, over(region) savedata("09_results", replace)
statplot heatdd cooldd, over(region) frame(sp_results) // Stata 16+
capture frame dir
****
*Testing previous statplot options with some updated features:
* (A) default path still equals collapse -----------------------------*
sysuse citytemp, clear
tempfile sd
qui statplot heatdd cooldd, over(region) savedata("`sd'", replace)
preserve
collapse (mean) heatdd, by(region)
qui su heatdd if region==1, meanonly
local truth = r(mean)
restore
preserve
use "`sd'", clear
qui su heatdd if region==1, meanonly
restore
* (B) percent == 100 x mean ------------------------------------------*
sysuse nlsw88, clear
qui statplot union, over(race) percent savedata("`sd'", replace)
qui su union if race==2, meanonly
local want = r(mean)*100
preserve
use "`sd'", clear
qui su union if race==2, meanonly
restore
* (C) ci bounds == ci means ------------------------------------------*
sysuse nlsw88, clear
qui statplot wage, over(race) ci savedata("`sd'", replace)
qui ci means wage if race==2
local lb = r(lb)
preserve
use "`sd'", clear
qui su lo if race==2, meanonly
restore
* (D) wrap and sort leave the values untouched -----------------------*
sysuse citytemp, clear
qui statplot heatdd cooldd, over(region) wrap(6) sort savedata("`sd'", replace)
preserve
collapse (mean) heatdd, by(region)
qui su heatdd if region==3, meanonly
local truth = r(mean)
restore
preserve
use "`sd'", clear
qui su heatdd if region==3, meanonly
restore
