clear all sysuse auto * Classic color-by-groups approaches * Layer multiple plot specifications sort weight twoway (line mpg weight if foreign==0) /// (line mpg weight if foreign==1), /// legend(label(1 "Domestic") label(2 "Foreign")) * Multiple y variables separate mpg, by(foreign) shortlabel line mpg0 mpg1 weight, ytitle("Mileage (mpg)") * Color is used as a third, categorical, scale * We can also think of color representing a continuous scale * <= Stata 17 this was only possible with a contour plot twoway contour mpg price weight twoway contour mpg price weight, levels(9) twoway scatter price weight, colorvar(mpg) twoway scatter price weight, colorvar(mpg) colorlevel(9) * for the Help pages on the many colorvar options, type help colorvar options * abbreviated commands are not documented twoway scatter price weight, colorv(mpg) colorlev(9) * In the Graphics manual, see section 3, Options * db twoway, then click Create ..., then select a plot type to see the * "colorvar" button twoway (scatter price weight, colorvar(mpg) colorkeysrange) * This is now an alternative way to use colors for categorical variables * - three additional options * - notice plegend reverses the order of the levels twoway scatter mpg weight, colorvar(foreign) /// colordiscrete coloruseplegend /// plegend(label(1 "Foreign") label(2 "Domestic")) * works with alternative color schemes set scheme s2mono twoway scatter price weight, colorv(mpg) colorlev(9) * for more examples see * https://www.stata.com/stata-news/news38-4/color-coded-twoway-graphs/