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

__Syntax__

__tw__**oway** **function** [[**y**]=] *f*(**x**) [*if*] [*in*] [**,** *options*]

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

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

__hor__**izontal** draw plot horizontally

__yvarf__**ormat(****%***fmt***)** display format for **y**
__xvarf__**ormat(****%***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 *exp*ression 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*.