Complicated graphics like Figure 3.26 require graph commands that are many physical lines long (although Stata views the whole command as one logical line). Do-files, introduced in Chapter 2, help in writing such multi-line commands. They also make it easy to save the command for future re-use, in case we later want to modify the graph or draw it again.
The following commands, typed into Stata’s Do-file Editor and saved with the file name fig03_26.do, become a new do-file for drawing Figure 3.26.
use C:\data\lakesunwin.dta, clear graph twoway line winout year ///
|| lowess winout year, lwidth(medthick) ///
|| if year>=1887 , xlabel(1890(10)2010) legend(off) xtitle(“”) ///
ytitle(“Ice Out day of year”) ///
text(87 1905 “Early 20th century” “warming”) ///
text(130 1950 “Mid-century cooling” “(global dimming)”, ///
box margin(small) bcolor(gs11)) ///
text(125 1998 “Modern rapid” “warming”, ///
graph save Graph C:\graphs\fig03_26.gph, replace
graph export C:\graphs\fig03_26.png, as(png) replace
graph export C:\graphs\fig03_26.eps, as(eps) replace
Ending lines with /// in this do-file tells Stata that the command continues on the next physical line. The command executes only after it reaches a line that does not end with ///. An alternative way to write multiline commands is to give a #delimit ; command, which would set the end-of- line delimiter to a semicolon instead of the default Enter or carriage return (#delimit cr), as illustrated in Chapter 2.
The graph save Graph command saves the image (by default, temporarily named “Graph” as seen in the Graph window) in Stata’s .gph format. We can always specify our own temporary name for a graph, by adding to the graph command an option such as name(newgraph) or name(fig03_26). Such temporary names for graphs become important when we have several graphs currently displayed, and want to save or print a particular one. Giving a temporary name to the graph as we create it does not save that graph to disk The temporary and saved-file names need not be the same. Type help name option for more discussion.
Our do-file’s graph export commands create second and third versions of the same image in different formats. Portable Network Graphics (.png) files are bitmapped images and have a fixed resolution, but they are very compatible and easily shared through Web pages, Powerpoint presentations or other applications. Encapsulated PostScript (.eps) is a high-quality vector format often preferred for publication. Type help graph export to learn about other options for this command.
Once the graph-drawing and graph-saving commands are saved as fig03_26.do, simply typing the command
. do fig03_26
causes this do-file to execute, redrawing the graph and saving it in three formats, writing over earlier versions of those files if they exist.
Source: Hamilton Lawrence C. (2012), Statistics with STATA: Version 12, Cengage Learning; 8th edition.