### Stata Learning Module Introduction to graphs in Stata

This module will introduce some basic graphs in Stata 8, including histograms, boxplots, scatterplots, and scatterplot matrices.

Let's use the auto data file for making some graphs.

sysuse auto.dta

The histogram command can be used to make a simple histogram of mpg

histogram mpg

The graph box command can be used to produce a boxplot which can help you examine the distribution of mpg. If mpg were normal, the line (the median) would be in the middle of the box (the 25th and 75th percentiles, Q1 and Q3) and the ends of the whiskers (the upper and lower adjacent values, which are the most extreme values which within Q3+1.5(Q3-Q1) and Q1-1.5*(Q3-Q1), respectively) would be equidistant from the box. The boxplot for mpg shows positive skew. The median is pulled to the low end of the box, and the 95th percentile is stretched out away from the box.

graph box mpg

The boxplot can be done separately for foreign and domestic cars using the by( ) option.

graph box mpg, by(foreign)

A two way scatter plot can be used to show the relationship between mpg and weight. As we would expect, there is a negative relationship between mpg and weight.

graph twoway scatter mpg weight

Note that you can save typing like this

twoway scatter mpg weight

We can show the regression line predicting mpg from weight like this.

twoway lfit mpg weight

We can combine these graphs like shown below.

twoway (scatter mpg weight) (lfit mpg weight)

We can add labels to the points labeling them by make  as shown below.  Note that mlabel is an option on the scatter command.

twoway (scatter mpg weight, mlabel(make) ) (lfit mpg weight)

We can combine get separate graphs for foreign and domestic cars as shown below, and we have requested confidence bands around the predicted values by using lfitci in place of lfit .  Note that the by option is at the end of the command.

twoway (scatter mpg weight) (lfitci mpg weight), by(foreign)

You can request a scatter plot matrix with the graph matrix command. Here we examine the relationships among mpg, weight and price.

graph matrix mpg weight price

The content of this web site should not be construed as an endorsement of any particular web site, book, or software product by the University of California.