• Laura Whiting

Stata Histograms - How to Show Labels Along the X Axis

When creating histograms in Stata, by default Stata lists the bin numbers along the x-axis. As histograms are most commonly used to display ordinal or categorical (sometimes called nominal) variables, the bin numbers shown usually represent something. In Stata, you can attach meaning to those categorical/ordinal variables with value labels. To learn how, check out this Tech Tip about The label command. When you have labels attached you can get these to display on your histogram. In this post we show you how to attach these labels to your histogram, in place of the base numbers.

How to Use:

Worked Example:

In this example I am going to use an economic dataset I developed called “econostata.dta”. In this dataset is the categorical variable “category”, which separates purchases into 9 different categories. As this variable is categorical that means it is discrete rather than continuous, and I am going to show frequency on the y-axis rather than the default density. I also don’t want the x-axis to show a title, as I am already showing all the category names instead. In the command pane I type the following:

This command produces the following graph:

To break down the command I used, the option xtitle(“”) prevented the variable name “Category” from appearing as a title along the x-axis. Instead we only see the category names and the “Frequency” label for the y-axis. Within the xlabel( option the 1(1)9, meant that all numeric categories are shown separately along the x-axis (there are 9 categories total); labsize(vsmall) tells Stata how small I want the category text to appear; angle(forty_five) tells Stata I want the labels to appear at a 45 degree angle; and valuelabel tells Stata to show category labels rather than just the number assigned to that category.

If you would like to play around with this dataset I have linked it here as an excel file. Please note the categorical variables were exported as text, so you will need to alter them in Stata using the encode command. A list of commands to do this is saved in the second sheet of the spreadsheet and I have included a text do-file here.

9 views0 comments

© 2020 by Survey Design and Analysis Services. 

  • LinkedIn
  • Facebook
  • Twitter
  • YouTube