Introduction to Stata 13 Graphics Dawn Koffman Office of Population Research Princeton University September 2013 1
Stata 13 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: 680 page graphics manual! Stata 13 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 13 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
Stata Graphics Syntax: A Simple Example sysuse uslifeexp.dta, clear graph twoway line le year /* OR */ twoway line le year /* OR */ line le year life expectancy 40 50 60 70 80 1900 1920 1940 1960 1980 2000 Year 4
Using Schemes line le year, scheme(s1mono) life expectancy 40 50 60 70 80 1900 1920 1940 1960 1980 2000 Year 80 line le year, scheme(economist) /* to see list of scheme names: graph query, schemes 70 60 50 life expectancy */ to change default scheme: set scheme schemename 40 1900 1920 1940 Year 1960 1980 2000 5
Multiple Dependent Variables line le_wmale le_wfemale le_bmale le_bfemale year 30 40 50 60 70 80 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 line le_wmale le_wfemale le_bmale le_bfemale year ///, text(32 1920 {bf:1918} {it:influenza} Pandemic", place(3)) 30 40 50 60 70 80 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 7
Overlaying Two-Way Plot Types scatter le year if year >= 1950 lfit le year if year >= 1950 /* OR */ scatter /// le year if year >= 1950 /// lfit le year if year >= 1950 /* OR */ twoway /// (scatter le year if year >= 1950) /// (lfit le year if year >= 1950) /* OR */ #delimit ; twoway (scatter le year if year >= 1950) (lfit le year if year >= 1950); #delimit cr 68 70 72 74 76 1950 1960 1970 1980 1990 2000 Year life expectancy Fitted values 8
Overlaying Two-Way Plot Types scatter le year if year >= 1925 /// lfit le year if year >= 1925 & /// year < 1950 /// lfit le year if year >= 1950 /* OR */ twoway /// (scatter le year if year >= 1925) /// (lfit le year if year >= 1925 & /// year < 1950) /// (lfit le year if year >= 1950) /* OR */ 55 60 65 70 75 1920 1940 1960 1980 2000 Year #delimit ; scatter le year if year >= 1925 lfit le year if year >= 1925 & year < 1950 lfit le year if year >= 1950; #delimit cr life expectancy Fitted values Fitted values 9
Overlaying Two-Way Plot Types #delimit ; scatter le_male le_female year if year >= 1950 lfit le_male year if year >= 1950 lfit le_female year if year >= 1950; #delimit cr 65 70 75 80 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 #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); #delimit cr US Male and Female Life Expectancy, 1950-2000 65 70 75 80 Female Male 1950 1960 1970 1980 1990 2000 Year 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(" by access to safe water, 1998") ytitle("") xtitle("") legend(ring(0) pos(5) order(2 "Linear fit" 1 "95% CI")); #delimit cr by access to safe water, 1998 50 60 70 80 Linear fit 95% CI 20 40 60 80 100 12
Markers Labels and Subtitles twoway (lfitci lexp safewater if region == 2) /* North America */ (scatter lexp safewater if region == 2, mlabel(country)),title(" by access to safe water, 1998") subtitle("north America") ytitle("") xtitle("") legend(ring(0) pos(5) order(2 "Linear fit" 1 "95% CI")); by access to safe water, 1998 North America 50 60 70 80 Haiti Canada CubaPuerto Rico Jamaica Panama Trinidad and Tobago Mexico Dominican Republic El Salvador Honduras Nicaragua Guatemala Linear fit 95% CI 20 40 60 80 100 13
Position of Marker Labels by access to safe water, 1998 North America Haiti Dominican Republic Honduras El Salvador generate pos = 12 if country == "Panama" replace pos = 12 if country == "Honduras" 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(" by access to safe water, 1998") subtitle("north America") ytitle("") xtitle("") legend(ring(0) pos(5) order(2 "Linear fit" 1 "95% CI")) plotregion(margin(r+10)); #delimit cr 50 60 70 80 Jamaica Trinidad and Tobago Guatemala Linear fit Cuba Panama Mexico Nicaragua 20 40 60 80 100 Canada Puerto Rico 95% CI 14
Position of Marker Labels #delimit ; twoway (scatter lexp safewater if region == 2 region == 3,mlabel(country)),title(" by access to safe water, 1998") subtitle("north and South America") ytitle("") xtitle("") plotregion(margin(r+10)); #delimit cr 55 60 65 70 75 80 by access to safe water, 1998 North and South America Haiti Paraguay Canada CubaPuerto Rico Jamaica Chile Panama Uruguay Argentina Venezuela Trinidad and Tobago Mexico Dominican Republic Ecuador Colombia El SalvadorHonduras Peru Nicaragua Brazil Bolivia Guatemala 20 40 60 80 100 15
Position of Marker Labels and Legend Display 55 60 65 70 75 80 replace pos = 9 if country == "Argentina" replace pos = 9 if country == "Canada" replace pos = 9 if country == "Cuba" replace pos = 9 if country == "Panama" 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(" by access to safe water, 1998") subtitle("north and South America") ytitle("") xtitle("") legend(ring(0) pos(5) order(1 "North America" 2 "South America") cols(1)); #delimit cr by access to safe water, 1998 North and South America Haiti 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 Bolivia Guatemala North America South America 20 40 60 80 100 16
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(" by access to safe water, 1998") subtitle("north and South America") ytitle("") xtitle("") legend(ring(0) pos(5) cols(1) order(1 "North America" 2 "South America" 3 "North America linear fit" 4 "South America linear fit")); 55 60 65 70 75 80 by access to safe water, 1998 North and South America Haiti 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 Bolivia Guatemala North America South America North America linear fit South America linear fit 20 40 60 80 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(" by access to safe water, 1998", color(black)) subtitle("north and South America") ytitle("") xtitle("") legend(ring(0) pos(5) cols(1) order(1 "North America" 2 "South America" 3 "North America linear fit" 4 "South America linear fit")); by access to safe water, 1998 North and South America 55 60 65 70 75 80 Haiti 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 Bolivia Guatemala North America South America North America linear fit South America linear fit 20 40 60 80 100 18
By-Graph: Separate Graphs for Each Subset of Data #delimit ; twoway scatter lexp safewater, by(region, total),ytitle("") xtitle("percent of population with access to safe water"); #delimit cr Eur & C.Asia N.A. 50 60 70 80 50 60 70 80 S.A. Total 20 40 60 80 100 20 40 60 80 100 Graphs by Region 19
By-Graph Options twoway scatter lexp safewater,by(region,total style(compact) title("life expectancy by access to safe water") note("")) ytitle("") xtitle(""); 50 60 70 80 50 60 70 80 Life expectancy by access to safe water Eur & C.Asia N.A. S.A. Total 20 40 60 80 10020 40 60 80 100 20
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("") ytitle("") ylabel(55(5)80, angle(0)); Axis Scale, Ticks and Labels Life expectancy by access to safe water Eur & C.Asia N.A. 80 75 70 65 60 55 80 75 70 65 60 S.A. Total 55 30 40 50 60 70 80 90 100 30 40 50 60 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("") xtitle("") legend(off); 80 North America Canada 75 70 65 60 Jamaica Dominican Republic El Salvador Honduras Guatemala Puerto Rico Cuba Panama Trinidad and Tobago Mexico Nicaragua 55 Haiti 20 40 60 80 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("") xtitle("") legend(off); 75 South America Argentina Venezuela Chile Uruguay 70 65 Paraguay Ecuador Brazil Colombia Peru Bolivia 60 40 50 60 70 80 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 80 75 70 65 60 55 Haiti El Salvador North America Jamaica Dominican Republic Honduras Guatemala Canada Puerto Rico Cuba Panama Trinidad Mexico and Tobago Nicaragua 20 40 60 80 100 75 70 65 60 Paraguay Bolivia South America Argentina Ecuador Brazil Venezuela Colombia Peru Chile Uruguay 40 50 60 70 80 90 24
Life expectancy by access to safe water 80 75 70 65 60 55 Haiti El Salvador North America Jamaica Dominican Republic Honduras Guatemala Canada Puerto Rico Cuba Panama Trinidad and Tobago Mexico Nicaragua 20 40 60 80 100 Combining Graphs 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); 80 South America 75 70 65 60 Paraguay Chile Uruguay Argentina Venezuela Ecuador Colombia Peru Brazil Bolivia 55 20 40 60 80 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 * New functions available in Stata 13 27