Beruflich Dokumente
Kultur Dokumente
UNIVERSITY OF COPENHAGEN
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
Graphs in stata
The main command is graph followed by the type of graph
Graphics in Stata
graph
graph
graph
graph
graph
graph
graph
graph
graph
Klaus K. Holst
29 Sep 2014
U.S. Life Expectancy
90
19001999
80
twoway
matrix
bar
dot
box
pie
save
use
combine
50
60
70
30
40
1
1900
1920
1940
1960
1980
2000
Year
Data 19001999
3
4
5
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
clear
sysuse uslifeexp
describe
Scatter plots
1
life expectancy
60
50
40
70
80
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
1900
1920
1940
1960
Year
1980
2000
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
Scatter plots
Line plots
70
life expectancy
60
50
40
40
50
life expectancy
60
70
80
80
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
1900
1920
1940
1960
1980
2000
1900
1920
1940
Year
1960
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
Line plots
80
50
life expectancy
60
70
1900
1920
1940
1960
Year
2000
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
Schemes
40
1980
Year
1980
2000
s2color
s2mono
s2manual
s2gmanual
s2gcolor
s1color
s1mono
s1rcolor
s1manual
sj
economist
s2color8
lean1
lean2
rbn1mono
see
see
see
see
see
see
see
see
see
see
see
see
see
see
see
help
help
help
help
help
help
help
help
help
help
help
help
help
help
help
scheme_s2color
scheme_s2mono
scheme_s2manual
scheme_s2gmanual
scheme_s2gcolor
scheme_s1color
scheme_s1mono
scheme_s1rcolor
scheme_s1manual
scheme_sj
scheme_economist
scheme_s2color8
scheme_lean1
scheme_lean2
scheme_rbn1mono
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
Schemes
1
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
40
40
50
50
60
life expectancy
60
70
70
80
80
1900
1920
1940
1960
1980
1900
2000
1920
1940
Graphics options
1980
2000
Graphics options
1960
Year
Year
General syntax:
twoway (line ..., line_options) ///
(scatter ..., scatter_options) ///
(lfit ..., lfit_options), twoway_options
In the previous plot the y-axis label disappeared:
1
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
Graphic options
Graphics options
80
shape of marker
colour of marker, inside and out
size of marker
inside or "fill" colour
colour of outline
thickness of outline
40
connect(connectstyle)
sort[(varlist)]
cmissing(y/n)
lpattern(linepatternstyle)
lwidth(linewidthstyle)
lcolor(colorstyle)
lstyle(linestyle)
...
msymbol(symbolstylelist)
mcolor(colorstylelist)
msize(markersizestylelist)
mfcolor(colorstylelist)
mlcolor(colorstylelist)
mlwidth(linewidthstylelist)
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
1900
1920
1940
1960
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
Graphics options
If the axis label is going to be reused many times we may store it in
a macro
1
2000
Digression: Macros
An alias that can be dereferenced in the program everywhere(!)
1
2
3
local a 1
local b a b c
global b "Hello"
local macros lives within this scope where they were defined (i.e.
the do-file or program/function).
1
2
di a
di "$b b"
1
Hello a b c
1980
Year
local a = a+1
di a
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
Digression: Macros
Graphics options
3
4
5
1
2
3
4
capture drop x1 x2
input x1 x2
1 3
2 4
end
local idx 1 2
foreach i in idx {
list xi in 1/2
}
40
+----+
| x1 |
|----|
1. | 1 |
2. | 2 |
+----+
1875
+----+
| x2 |
|----|
1. | 3 |
2. | 4 |
+----+
1900
1925
1950
Year
1975
2000
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
Symbols
palette linepalette
palette symbolpalette
Symbol palette
solid
Oh
oh
Dh
dh
Th
th
Sh
sh
dash
longdash_dot
dot
longdash
dash_dot
smplus
shortdash
shortdash_dot
blank
2025
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
Line types
1
80
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
Colours
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
note
caption
r1 r2
1 2
ringposstyle
7
6
2
1
plot region
0
0
0
0
0
0
0
r1title
title
subtitle
t2title
t1title
r2title
l2title
0 0 0 0 0 0 0 0 0 0 0
l1title
l2 l1
1 2
b1title
b2title
legend
1
2
3
4
5
title
subtitle
t2
t1
b1
b2
legend
note
caption
Customize
single-line
1
set rgbwith
"255
100 50"color-colname.style (in ado-path):
help
title_options
Description
Titles are the adornment around a graph that explains DEPARTMENT
the graphs purpose.
OF BIOSTATISTICS
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
Titles
UNIVERSITY OF COPENHAGEN
Titles
Options
title(tinfo) specifies the overall title of the graph. The title usually appears centered at the top of
the graph. It is sometimes desirable to specify the span suboption when specifying the title, as in
6
7
8
9
10
11
90
80
70
60
. graph
or
50
#delimit ;
twoway (maleline) (femaleline),
title("U.S. Life Expectancy")
subtitle("1900-1999")
caption("Life expectancy by gender")
note("Data 1900-1999")
legend(col(1) ring(0) position(11))
yscale(range(30 90))
ylabel(30(10)90)
name(lifeexptitles, replace);
#delimit cr
40
If subtitle() is used in conjunction with title() and you specify suboption span with
title(), remember also to specify span with subtitle().
30
1900
1920
1940
1960
Year
Data 19001999
1980
2000
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
describe
GNP
per
capita
4
Avg.
annual
%
growth
2
0
80
Life
expectancy
at birth
70
60
50
100
safewater
50
0
0
20000
40000 0
450
60
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
Stratification
We can also use the by option to make different plots for different
levels of a third variable.
4000
2000
Subsetting
1000
Scatter plots
80
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
55
Scatter plots
70
South America
16000
8000
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
Scatter plots, by
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
Scatter plots
Labels can be added to the graph (here we also remove the points)
1
Canada
N.A.
United States
S.A.
75
80
Jamaica
70
60
50
Panama
10000
20000
30000
40000 0
10000
20000
30000
40000 0
10000
20000
30000
40000
Mexico
Dominican Republic
Honduras
El Salvador
Nicaragua
Guatemala
55
60
Graphs by Region
Haiti
10000
30000
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
Curve fits
70
75
75
80
80
55
60
65
20
55
20000
GNP per capita
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
40
60
safewater
95% CI
Life expectancy at birth
0
10000
20000
30000
80
Fitted values
100
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
Bar plots
1
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
Box plots
Box-whisker plots
60
40
20
Box limits are the 25% and 75% quantiles and with median
marked. as a line in the box. The whiskers shows the most extreme
observations (min/max) within 1.5IQR from the the box limits (or
else this limit).
Eur & C.Asia
N.A.
mean of lexp
mean of safewater
S.A.
p 50 of lexp
p 50 of safewater
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
Box plots
80
75
60
Bolivia
55
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
Haiti
N.A.
S.A.
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
Histograms
1
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
QQ-plots
Comparison with the theoretical quantiles of a normal distribution
qnorm lexp
55
60
65
70
Life expectancy at birth
75
80
60
DEPARTMENT OF BIOSTATISTICS
UNIVERSITY OF COPENHAGEN
QQ-plots
1
2
3
capture drop z
gen z = rnormal()
qnorm z
50
.02
.04
Density
.06
.08
80
.1
65
70
75
Inverse Normal
80
85