Load the following dataset into Stata using the sysuse command.
sysuse auto, clear
The sysuse command loads into memory an example dataset from Stata's default directory in your computer's hard drive. It is very useful for experimenting with commands. To see a list of all available example datasets, type sysuse dir.
Using the string function called word, extract automakers from the variable make, saving them in a new variable called automaker.
gen automaker = word(make,1)
To see a list of all the string functions available in Stata, type help string.
Next, collapse the data to obtain the average price and mpg across models for each automaker.
collapse (mean) avg_price = price avg_mpg = mpg, by(automaker)
Produce the graph using the twoway scatter command.
twoway scatter avg_mpg avg_price, mlabel(automaker) mlabangle(45) ytitle("Average MPG across models") xtitle("Average price across models ($)") title("Figure 2")
As long as you are working within a do-file, you can break up lengthy commands across multiple lines using three slashes.
twoway scatter avg_mpg avg_price, ///
mlabel(automaker) mlabangle(45) ///
ytitle("Average MPG across models") ///
xtitle("Average price across models ($)") ///
You can export the graph to an external format like PNG or PDF.
graph export automakers_mpg_price.png, replace