Stata 15 help for twoway function

[G-2] graph twoway function -- Twoway line plot of function

Syntax

twoway function [[y]=] f(x) [if] [in] [, options]

options Description ------------------------------------------------------------------------- range(# #) plot over x = # to # range(varname) plot over x = min to max of varname n(#) evaluate at # points; default is 300

droplines(numlist) draw lines to axis at specified x values base(#) base value for dropline(); default is 0

horizontal draw plot horizontally

yvarformat(%fmt) display format for y xvarformat(%fmt) display format for x

cline_options change look of plotted line

axis_choice_options associate plot with alternative axis

twoway_options titles, legends, axes, added lines and text, by, regions, name, aspect ratio, etc. ------------------------------------------------------------------------- All explicit options are rightmost, except horizontal, which is unique; see repeated options. if exp and in range play no role unless option range(varname) is specified. In the above syntax diagram, f(x) stands for an expression in terms of x.

Menu

Graphics > Twoway graph (scatter, line, etc.)

Description

twoway function plots y = f(x), where f(x) is some function of x. That is, you type

. twoway function y=sqrt(x)

It makes no difference whether y and x are variables in your data.

Options

range(# #) and range(varname) specify the range of values for x. In the first syntax, range() is a pair of numbers identifying the minimum and maximum. In the second syntax, range() is a variable name, and the range used will be obtained from the minimum and maximum values of the variable. If range() is not specified, range(0 1) is assumed.

n(#) specifies the number of points at which f(x) is to be evaluated. The default is n(300).

droplines(numlist) adds dropped lines from the function down to, or up to, the axis (or y=base() if base() is specified) at each x value specified in numlist.

base(#) specifies the base for the droplines(). The default is base(0). This option does not affect the range of the axes, so you may also want to specify the axis_scale_option yscale(range(#)) as well; see [G-3] axis_scale_options.

horizontal specifies that the roles of y and x be interchanged and that the graph be plotted horizontally rather than vertically (that the plotted function be reflected along the identity line).

yvarformat(%fmt) and xvarformat(%fmt) specify the display format to be used for y and x. These formats are used when labeling the axes; see [G-3] axis_label_options.

cline_options specify how the function line is rendered; see [G-3] cline_options.

axis_choice_options associate the plot with a particular y or x axis on the graph; see [G-3] axis_choice_options.

twoway_options are a set of common options supported by all twoway graphs. These options allow you to title graphs, name graphs, control axes and legends, add lines and text, set aspect ratios, create graphs over by() groups, and change some advanced settings. See [G-3] twoway_options.

Remarks

Remarks are presented under the following headings:

Typical use Advanced use 1 Advanced use 2

Typical use

You wish to plot the function y=exp(-x/6)sin(x) over the range 0 to 4pi:

. twoway function y=exp(-x/6)*sin(x), range(0 12.57) (click to run)

A better rendition of the graph above is

. twoway function y=exp(-x/6)*sin(x), range(0 12.57) yline(0, lstyle(foreground)) xlabel( 0 3.14 "{&pi}" 6.28 "2{&pi}" 9.42 "3{&pi}" 12.57 "4{&pi}") plotregion(style(none)) xsca(noline) (click to run)

yline(0, lstyle(foreground)) added a line at y=0; lstyle(foreground) gave the line the same style as used for the axes. See [G-3] added_line_options.

xlabel(0 3.14 "{&pi}" 6.28 "2{&pi}" 9.42 "3{&pi}" 12.57 "4{&pi}") labeled the x axis with the numeric values given and substituted text for the numeric values; see [G-3] axis_label_options.

plotregion(style(none)) suppressed the border around the plot region; see [G-3] region_options.

xsca(noline) suppressed the drawing of the x-axis line; see [G-3] axis_scale_options.

Advanced use 1

The following graph appears in many introductory textbooks:

. twoway function y=normalden(x), range(-4 -1.96) color(gs12) recast(area) || function y=normalden(x), range(1.96 4) color(gs12) recast(area) || function y=normalden(x), range(-4 4) lstyle(foreground) ||, plotregion(style(none)) ysca(off) xsca(noline) legend(off) xlabel(-4 "-4 sd" -3 "-3 sd" -2 "-2 sd" -1 "-1 sd" 0 "mean" 1 "1 sd" 2 "2 sd" 3 "3 sd" 4 "4 sd" , grid gmin gmax) xtitle("") (click to run)

We drew the graph in three parts: the shaded area on the left, the shaded area on the right, and then the overall function. To obtain the shaded areas, we used the advanced_option recast(area) so that, rather than the function being plotted by graph twoway line, it was plotted by graph twoway area; see [G-3] advanced_options and [G-2] graph twoway area. Concerning the overall function, we drew it last so that its darker foreground-colored line would not get covered up by the shaded areas.

Advanced use 2

function plots may be overlaid with other twoway plots. For instance, function is one way to add y=x lines to a plot:

. sysuse sp500, clear

. scatter open close, msize(*.25) mcolor(*.6) || function y=x, range(close) yvarlab("y=x") clwidth(*1.5) (click to run)

In the above, we specified the advanced_option yvarlab("y=x") so that the variable label of y would be treated as "y=x" in the construction of the legend; see [G-3] advanced_options. We specified msize(*.25) to make the marker symbols smaller, and we specified mcolor(*.6) to make them dimmer; see [G-4] relativesize and [G-4] colorstyle.


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index