Twoway graphs (line & scatter)
So far the created graphs had one thing in common: One axis was always numerical and the other one was defined by a categorical variable
In some cases, you may have two numerical variables and are interested in how/ whether their values are related using
- a scatter plot
- a line plot
* Twoway: Scatter plots
sysuse auto.dta, clear
// Draws scatter plot (one dot per observation)
twoway scatter price weight
// Shorthand
scatter price weight
// Change the symbol
twoway scatter price weight, msymbol(x)
// Use style of Economist magazine
twoway scatter price weight, scheme(economist)
// Add a line at a specific value (here: mean of price)
sum price
sum weight
twoway scatter price weight, yline(6165.257) xline(3019.459)
* Twoway: Line plots
clear
set obs 100
gen time=_n
gen white_noise=rnormal()
// Connects dots sequentially; e.g. in financial economics, evolution over time
twoway line white_noise time
// Compare this to the scatter plot: plots each dot by itself to assess relationship between two variables
twoway scatter white_noise time
// Similar plots to line plot:
// Combination of scatter and line plot
twoway connected white_noise time
// Line plot with shaded area underneath
twoway area white_noise time
// Spike for each observation
twoway spike white_noise time