Home  /  Resources & support  /  FAQs  /  Pie and bar charts
Note: This FAQ is relevant for users of releases prior to Stata 8.

How can I create a pie or bar chart containing categorical data?

Title   Pie and bar charts
Authors Nicholas J. Cox, Durham University, UK
Jeremy Wernow, StataCorp

In Stata, pie and bar charts are drawn using the sum of the variables specified. Therefore, any zero values will not appear in the chart, as they sum to zero and make no difference to the sum of any other values. They will, however, be mentioned in the key.

If you have a categorical variable that contains labeled integers (for example, 0 or 1, or 1 upwards), and you want a pie or bar chart, you presumably want to show counts or frequencies of those integer values. To create pie charts, first run the variable through tabulate to produce a set of indicator variables:

. tabulate foreign, generate(f) . graph f1 f2, pie

pie1.gif

. tabulate rep78, generate(r) . graph r1 r2 r3 r4 r5, pie

pie2.gif

Indicator variables consist of 1s and 0s. The sum of lots of 1s is, naturally enough, a count (think 1 + 1 + 1 ...). Moreover, any number of 0s make no difference to such a sum.

This concept also applies to bar charts.

. graph f1 f2, bar

bar2.gif

. graph r1 r2 r3 r4 r5, bar

bar1.gif

Another method of creating bar charts is to create a single variable containing only values of 1 and to sum that.

. gen freq = 1 . label var freq "Repair Record 1978" . sort rep78 . gra freq , by(rep78) bar yla

bar3.gif

The resulting graph is different in that the bars are all the same pen color and they do not touch. You may choose which style you prefer.

This method does not apply to pie charts, as graph, pie expects to be fed at least two variables.

A more elaborate application of this approach is the community-contributed program fbar, available for Stata 7. Use net search to identify locations of the program files:

. net search fbar (contacting http://www.stata.com) 1 package found (STB listed first) ---------------------------------- fbar from http://fmwww.bc.edu/RePEc/bocode/f 'FBAR': module to produce bar charts showing frequencies of categorical variables / fbar produces a bar chart showing the frequencies (counts or / percents) of the categories of a variable as vertical bars. The / default is to show counts, as modified by any weights. / Optionally each category

Alternative displays based on automatic counting of occurrences of categorical data are obtainable with two community-contributed commands, tabhbar and tabhplot. Use net search to identify locations of the program files:

. net search tabhbar (contacting http://www.stata.com) 2 packages found (STB listed first) ----------------------------------- tabhbar from http://fmwww.bc.edu/RePEc/bocode/t 'TABHBAR': module to show table of frequencies as horizontal bar chart / tabhbar is a driver for hbar that produces a horizontal bar chart / of the frequencies of the distinct values of either one or two / categorical variables. With one variable, its values define the / `rows' of the bar hplot from http://www.stata.com/meetings/5uk hplot and hbar for presentation graphics / Nick Cox / University of Durham / [email protected] / / After installation, see help hplot, help hbar.
. net search tabhplot (contacting http://www.stata.com) 2 packages found (STB listed first) ----------------------------------- tabhplot from http://fmwww.bc.edu/RePEc/bocode/t 'TABHPLOT': module to show table of frequencies as horizontal plot / tabhplot is a driver for hplot that produces a horizontal plot of / the frequencies of the distinct values of either one or two / categorical variables. With one variable, its values define the / `rows' of the plot. hplot from http://www.stata.com/meetings/5uk hplot and hbar for presentation graphics / Nick Cox / University of Durham / [email protected] / / After installation, see help hplot, help hbar.