************************** *** First steps ************************** cd d:/Stata // This should replaced by the working directory of your own choice set more off capture log close log using week3.txt,replace sysuse auto,clear ************************** *** One variable graphs ************************** *** Histogram // basic command: histogram mpg // defaut is density plots, to plot fraction and frequency: histogram mpg, frequency histogram mpg, fraction histogram mpg, percent // add a normal density to the graph histogram mpg, normal // add a kernel density to the graph histogram mpg, kdensity // For discrete variables: histogram rep78, percent discrete width(.5) histogram foreign, percent discrete *** Bar // basic command: graph bar price // to get price by repair records: graph bar price,by(rep78) // to plot bars by repair records on the same graph graph bar price,over(rep78) // plot over repair records and foreign graph bar price,over(foreign) over(rep78) // the variable labels look messy. Want a better version: // as a review of the last session: label define flb 0 "D" 1 "F" label values foreign flb graph bar price,over(foreign) over(rep78) // Change the look of the bars graph bar price, over(foreign) over(rep78) bar(1,color(black)) ************************** *** Twoway Graphs ************************** // basic command: twoway scatter mpg weight // add a fitted line (overlay graphs) twoway (scatter mpg weight) (lfit mpg weight) // label makers twoway (scatter mpg weight,mlabel(make)) (lfit mpg weight) // use color to indicate foreign and domestic cars: twoway (scatter mpg weight if foreign==0) (scatter mpg weight if foreign==1) // edit the legend twoway (scatter mpg weight if foreign==0) (scatter mpg weight if foreign==1), legend(label(1 "Domestic") label(2 "Foreign")) // use symbols and color twoway (scatter mpg weight if foreign==0,msymbol(O) mcolor(black)) (scatter mpg weight if foreign==1,msymbol(X) mcolor(black)), legend(label(1 "Domestic") label(2 "Foreign")) // add title to the graphs twoway (scatter mpg weight),title("Mileage and Weight") ************************** *** Save and Export ************************** // save graphs in Stata format for future edits twoway scatter mpg weight,saving(g1,replace) twoway scatter price weight,saving(g2,replace) // combine multiple graphs graph combine g1.gph g2.gph // export graphs graph export graph.png,width(2048) replace ************************** *** Before you go ************************** log close ************************************ *** Creating the graphs included in my slides ************************************ cd "d:\Dropbox\Courses\Teaching\EC204\week 3" sysuse auto,clear histogram rep78, percent discrete width(.5) graph export histogram.png,width(2048) replace label define flb 0 "D" 1 "F" label values foreign flb graph bar price,over(foreign) over(rep78) graph export bar.png,width(2048) replace graph twoway (scatter mpg weight) (lfit mpg weight) graph export twoway.png,width(2048) replace