[Stata] Creating Custom Graphs in Stata

In this blog post, we’ll explore various graphing options in Stata using the NHANES2 dataset. The National Health and Nutrition Examination Survey (NHANES) provides a wealth of data on the health and nutritional status of the U.S. population.

Stata
webuse nhanes2, clear

Basic Graphing in Stata

Let’s start with some basic graph examples before we dive into customization.

Histogram

A histogram is a great way to visualize the distribution of a variable like age. The basic command for a histogram looks like this:

Stata
hist age, width(5) frequency

Customizing Titles and Axis Labels

Stata provides several options for customizing the appearance of your graphs. Here’s how to apply different customizations to the histogram.

1. Adding a Title and Subtitle

Use the title() option to add a title, and subtitle() to add a subtitle. This helps give context to the graph.

Stata
histogram age, width(5) frequency title("Age Distribution in NHANES II") subtitle("Sample Size: 10,000 Participants")

2. Customizing Axis Labels

To make the graph easier to understand, it’s important to clearly label both axes. Use xtitle() and ytitle() to set the x-axis and y-axis titles.

Stata
histogram age, width(5) frequency title("Age Distribution in NHANES II") ///
    subtitle("Sample Size: 10,000 Participants") xtitle("Age of Participants") ytitle("Frequency")

3. Changing Axis Label Angles

You can adjust the angle of axis labels to make them more readable, especially for long variable names. Use angle() within the xlabel() or ylabel() option to rotate labels.

Stata
histogram age, width(5) frequency title("Age Distribution in NHANES II") ///
    subtitle("Sample Size: 10,000 Participants") xtitle("Age (Years)") ytitle("Frequency") ///
    xlabel(, angle(45))

4. Customizing Axis Ticks

By default, Stata will decide the tick marks on the axes. However, you can control these with the xlabel() and ylabel() options.

Stata
histogram age, width(5) frequency title("Age Distribution in NHANES II") ///
    subtitle("Sample Size: 10,000 Participants") xtitle("Age (Years)") ytitle("Frequency") ///
    xlabel(10(10)100) ylabel(0(500)5000)

Here, xlabel(10(10)100) tells Stata to place ticks every 10 units from 10 to 100 on the x-axis, while ylabel(0(500)5000) places ticks every 500 units from 0 to 5000 on the y-axis.

5. Changing Schemes and Fonts

You can easily modify the appearance of the graph, including fonts and color schemes. For example, you can change the default font of graph to Times New Roman as follows:

Stata
graph set window fontface "Times New Roman"

To make it default again, you can use the following command:

Stata
graph set window fontface default

To customize the graph style with the scheme, please refer to the following post:

6. Adding a Legend

For graphs with multiple elements (like multiple series in a line graph), you can add a legend to identify each element. Use the legend() option.

Stata
graph twoway (line systolic age) (line diastolic age), legend(label(1 "Systolic") label(2 "Diastolic"))

In this example, the legend() option assigns labels to each line in the graph.

7. Saving Your Graph

Once you are satisfied with your graph, save it to your disk. Stata allows you to save your graphs in a variety of formats, such as PNG, JPEG, PDF, and EPS.

Stata
graph export "graph_filename.png", as(png) replace
  • November 13, 2024