Stata Learning Module
Graphics: Twoway Scatterplots

This module shows some of the options when using the twoway command to produce scatterplots.  This is illustrated by showing the command and the resulting graph.  This includes hotlinks to the Stata Graphics Manual available over the web and from within Stata by typing help graph.

Two Way Scatterplots


....

Basic twoway scatterplot

twoway (scatter read write)


Schemes


 

Using Economist Scheme

twoway (scatter read write) , scheme(economist)
 

 

Using s1mono Scheme

twoway (scatter read write) , scheme(s1mono)


Marker Placement Options (i.e. Jitter)


 

Scatterplot with jitter

twoway (scatter write read, jitter(3))

Without jitter

twoway (scatter write read)


Marker Label Options


 

Using small black square symbols.

twoway (scatter write read, msymbol(square) msize(small) mcolor(black))


 

With markers red on the inside, black medium thick outline

twoway (scatter write read, mfcolor(red) mlcolor(black) mlwidth(medthick) )


Identifying Observations with Marker Labels

twoway (scatter read write, mlabel(id))


 

Using large red marker labels at 12 O'clock

twoway (scatter read write if id <=10, mlabel(id) mlabposition(12) mlabsize(large) mlabcolor(red))


 

Markers at 90 degree angle at 12 O'clock with a gap of 5

twoway (scatter read write if id <=10, 
        mlabel(ses) mlabangle(90) mlabposition(12) mlabgap(5))

If mlabgap option is omitted

twoway (scatter read write if id <=10, ///
        mlabel(ses) mlabangle(90) mlabposition(12))


 

Modifying marker position separately for variables (1)

generate pos = 3
replace pos = 1 if (id == 5)
replace pos = 5 if (id == 6)
replace pos = 9 if (id == 3)
twoway (scatter read write if id <= 10,mlabel(ses) mlabv(pos))

 If option mlabv is not used

twoway (scatter read write if id <= 10, mlabel(ses))     
 
 

Connect Options


 

Connecting with straight line

egen mread = mean(read), by(write)
twoway (scatter mread write, connect(l) sort)

If the sort option is omitted

twoway (scatter mread write, connect(l))

 

Medium thick black dotted connecting line

twoway (scatter mread write, connect(l) clwidth(medthick) clcolor(black) clpattern(dot) sort)


 

Show gaps in line when there are missing values

egen sdread = sd(read), by(write)
twoway (scatter sdread write, connect(l) sort cmissing(n))

Omitting cmissing option

twoway (scatter sdread write, connect(l) sort cmissing(n))

Footnotes

#1. Notice that the variable pos is used to control the position of the marker label. As shown in the code (repeated below), pos is assigned a value of 3 representing 3 O'Clock, and then when id is 5 the position of the marker label is 1 O'Clock, and when id is 5 the position is 5 O'Clock, and then when id is 3 the position is 9 O'Clock, allowing us to avoid labels that run off the edge of the graph or overwrite each other.

generate pos = 3
replace pos = 1 if (id == 5)
replace pos = 5 if (id == 6)
replace pos = 9 if (id == 3)


How to cite this page

Report an error on this page or leave a comment

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.