Histograms are a popular tool used to visualize the distribution of a continuous variable. You can use Stata's histogram command to create simple histograms, or you can add options to make more sophisticated charts.
You can also use twoway graph histogram to create histograms. Here we will focus only on histogram.
Let's begin by opening the nhanes2l dataset.
. webuse nhanes2l (Second National Health and Nutrition Examination Survey)
We can use histogram to create a simple histogram for body mass index (bmi).
. histogram bmi (bin=40, start=12.385596, width=1.2186025)
Next, let's add a title to our graph. Note that I'm using the “triple slash” to write my command across two lines. You can't do this in the Command window, but it is useful when writing long graph commands in do-files.
. histogram bmi, /// title("Histogram of body mass index (BMI)")
histogram renders the y axis using a density scale, but we can rescale it as a frequency, fraction, or percent. Let's rescale our y axis as a frequency.
. histogram bmi, frequency /// title("Histogram of body mass index (BMI)")
Each of the bars is called a "bin", and Stata has chosen the number of bins for us automatically. But we can choose the number of bins if we like. I have specified 15 bins in the example below.
. histogram bmi, frequency bin(15) /// title("Histogram of body mass index (BMI)")
We could specify a starting value and width of the bins rather than the number of bins. I have specified a starting value of 10 and a bin width of 2 in the example below.
. histogram bmi, frequency start(10) width(2) /// title("Histogram of body mass index (BMI)")
We can use the addlabel option to add labels at the top of each bin. And we can use the addlabopts() option to add them with a 6-point black font.
. histogram bmi, frequency start(10) width(2) /// title("Histogram of body mass index (BMI)") /// addlabel addlabopts(mlabsize(6-pt) mlabcolor(black))
We can also add a normal distribution to our histogram. Stata estimates the mean and standard deviation of bmi and overlays a normal distribution with that mean and standard deviation on top of our histogram.
. histogram bmi, frequency start(10) width(2) /// title("Histogram of body mass index (BMI)") /// addlabel addlabopts(mlabsize(6-pt) mlabcolor(black)) /// normopts(lcolor(green) lwidth(thick))
There are many other options that you can use to customize your histograms, and you can read about them in the manual. You can also watch a demonstration of these commands by clicking on the link to the YouTube video below.
Watch Histograms in Stata.
Read more in the Stata Base Reference Manual; see [R] histogram.
Read more in the Stata Graphics Reference Manual; see [G] graph twoway histogram.