David LB Schwappach asked privately about getting
results equivalent to -tabhbar- (available from
SSC; requires Stata 6) with -graph hbar- in Stata 8.
The context is that one likes to show tables of
frequencies or percents, say, as horizontal bar
charts.
To explain a little: commands like
. tabhbar rep78
. tabhbar foreign rep78
show horizontal bar charts with the
frequencies of the distinct categories
of -rep78- or of -foreign- and -rep78-
combined.
Various options allow percents or proportions,
among other things.
At first sight there is no near equivalent
with -graph hbar-, but so long as you construct
a variable to feed to -graph hbar-, then various
graphs are easy.
. u auto
Stata can count, just as you can, by adding
1 and 1 and 1 and ..., so that simple frequency displays
need a variable with all values 1:
. gen values = 1
. graph hbar (count) values, over(foreign)
. graph hbar (count) values, over(foreign) over(rep78)
To show percents, we must work backwards using
the definition that percents sum to 100. In
the simplest case that would mean a variable
with values all 100 / _N, but that is a bit awkward
to generalise -- interactively -- to problems
in which subsets of data are specified using -if-
and/or -in-. We can use instead -egen, pc()-
. egen mypc = pc(100)
. graph hbar (sum) mypc, over(rep78)
over(foreign) ytitle(percent of total)
. egen mypc2 = pc(100), by(foreign)
. graph hbar (sum) mypc2, over(rep78)
over(foreign) ytitle(percent of car type)
. egen mypc3 = pc(100) if !foreign
. graph hbar (sum) mypc3 if !foreign,
over(rep78) ytitle(percent of domestic cars)
Fractions (proportions) are equally straightforward:
. egen myprop = pc(1), prop
. graph hbar (sum) myprop, over(rep78)
over(foreign) ytitle(fraction of total)
I suspect that all these commands are
equally valid if -bar- were substituted
for -hbar-, but for that kind of display, there
is an easier way to proceed, by
getting histograms of discrete variables.
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/