Although I prefer dotplots over barcharts, I often miss some of the
facilities we have with R base
barplot, or its
barchart, used in combination with
Here is a sample data file, taken
from Hosmer and Lemeshow textbook on Applied Logistic Regression. You can
webuse lbw if you prefer.
insheet using "birthwt.csv", clear label define ethn 1 "White" 2 "Black" 3 "Other" label values race ethn codebook bwt race, compact
Here is one possible way to draw a bar chart of frequencies for mothers' ethnicity.
tabulate race, generate(racec) graph bar racec*, ascategory yvaroptions(relabel(1 "White" 2 "Black" 3 "Other")) ytitle("Frequencies")
We can add
graph bar to get counts instead of frequencies.
We may often found the following suggestion on Stata listserver, which
basically consists in letting Stata acummulate counts for a constant
freq, over variable categories with a
generate freq = 1 graph bar (sum) freq, over(race) ytitle("Ethnicity")
However, if the dataset comes with an unique ID for each individual, it is equivalent to use (see,
generate id = _n graph bar (count) id, over(race) asyvars percent showyvars bargap(20) ytitle("Proportions") legend(off)
Much simpler, and intuitive! More, we can use the
percent options to
display proportions rather than counts. To use the same color for all bars,
we can add further options like
bar(1, color(gs6)) bar(2, color(gs6)) bar(3, color(gs6)).
It is also possible to use
twoway bar, as discussed on Stata FAQs,
How can I create variables containing percent summaries?.