Sei sulla pagina 1di 27

Introduction to Stata 14 Graphics

Dawn Koffman
Office of Population Research
Princeton University
September 2015

Stata 14 Graphics
Pros:
Many graph types and plot types provided
Multiple plot types may be overlaid
Can easily change overall look of graphs
Same options available for most types of graphs
Very flexible

Cons:
Sometimes slow
Large syntax: 731 page graphics manual!
Stata 14 Graphics Manual is only available on-line:
Help -> PDF Documentation -> [G] Graphics

Stata Graphics References:


http://data.princeton.edu/stata/Graphics.html, by German Rodriguez
A Visual Guide To Stata Graphics, Third Edition, by Michael Mitchell
Stata 14 Graphics Manual (may want to start with graph intro)
2

Stata Graphics Syntax


graph <graphtype>
graph bar
graph twoway <plottype>
graph twoway scatter
graph twoway line
graph twoway lfit
graph twoway lfitci
graphs commands may have options
some options have suboptions or a list of options
graph twoway scatter var1 var2, xlabel(30(10)100, labsize(small))
appearance of graph defined by graph elements:
data - marker symbols, lines
elements within plot region text, marker labels, line labels
elements outside plot region titles, legend, notes, axis labels, tick marks, axis titles
size and shape of plot region and entire graph
3

/* OR */

50

twoway line le year

70

graph twoway line le year

life expectancy
60

sysuse uslifeexp.dta, clear

80

Stata Graphics Syntax: A Simple Example

40

/* OR */

line le year

1900

1920

1940

1960

1980

2000

Year

80
40

50

line le year, scheme(s1mono)

life expectancy
60

70

Using Schemes

1900

1920

1940

1960

1980

2000

Year

80

line le year, scheme(economist)


/* to see list of
scheme names:
graph query, schemes
to change default scheme:
set scheme schemename
*/

60

life expectancy

70

50

1900

1920

1940

1960

1980

40
2000

Year

Multiple Dependent Variables

30

40

50

60

70

80

line le_wmale le_wfemale le_bmale le_bfemale year

1900

1920

1940

1960

1980

2000

Year
Life expectancy, white males
Life expectancy, black males

Life expectancy, white females


Life expectancy, black females
6

Adding Text

40

50

60

70

80

line le_wmale le_wfemale le_bmale le_bfemale year ///


, text(32 1920 {bf:1918} {it:Influenza} Pandemic", place(3))

30

1918 Influenza Pandemic


1900

1920

1940

1960

1980

2000

Year
Life expectancy, white males
Life expectancy, black males

Life expectancy, white females


Life expectancy, black females

Overlaying Two-Way Plot Types

scatter le year if year >= 1950 || lfit le year if

year >= 1950

twoway ///
(scatter le year if year >= 1950) ///
(lfit
le year if year >= 1950)

74
72

/* OR */

70

scatter ///
le year if year >= 1950 ///
|| lfit le year if year >= 1950

76

/* OR */

#delimit
twoway
(scatter
(lfit
#delimit

68

/* OR */
1950

le year if year >= 1950)


le year if year >= 1950);
cr

1960

1970
life expectancy

Year

1980

1990

2000

Fitted values

Overlaying Two-Way Plot Types


>=
>=
<
>=

1925
///
1925 & ///
1950
///
1950
75

scatter le year if year


|| lfit le year if year
year
|| lfit le year if year

65
60

///
>= 1925) ///
>= 1925 & ///
< 1950) ///
>= 1950)

55

