Home  /  Resources & Support  /  Introduction to Stata basics  /  Bar charts

Bar charts are a popular tool used to visualize the frequency or percentage of observations in each group of a categorical variable. You can use Stata's graph bar command to create simple bar charts, or you can add options to make more sophisticated charts.

Let's begin by opening the nhanes2l dataset and tabulating the variable hlthstat.

. webuse nhanes2l
(Second National Health and Nutrition Examination Survey)

. tabulate hlthstat
Health status Freq. Percent Cum.
Excellent 2,407 23.29 23.29
Very good 2,591 25.07 48.36
Good 2,938 28.43 76.79
Fair 1,670 16.16 92.95
Poor 729 7.05 100.00
Total 10,335 100.00

Let's use graph bar to create a simple bar chart for the five categories of hlthstat.

. graph bar (count), over(hlthstat)

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.

. graph bar (count), over(hlthstat)        ///
     title("Categories of health status")

We could rotate our graph to make a horizontal bar chart. This is a useful option when the categories have long names.

. graph hbar (count), over(hlthstat)        ///
     title("Categories of health status")

We can use the blabel() option to display the frequency of each category using a medium-sized, black font.

. graph hbar (count), over(hlthstat)          ///
     title("Categories of health status")     ///
     blabel(bar, size(medium) color(black))

We could also display the percentage of observations in each category and use the format() option to display one digit to the right of the decimal place.

. graph hbar, over(hlthstat)                      ///
     title("Categories of health status")         /// 
     blabel(bar, color(black) size(medium) format(%9.1f))

We can display bar charts for two or more categorical variables and display them in different ways. We could display categories of hlthstat nested within categories of diabetes.

. graph hbar, over(hlthstat) over (diabetes)     ///
     title("Categories of health status")        ///
     blabel(bar, color(black) size(medium) format(%9.1f))

We could display categories of diabetes nested within categories of hlthstat by changing the order of the over() options. We can include the asyvars option to display the bars with different colors.

. graph hbar, over(diabetes) over(hlthstat) asyvars      ///
     title("Categories of health status")                ///
     blabel(bar, size(medium) color(black) format(%9.1f))

We could also stack the bars to display the percentage of people with and without diabetes within each category of hlthstat.

. graph hbar, over(diabetes) over(hlthstat)     ///
     asyvars percentages stack                  ///
     title("Categories of health status")

There are many other options that you can use to customize your bar charts, 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.

See it in action

Watch Bar graphs in Stata.

Tell me more

Read more in the Stata Graphics Reference Manual; see [G] graph bar.