**[G-2] graph twoway kdensity** -- Kernel density plots

__Syntax__

__tw__**oway** **kdensity** *varname* [*if*] [*in*] [*weight*] [**,** *options*]

*options* Description
-------------------------------------------------------------------------
__bw__**idth(***#***)** smoothing parameter
__k__**ernel(***kernel***)** specify kernel function; default is
**kernel(epanechnikov)**

__ra__**nge(***#* *#***)** range for plot, minimum and maximum
__ra__**nge(***varname***)** range for plot obtained from *varname*
**n(***#***)** number of points to evaluate
**area(***#***)** rescaling parameter
__hor__**izontal** graph horizontally
**boundary** estimate density one **bwidth()** beyond maximum
and minimum; not allowed with **range()**

*cline_options* change look of the line

*axis_choice_options* associate plot with alternative axis

*twoway_options* titles, legends, axes, added lines and text,
by, regions, name, aspect ratio, etc.
-------------------------------------------------------------------------

*kernel* Description
-------------------------------------------------------------------------
__ep__**anechnikov** Epanechnikov kernel function; the default
**epan2** alternative Epanechnikov kernel function
__bi__**weight** biweight kernel function
__cos__**ine** cosine trace kernel function
__gau__**ssian** Gaussian kernel function
__par__**zen** Parzen kernel function
__rec__**tangle** rectangle kernel function
__tri__**angle** triangle kernel function
-------------------------------------------------------------------------

**fweight**s and **aweight**s are allowed; see weight.

__Menu__

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

__Description__

**graph** **twoway** **kdensity** plots a kernel density estimate for *varname* using
**graph twoway line**.

__Options__

**bwidth(***#***)** and **kernel(***kernel***)** specify how the kernel density estimate is
to be obtained and are in fact the same options as those specified
with the command **kdensity**; see **[R] kdensity**.

**bwidth(***#***)** specifies the smoothing parameter.

**kernel(***kernel***)** specify the kernel-weight function to be used. The
default is **kernel(epanechnikov)**.

See **[R] kdensity** for more information about these options.

All the other **graph** **twoway** **kdensity** options modify how the result is
displayed, not how it is obtained.

**range(***#* *#***)** and **range(***varname***)** specify the range of values at which the
kernel density estimates are to be plotted. The default is
**range(***m* *M***)**, where *m* and *M* are the minimum and maximum of the *varname*
specified on the **graph** **twoway** **kdensity** command.

**range(***#* *#***)** specifies a pair of numbers to be used as the minimum and
maximum.

**range(***varname***)** specifies another variable for which its minimum and
maximum are to be used.

**n(***#***)** specifies the number of points at which the estimate is evaluated.
The default is **n(300)**.

**area(***#***)** specifies a multiplier by which the density estimates are
adjusted before being plotted. The default is **area(1)**. **area()** is
useful when overlaying a density estimate on top of a histogram that
is itself not scaled as a density. For instance, if you wished to
scale the density estimate as a frequency, **area()** would be specified
as the total number of nonmissing observations.

**horizontal** specifies that the result be plotted horizontally (that is,
reflected along the identity line).

**boundary** specifies that the result be estimated for one **bwidth()** beyond
the maximum and minimum value of *varname*. **boundary** cannot be
specified with **range()**.

*cline_options* specify how the density line is rendered and its
appearance; 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__

**graph** **twoway** **kdensity** *varname* uses the **kdensity** command to obtain an
estimate of the density of *varname* and uses **graph** **twoway** **line** to plot the
result.

Remarks are presented under the following headings:

Typical use
Use with by()

__Typical use__

The density estimate is often graphed on top of the histogram:

**. sysuse lifeexp**

**. twoway histogram lexp, color(*.5) || kdensity lexp**
*(**click to run**)*

Notice the use of **graph** **twoway** **histogram**'s **color(*.5)** option to dim the
bars and make the line stand out; see **[G-4]** *colorstyle*.

Notice also the *y* and *x* axis titles: "Density/kdensity lexp" and "Life
expectancy at birth/x". The "kdensity lexp" and "x" were contributed by
the **twoway** **kdensity**. When you overlay graphs, you nearly always need to
respecify the axis titles using the *axis_title_options* **ytitle()** and
**xtitle()**; see **[G-3]** *axis_title_options*.

__Use with by()__

**graph** **twoway** **kdensity** may be used with **by()**:

**. sysuse lifeexp, clear**

**. twoway histogram lexp, color(*.5) || kdensity lexp ||, by(region)**
*(**click to run**)*