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.

*Related*

Filed under: Graphics Tagged: | function, graph, marker symbols, twoway

Tom, on 29 April 2011 at 4:05 AM said:This is great – especially for black and white graphics – you can only have so many slightly-less grey dashed lines.

For some reason though, when I try this, I get an error regarding the n(20). I’m running Stata 10, and wondering if this is the reason.

For instance, if I run this:

.twoway (line emplshr year if sfla==1 & indcode==800, recast(connected) msymbol(O) n(8) lc(gs2))

I get this error–> option n() not allowed

Just curious if there is anything I can about this, short of getting Stata 11!

Thanks.

Mitch Abdon, on 2 May 2011 at 10:10 AM said:Hi Tom,

I tried “function y = x^3, range(-2 2) recast(connected) msymbol(T) n(20)” on Stata 10 and it worked.

In your case, you may also try:

tw scatter mplshr year if sfla==1 & indcode==800, sort connect(l) msymbol(O) lc(gs2)

Hope this helps..

Mitch

Tom, on 2 May 2011 at 9:23 PM said:Mitch:

Thanks for your help. I tried both graphing your function and the scatter command you suggested.

The function graph works perfectly, but the ‘

line’ doesn’t allow me to specify a smaller number of symbols. Thats’ the real issue it seems.

In other words, I can run this, no problem:

twoway (line var1 var2, recast(connected) msymbol(T))

Bur I can’t run this:

twoway (line var1 var2, recast(connected) msymbol(T) n(10))