Stata for Researchers: Graphics

This is part nine of the Stata for Researchers series. For a list of topics covered by this series, see the Introduction. If you're new to Stata we highly recommend reading the articles in order.

Stata has a suite of tools for creating publication-quality graphs. Graphs are inherently complicated objects and the syntax for creating them can also get quite complicated. However, simple graphs with the default settings are very easy to make.

This article will give you a very brief introduction to Stata's graphical capabilities. If this is a topic that interests you, you may want to skip this article and read our Introduction to Stata Graphics instead (either before or after proceeding to the next article in this series, Do Files and Project Management).

Load the automobile data set once again:

sysuse auto, replace

Then make a scatterplot of mpg versus weight by typing:

scatter mpg weight

If you want a line graph instead, type:

line mpg weight, sort

The sort option here does not mean Stata should sort the data. Rather it means that the line should be drawn from the observation with the smallest value of weight to the observation with the next smallest, etc. Without it the line would be drawn from observation one to observation two to observation three and so forth, and the result would look like a scribble (try it).

Since graphs can be so complicated we don't suggest trying to memorize the syntax for every setting and detail. Keeping track of such things is the great strength of a point-and-click graphical user interface, so this is an area where we suggest taking advantage of it. Stata will translate what you choose into a Stata command which you can then put into a do file, rerun or modified.

To make the same basic line graph using the graphical user interface, start by clicking Graphics, Twoway graph (twoway meaning a graph that has an X and a Y). Then click the Create button to create a new plot.

You'll then get a window where you can choose the basic properties of your plot. Leave the category set to Basic plots, set the type to Line and choose or type mpg as the Y variable and weight as the X variable. Check the box that says Sort on x variable. Then click Accept.

Creating a line graph

This will take you back to the main graphics Window. You could click Create again to add another plot, which would be overlaid on the line plot you already defined. But there are several other tabs that control the properties of the entire graph.

Select the if/in tab and you can choose which observations are to be included. Type price<10000 in the If: box (note that you shouldn't type the word if).

Selecting observations with if

Next click on the By tab. Check the box Draw subgraphs for unique values of variables and for Variables choose or type foreign.

By: options

Click OK, and the graph will be created. The command for creating it will also be placed in the results window:

twoway (line mpg weight, sort) if price<10000, by(foreign)

Note how for graphs by is an option, not a prefix like you're used to. That's because you're not creating two completely separate graphs for the domestic and foreign cars like you would with the standard by:. Instead you're creating one graph with the two subpopulations next to each other.

If you click Graphics, Twoway graph again, the same settings will still be there so you can refine the options you chose. Once you've got the graph you want, copy the resulting command into a do file. If you want to start a new graph instead, click on the large R (reset) button in the lower left of the window.

For much more information about creating graphs, see An Introduction to Stata Graphics.


  1. Create a scatter plot of price on weight.
  2. Split that scatter plot into seven plots, one for each value of rep78 including missing and a total.

Next: Project Management

Previous: Combining Data Sets


Last Revised: 10/19/2009