Horizontal text and bar plot
textbarplot textvar barvar [if exp] [in range] [ , y(integervar) scatter(scatter_options) addplot(plot) plot(plot) bar_options ]
Description
textbarplot produces a horizontal bar plot with text shown to the left of the bars. It is designed to give a quick-and-easy display. textvar specifies the text and barvar, which must be a numeric variable, specifies the magnitude of the bars.
By default
1. If textvar is a string variable, then observation numbers are used to determine row positions for the bars, which are y axis values on a reversed scale, and its values are used as text.
2. If textvar is a numeric variable with labels, then its numeric values are used to determine row positions, and its labels are used as text.
3. If textvar is an integer-valued numeric variable, then its numeric values are used to determine row positions and are also shown as text.
These defaults can be over-ridden by specifying an integer-valued y variable with the y() option. In that case there are no constraints on what textvar is.
textbarplot is a wrapper for twoway bar and twoway scatter. Other kinds of graph can be obtained by using a recast() option. The most useful alternatives are recast(dot), recast(dropline) and recast(spike).
Remarks
The easiest way of drawing horizontal bar plots for various categories is graph hbar. However, it can be difficult to produce some designs with graph hbar that include many gaps between clusters of bars or repeated category names. textbarplot is implemented in terms of twoway to provide a simpler alternative for some kinds of plots.
Options
y(integervar) specifies an integer-valued numeric variable indicating row positions for the bars. This overrides any default set of row positions.
scatter(scatter_options) indicates options of twoway scatter. Know that the default is scatter y_variable barvar ytitle("") ms(none). Thus there is scope for (e.g.) adding marker labels.
addplot(plot) specifies instructions for other plots to add to the graph. (Stata 9 up.)
plot(plot) specifies instructions for other plots to add to the graph. (Stata 8.)
bar_options refers to options of twoway bar. Know that the default is twoway bar barvar y_variable , yla(row_positions, base(0) valuelabel nogrid noticks ang(h)) horizontal ysc(reverse) ytitle("") barw(0.6) legend(off). Note that possible options include vertical. Those who like their bars to touch the associated axis should specify plotregion(margin(l=0)) or plotregion(margin(b=0)).
Examples
. sysuse auto . textbarplot make mpg if foreign . gsort foreign - mpg . textbarplot make mpg if foreign . textbarplot make mpg if foreign, recast(dot) . textbarplot make mpg if foreign, scatter(mla(mpg)) xsc(r(. 43)) bcolor(red)
The following data come from Statistiska centralbyrån. 2003. Sweden in figures/Sverige i siffror 2004. p.52:
home access to internet 2002 (%) -------------------------------- Men 66.7 Women 60.3 16-24 75.5 25-34 75.0 35-44 80.0 45-54 75.4 55-64 59.9 65-74 29.8 75-84 10.3 Labourers 49.9 Lower white collar 60.8 Managers and officials 83.5 Entrepreneurs 66.3 Farmers 26.8 Old-age pensioners 22.1
To use textbarplot, enter as two variables, say text and access, with blank strings and missing numerics in observations 3 and 11 to indicate blank lines. Then
. label var access "home access to internet (%)" . textbarplot text access
A corresponding set of commands is, given text and access as variables,
. gen axis = _n . labmask axis, values(text) . graph hbar (asis) access, over(axis, relabel(3 "{c 32}" 11 "{c 32}"))
Alternatively, set up a variable directly with integer values and the text here as value labels.
Acknowledgements
Hiroshi Maeda posted a stimulating example on Statalist. Austin Nichols contributed to discussion of graph hbar. Friedrich Huebler, Ronán Conroy and Vince Wiggins made helpful suggestions.
Author
Nicholas J. Cox, Durham University n.j.cox@durham.ac.uk
Also see
On-line: help for twoway bar, graph bar, graph dot, catplot (if