Stata 15 help for line

[G-2] graph twoway line -- Twoway line plots

Syntax

[twoway] line varlist [if] [in] [, options]

where varlist is

y_1 [y_2 [...]] x

options Description ------------------------------------------------------------------------- connect_options change look of lines or connecting method

axis_choice_options associate plot with alternative axis

twoway_options titles, legends, axes, added lines and text, by, regions, name, aspect ratio, etc. ------------------------------------------------------------------------- connect_options discusses options for one y versus one x; see connect_options in [G-2] graph twoway scatter when plotting multiple ys against one x.

Menu

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

Description

line draws line plots.

line is a command and a plottype as defined in [G-2] graph twoway. Thus the syntax for line is

. graph twoway line ...

. twoway line ...

. line ...

Being a plottype, line may be combined with other plottypes in the twoway family, as in

. twoway (line ...) (scatter ...) (lfit ...) ...

which can equivalently be written as

. line ... || scatter ... || lfit ... || ...

Options

connect_options specify how the points forming the line are connected and the look of the lines, including pattern, width, and color; see [G-3] connect_options.

[G-3] connect_options discusses options for one y versus one x, see connect_options in [G-2] graph twoway scatter when plotting multiple ys against one x.

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:

Oneway equivalency of line and scatter Typical use Advanced use Cautions

Oneway equivalency of line and scatter

line is similar to scatter, the differences being that by default the marker symbols are not displayed and the points are connected:

Default msymbol() option: msymbol(none ...)

Default connect() option: connect(l ...)

Thus you get the same results typing

. line yvar xvar

as typing

. scatter yvar xvar, msymbol(none) connect(l)

You can use scatter in place of line, but you may not use line in place of scatter. Typing

. line yvar xvar, msymbol(O) connect(none)

will not achieve the same results as

. scatter yvar xvar

because line, while it allows you to specify the marker_option msymbol(), ignores its setting.

Typical use

line draws line charts:

. sysuse uslifeexp

. line le year (click to run)

Line charts work well with time-series data. With other datasets, lines are often used to show predicted values and confidence intervals:

. sysuse auto, clear

. quietly regress mpg weight

. predict hat

. predict stdf, stdf

. generate lo = hat - 1.96*stdf

. generate hi = hat + 1.96*stdf

. scatter mpg weight || line hat lo hi weight, pstyle(p2 p3 p3) sort (click to run)

Do not forget to include the sort option when the data are not in the order of the x variable, as they are not above. We also included pstyle(p2 p3 p3) to give the lower and upper confidence limit lines the same look; see Appendix: Styles and composite styles under Remarks in [G-2] graph twoway scatter.

Because line is scatter, we can use any of the options allowed by scatter. Below we return to the U.S. life expectancy data and graph black and white male life expectancies, along with the difference, specifying many options to create an informative and visually pleasing graph:

. sysuse uslifeexp, clear

. generate diff = le_wm - le_bm

. label var diff "Difference"

. line le_wm year, yaxis(1 2) xaxis(1 2) || line le_bm year || line diff year || lfit diff year ||, ylabel(0(5)20, axis(2) gmin angle(horizontal)) ylabel(0 20(10)80, gmax angle(horizontal)) ytitle("", axis(2)) xlabel(1918, axis(2)) xtitle("", axis(2)) ylabel(, axis(2) grid) ytitle("Life expectancy at birth (years)") title("White and black life expectancy") subtitle("USA, 1900-1999") note("Source: National Vital Statistics, Vol 50, No. 6" "(1918 dip caused by 1918 Influenza Pandemic)") (click to run)

See [G-2] graph twoway scatter.

Advanced use

The above graph would look better if we shortened the descriptive text used in the keys. Below we add

legend(label(1 "White males") label(2 "Black males"))

to our previous command:

. line le_wm year, yaxis(1 2) xaxis(1 2) || line le_bm year || line diff year || lfit diff year ||, ylabel(0(5)20, axis(2) gmin angle(horizontal)) ylabel(0 20(10)80, gmax angle(horizontal)) ytitle("", axis(2)) xlabel(1918, axis(2)) xtitle("", axis(2)) ylabel(, axis(2) grid) ytitle("Life expectancy at birth (years)") title("White and black life expectancy") subtitle("USA, 1900-1999") note("Source: National Vital Statistics, Vol 50, No. 6" "(1918 dip caused by 1918 Influenza Pandemic)") legend(label(1 "White males") label(2 "Black males")) (click to run)

We might also consider moving the legend to the right of the graph, which we can do by adding

legend(col(1) pos(3))

resulting in

. line le_wm year, yaxis(1 2) xaxis(1 2) || line le_bm year || line diff year || lfit diff year ||, ylabel(0(5)20, axis(2) gmin angle(horizontal)) ylabel(0 20(10)80, gmax angle(horizontal)) ytitle("", axis(2)) xlabel(1918, axis(2)) xtitle("", axis(2)) ylabel(, axis(2) grid) ytitle("Life expectancy at birth (years)") title("White and black life expectancy") subtitle("USA, 1900-1999") note("Source: National Vital Statistics, Vol 50, No. 6" "(1918 dip caused by 1918 Influenza Pandemic)") legend(label(1 "White males") label(2 "Black males")) legend(col(1) pos(3)) (click to run)

See [G-3] legend_options for more information about dealing with legends.

Cautions

Be sure that the data are in the order of the x variable, or specify line's sort option. If you do neither, you will get something that looks like the scribblings of a child:

. sysuse auto, clear

. line mpg weight (click to run)


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