Functions and marker symbols


Stata’s -graph twoway function- draws the line plot of a specified function. Example, the half of a parabola with the equation y = x^2 can be drawn by typing:

tw function y = 4*x^2


The default is that the function is drawn over the range [0,1]. If you want to draw the other side of the parabola or change the range, you can specify the range as follows:

tw function y = 4*x^2, range(-2 2)



It is easy to change the line attributes of the plot. For example, if you want to change the color, width, and pattern of the line use lcolor, lwidth, and lpattern options:

tw function y = 4*x^2, range(-2 2) lcolor(red) lwidth(medthick) lpattern(-)



These options are particularly helpful when you have many functions to plot. For example:

tw function y = x^2, range(-2 2) || ///
function y = x^3, range(-2 2) lpattern(-) || ///
function y = x^4, range(-2 2) lpattern(.-) ///
legend(label(1 “y = x{sup:2}”) ///
label(2 “y = x{sup:3}”)  ///
label(3 “y = x{sup:4}”) ///
cols(3) pos(5) ring(0) region(lcolor(none)))


[Note: For more text in graph options (e.g. bold, Greek letters, font type), see -help graph text-. See also Writing Greek letters and other symbols in graphs.]

But suppose you prefer to use marker symbols rather than line patterns to differentiate the line plots, how can you specify this option? Use the recast option (-help advanced_options-):

tw function y = x^2, range(-2 2) recast(connected) msymbol(O) || ///
function y = x^3, range(-2 2) recast(connected) msymbol(T) n(20) || ///
function y = x^4, range(-2 2) recast(connected) msymbol(S) n(20)  ///
legend(label(1 “y = x{sup:2}”) ///
label(2 “y = x{sup:3}”)  ///
label(3 “y = x{sup:4}”) ///
cols(3) pos(5) ring(0) region(lcolor(none)))


The recast option will tell Stata to treat the plot as a new plot. In our case, we specified that the new plot type is a connected graph, for which you can specify marker symbols. What is “n(#)” for? This tells Stata to draw the plot at 20 points. If this were not specified, as in the plot for y = x^2, the plot will be connected by 300 (the default) markers — ugly.