Skip to Main Content

Graphics:Twoway Scatterplots | Stata Learning Modules

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)

Image graph81-1


Schemes


Using Economist Scheme

twoway (scatter read write) , scheme(economist)
Image wpeC

Using s1mono Scheme

twoway (scatter read write) , scheme(s1mono)

Image graph95


Marker Placement Options (i.e. Jitter)


Scatterplot with jitter

twoway (scatter write read, jitter(3))

Image graph83-1

Without jitter

twoway (scatter write read)

Image graph94


Marker Label Options


Using small black square
symbols.

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

Image graph82


With markers red on the inside, black medium
thick outline

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

Image graph98


Identifying Observations with Marker Labels

twoway (scatter read write, mlabel(id))

Image graph96


Using large red marker labels at 12 O’clock

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

Image graph97


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))

Image wpe15

If mlabgap option is omitted

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

Image graph811


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))
Image graph100

 If option mlabv is not used

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

Image graph812

Connect Options


Connecting with straight line

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

Image wpe1F

If the sort option is omitted

twoway (scatter mread write, connect(l))

Image graph810-1


Medium thick black dotted connecting line

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

Image graph813


Show gaps in line when there are
missing values

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

Image graph814

Omitting cmissing option

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

Image graph815

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)