twoway
(scatter le year if year
(lfit
le year if year
year
(lfit
le year if year

70

/* OR */

/* OR */

1920

#delimit ;
scatter le year if year >= 1925
|| lfit le year if year >= 1925 & year < 1950
|| lfit le year if year >= 1950;
#delimit cr

1940

1960
Year
life expectancy
Fitted values

1980

2000

Fitted values

Overlaying Two-Way Plot Types


#delimit ;
scatter le_male le_female year if year >= 1950
|| lfit le_male

year if year >= 1950

75
70
65

#delimit cr

80

|| lfit le_female year if year >= 1950;

1950

1960

1970

1980

1990

2000

Year
Life expectancy, males
Fitted values

Life expectancy, females


Fitted values

10

Adding a Title and Removing the Legend

75

80

#delimit ;
scatter le_male le_female year if year >= 1950
|| lfit le_male
year if year >= 1950
|| lfit le_female year if year >= 1950
,title("US Male and Female Life Expectancy, 1950-2000")
text(75 1978 "Female", place(3))
text(68 1978 "Male", place(3))
legend(off);
US Male and Female Life Expectancy, 1950-2000
#delimit cr

70

Female

65

Male

1950

1960

1970

1980
Year

1990

2000

11

Showing Confidence Intervals, Labelling Axes, Modifying Legend


sysuse lifeexp.dta, clear
#delimit ;
twoway
(lfitci lexp safewater if region == 2) /* North America */
(scatter lexp safewater if region == 2)
,title("Life expectancy at birth by access to safe water, 1998")
ytitle("Life expectancy at birth")
xtitle("Percent of population with access to safe water")
legend(ring(0) pos(5) order(2 "Linear fit" 1 "95% CI"));
#delimit cr

Life expectancy at birth


60
70

80

Life expectancy at birth by access to safe water, 1998

50

Linear fit
20

40
60
80
Percent of population with access to safe water

95% CI
100

12

Markers Labels and Subtitles


twoway
(lfitci lexp safewater if region == 2) /* North America */
(scatter lexp safewater if region == 2, mlabel(country))
,title("Life expectancy at birth by access to safe water, 1998")
subtitle("North America")
ytitle("Life expectancy at birth")
xtitle("Percent of population with access to safe water")
legend(ring(0) pos(5) order(2 "Linear fit" 1 "95% CI"));
Life expectancy at birth by access to safe water, 1998
80

North America
Canada
CubaPuerto Rico

Jamaica

Life expectancy at birth


60
70

Panama
Trinidad and Tobago
Mexico
Dominican Republic

El Salvador Honduras

Nicaragua

Guatemala

Haiti

50

Linear fit
20

40
60
80
Percent of population with access to safe water

95% CI
100

13

Position of Marker Labels

Life expectancy at birth by access to safe water, 1998


80

North America
Canada
Cuba
Panama

Life expectancy at birth


60
70

Jamaica

Trinidad and Tobago


Dominican Republic
Honduras
El Salvador

Puerto Rico

Mexico
Nicaragua

Guatemala

50

Haiti

Linear fit

generate pos = 12 if country == "Panama"


20
40
60
80
replace pos = 12 if country == "Honduras"
Percent of population with access to safe water
replace pos = 10 if country == "Cuba"
replace pos = 9 if country == "Jamaica"
replace pos = 9 if country == "El Salvador"
replace pos = 9 if country == "Trinidad and Tobago"
replace pos = 9 if country == "Dominican Republic"
#delimit ;
twoway
(lfitci lexp safewater if region == 2) /* North America */
(scatter lexp safewater if region == 2
, mlabel(country) mlabvposition(pos))
,title("Life expectancy at birth by access to safe water, 1998")
subtitle("North America")
ytitle("Life expectancy at birth")
xtitle("Percent of population with access to safe water")
legend(ring(0) pos(5) order(2 "Linear fit" 1 "95% CI"))
plotregion(margin(r+10));
#delimit cr

95% CI
100

14

Position of Marker Labels


#delimit ;
twoway
(scatter lexp safewater if region == 2 | region == 3
,mlabel(country))
,title("Life expectancy at birth by access to safe water, 1998")
subtitle("North and South America")
ytitle("Life expectancy at birth")
xtitle("Percent of population with access to safe water")
plotregion(margin(r+10));
#delimit cr
Life expectancy at birth by access to safe water, 1998
80

North and South America

Life expectancy at birth


60
65
70
75

Canada
CubaPuerto Rico
Chile
Panama
Uruguay
Argentina
Venezuela
Trinidad and Tobago
Mexico
Dominican Republic
Ecuador Colombia
El SalvadorHonduras
Peru
Nicaragua
Brazil
Jamaica

Paraguay

Guatemala

55

Bolivia

Haiti

20

40
60
80
Percent of population with access to safe water

100

15

and

Legend Display

Life expectancy at birth by access to safe water, 1998


80

North and South America

Life expectancy at birth


60
65
70
75

Position of Marker
Labels

Canada
Puerto Rico
Cuba
Jamaica
Chile
Panama
Uruguay
Argentina
Venezuela
Trinidad and Tobago
Mexico
Dominican Republic
Paraguay
Ecuador
Colombia
El Salvador
Honduras
Peru
Nicaragua
Brazil
Guatemala
Bolivia

55

North America
replace pos = 9 if country == "Argentina"
South America
Haiti
replace pos = 9 if country == "Canada"
replace pos = 9 if country == "Cuba"
20
40
60
80
100
replace pos = 9 if country == "Panama"
Percent of population with access to safe water
replace pos = 9 if country == "Venezuela"
replace pos = 9 if country == "Jamaica"
replace pos = 9 if country == "Dominican Republic"
replace pos = 9 if country == "Ecuador"
replace pos = 9 if country == "El Salvador"
replace pos = 12 if country == "Puerto Rico"
#delimit ;
twoway
(scatter lexp safewater if region == 2
,mlabel(country) mlabvposition(pos))
(scatter lexp safewater if region == 3
,mlabel(country) mlabvposition(pos))
,title("Life expectancy at birth by access to safe water, 1998")
subtitle("North and South America")
ytitle("Life expectancy at birth")
xtitle("Percent of population with access to safe water")
legend(ring(0) pos(5) order(1 "North America" 2 "South America") cols(1));
16
#delimit cr

Marker Size and Symbol, Line Color


twoway
(scatter lexp safewater if region == 2
,mlabel(country) mlabvposition(pos) msize(small))
(scatter lexp safewater if region == 3
,mlabel(country) mlabvposition(pos) msize(small) msymbol(circle_hollow))
(lfit lexp safewater if region == 2, clcolor(navy))
(lfit lexp safewater if region == 3, clcolor(maroon))
,title("Life expectancy at birth by access to safe water, 1998")
subtitle("North and South America")
ytitle("Life expectancy at birth")
xtitle("Percent of population with access to safe water")
legend(ring(0) pos(5) cols(1) order(1 "North America" 2 "South America"
3 "North America linear fit" 4 "South America linear fit"));

Life expectancy at birth by access to safe water, 1998


80

North and South America


Canada

Life expectancy at birth


60
65
70
75

Puerto Rico
Cuba
Chile
Panama
Uruguay
Argentina
Venezuela
Trinidad and Tobago
Mexico
Dominican Republic
Paraguay
Ecuador
Colombia
El Salvador
Honduras
Peru
Nicaragua
Brazil
Jamaica

Guatemala

55

Bolivia

Haiti

20

North America
South America
North America linear fit
South America linear fit
40
60
80
Percent of population with access to safe water

100

17

Marker and Marker Label Color, Line Style


twoway
(scatter lexp safewater if region == 2
,mlabel(country) mlabvposition(pos) msize(small) mcolor(black) mlabcolor(black))
(scatter lexp safewater if region == 3
,mlabel(country) mlabvposition(pos) msize(small) mcolor(black) mlabcolor(black)
msymbol(circle_hollow))
(lfit lexp safewater if region == 2, clcolor(black))
(lfit lexp safewater if region == 3, clcolor(black) clpattern(dash))
,title("Life expectancy at birth by access to safe water, 1998", color(black))
subtitle("North and South America")
ytitle("Life expectancy at birth")
xtitle("Percent of population with access to safe water")
legend(ring(0) pos(5) cols(1) order(1 "North America" 2 "South America"
3 "North America linear fit" 4 "South America linear fit"));
Life expectancy at birth by access to safe water, 1998
80

North and South America


Canada

Life expectancy at birth


60
65
70
75

Puerto Rico
Cuba
Chile
Panama
Uruguay
Argentina
Venezuela
Trinidad and Tobago
Mexico
Dominican Republic
Paraguay
Ecuador
Colombia
El Salvador
Honduras
Peru
Nicaragua
Brazil
Jamaica

Guatemala

55

Bolivia

Haiti

20

North America
South America
North America linear fit
South America linear fit
40
60
80
Percent of population with access to safe water

100

18

By-Graph: Separate Graphs for Each Subset of Data

60
50

Total

60

70

80

S.A.

50

Life expectancy at birth

70

80

#delimit ;
twoway scatter lexp safewater, by(region, total)
,ytitle("Life expectancy at birth")
xtitle("Percent of population with access to safe
water");
#delimit cr
Eur & C.Asia
N.A.

20

40

60

80

100 20

40

60

Percent of population with access to safe water


Graphs by Region

80

100

19

By-Graph Options
twoway scatter lexp safewater
,by(region,total style(compact)
title("Life expectancy by access to safe water") note(""))
ytitle("Life expectancy at birth")
xtitle("Percent of population with access to safe water");
Life expectancy by access to safe water
N.A.

S.A.

Total

60
50
80
70
60
50

Life expectancy at birth

70

80

Eur & C.Asia

20

40

60

80

10020

40

60

Percent of population with access to safe water

80

100

20

Axis Scale, Ticks and Labels


twoway scatter lexp safewater
, by(region,total style(compact)
title("Life expectancy by access to safe water") note(""))
xscale(range(20 100))
xtick(20(10)100)
xlabel(30(10)100, labsize(small))
xtitle("Percent of population with access to safe water")
ytitle("Life expectancy at birth")
ylabel(55(5)80, angle(0));

Life expectancy by access to safe water


Eur & C.Asia

N.A.

S.A.

Total

80
75

Life expectancy at birth

70
65
60
55
80
75
70
65
60
55
30

40

50

60

70

80

90

100

30

40

50

60

Percent of population with access to safe water

70

80

90

100

21

Storing Graphs in Memory


twoway
(scatter lexp safewater if region == 2,
mcolor(black) msize(small)
mlabel(country) mlabvposition(pos) mlabcolor(black))
(lfit lexp safewater if region == 2, clcolor(black))
,name(north_america, replace)
subtitle("North America", color(black))
ylabel(,angle(0))
ytitle("Life expectancy at birth")
xtitle("Percent of population with access to safe water")
legend(off);
North America

80

Canada
Puerto Rico
Cuba

Life expectancy at birth

75

Jamaica
Panama
Trinidad and Tobago
Mexico
Dominican Republic

70

El Salvador
Honduras

65

Nicaragua

Guatemala

60

55

Haiti

20

40
60
80
Percent of population with access to safe water

100

22

Storing Graphs in Memory


twoway
(scatter lexp_sa safewater if region == 3,
mcolor(black) msize(small)
mlabel(country) mlabvposition(pos) mlabcolor(black))
(lfit lexp safewater if region == 3, clcolor(black))
,name(south_america, replace)
subtitle("South America", color(black))
ylabel(, angle(0))
ytitle("Life expectancy at birth")
xtitle("Percent of population with access to safe water")
legend(off);
South America
75

Chile
Uruguay

Life expectancy at birth

Argentina

70

Paraguay

Venezuela

Ecuador

Colombia
Peru

Brazil

65

Bolivia

60
40

50
60
70
80
Percent of population with access to safe water

90

23

Combining Graphs
graph combine north_america south_america
,title("Life expectancy by access to safe water", color(black)) col(1);

Life expectancy by access to safe water


Life expectancy at birth

North America
80
75

Canada
Puerto Rico
Cuba
Panama
Trinidad
Mexico and Tobago

Jamaica

70

El Salvador

Dominican Republic

Nicaragua

Honduras
Guatemala

65
60
55

Haiti

20

40

60
80
Percent of population with access to safe water

100

Life expectancy at birth

South America
75

Chile
Argentina

70

Paraguay

Venezuela
Ecuador

Uruguay

Colombia
Peru
Brazil

65
Bolivia

60
40

50

60
70
Percent of population with access to safe water

80

90

24

Life expectancy by access to safe water


North America
80

Canada

Life expectancy at birth

75

Jamaica

Dominican Republic

70

Puerto Rico
Cuba

Nicaragua

Honduras

graph combine north_america south_america


,title
("Life expectancy by access to safe water",
color(black))
xcommon ycommon
xsize(7) ysize(10.5)
col(1);

Guatemala

60

55

Haiti

20

Graphs

Panama
Trinidad and Tobago
Mexico

El Salvador

65

Combining

40
60
80
Percent of population with access to safe water

100

South America
80

Life expectancy at birth

75
Argentina

70

Paraguay

Venezuela

Ecuador

Chile
Uruguay

Colombia
Peru
Brazil

65
Bolivia

60

55
20

40
60
80
Percent of population with access to safe water

100

25

Saving and Including Stata Graphs


save graph in portable format (format determined by filename extension)
vector formats contain drawing instructions (.wmf .emf .ps .eps .pdf)
resolution independent
work well if graph my be resized
graph export north_america.wmf
raster formats save graph pixel-by-pixel (.png)
use current resolution
work well if including graph on web pages
graph export north_america.png

include "portable-format-graph" in Windows application (Word, Powerpoint):


Insert -> Picture -> From File

26

Using Mata Functions to Add Graphs to Word Document*


create Stata graphs and and use graph export to save graphs in portable format
sysuse uslifeexp
line le year
graph export us_lifeexp_overall.emf, replace
line le_wmale le_wfemale le_bmale le_bfemale year
graph export us_lifeexp_race_gender.emf, replace
use Mata functions to:
create Word document
add Stata graphs
save Word document
mata:
dh = _docx_new()
_docx_image_add(dh, us_lifeexp_overall.emf)
_docx_image_add(dh, us_lifeexp_race_gender.emf)
rc = _docx_save(dh, us_lifeexp_graphs.docx)
end

* Functions available starting with Stata 13


27

Potrebbero piacerti anche