--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- name: log: d:\Stata\week3.log log type: text opened on: 23 Sep 2019, 16:08:36 . sysuse auto,clear (1978 Automobile Data) . ************************** . *** One variable graphs . ************************** . . *** Histogram . // basic command: . histogram mpg (bin=8, start=12, width=3.625) . // defaut is density plots, to plot fraction and frequency: . histogram mpg, frequency (bin=8, start=12, width=3.625) . histogram mpg, fraction (bin=8, start=12, width=3.625) . histogram mpg, percent (bin=8, start=12, width=3.625) . // add a normal density to the graph . histogram mpg, normal (bin=8, start=12, width=3.625) . // add a kernel density to the graph . histogram mpg, kdensity (bin=8, start=12, width=3.625) . // For discrete variables: . histogram rep78, percent discrete width(.5) (start=1, width=.5) . histogram foreign, percent discrete (start=0, width=1) . . *** 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) (file g1.gph saved) . twoway scatter price weight,saving(g2,replace) (file g2.gph saved) . // combine multiple graphs . graph combine g1.gph g2.gph . // export graphs . graph export graph.png,width(2048) replace (file graph.png written in PNG format) . . ************************** . *** Before you go . ************************** . log close name: log: d:\Stata\week3.log log type: text closed on: 23 Sep 2019, 16:08:51 ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------