📄 graph_intro.hlp
字号:
||,
ytitle( "", axis(2) )
xtitle( "", axis(2) )
xlabel( 1918, axis(2) )
ylabel( 0(5)20, axis(2) grid gmin angle(horizontal) )
ylabel( 0 20(10)80, gmax angle(horizontal) )
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") ){txt}
{pstd}
We started with the first graph we showed you,
{cmd:. twoway line le_wm year || line le_bm year}
{pstd}
and then, to emphasize the comparison of life expectancy
for whites and blacks, we added the difference,{cmd}
. generate diff = le_wm - le_bm
. twoway line le_wm year,
|| line le_bm year
|| line diff year{txt}
{pstd}
and then, to emphasize the linear trend in the difference, we added
"{cmd:lfit} {cmd:diff} {cmd:year}",
{cmd}. twoway line le_wm year,
|| line le_bm year
|| line diff year,
|| lfit diff year{txt}
{pstd}
and then we added options to make the graph look more like what we wanted.
We introduced the options one at a time. It was rather fun, really.
As our command grew, we switched to using the Do-file Editor, where
we could add an option and hit the {bf:Do} button to see where we were. Since
the command was so long, when we opened the do-file editor, we typed on the
first line
{cmd:#delimit ;}
{pstd}
and we typed on the last line
{cmd:;}
{pstd}
and then we typed our ever-growing command in between.
{pstd}
Note that many of the options we used above are common to most of the graph
families, including {cmd:twoway}, {cmd:bar}, {cmd:box}, {cmd:dot}, and
{cmd:pie}. If you understand how the {cmd:title()} or {cmd:legend()} option
is used with one family, you can apply that knowledge to all graphs,
because these options work the same across families.
{pstd}
While we are on the subject of life expectancy, using another dataset, we
drew
{it:({stata "gr_example2 markerlabel3":click to run})}
{* graph markerlabel3}{...}
{pstd}
See {it:{help marker_label_options}} for an explanation of how we did this.
Staying with life expectancy, we produced
{it:({stata "gr_example2 combine4":click to run})}
{* graph combine4}{...}
{pstd}
which we drew by separately drawing three rather easy graphs{cmd}
. twoway scatter lexp loggnp,
yscale(alt) xscale(alt)
xlabel(, grid gmax) saving(yx)
. twoway histogram lexp, fraction
xscale(alt reverse) horiz saving(hy)
. twoway histogram loggnp, fraction
yscale(alt reverse)
ylabel(,nogrid)
xlabel(,grid gmax) saving(hx){txt}
{pstd}
and then combining them into one:
{cmd}. graph combine hy.gph yx.gph hx.gph,
hole(3)
imargin(0 0 0 0) grapharea(margin(l 22 r 22))
title("Life expectancy at birth vs. GNP per capita")
note("Source: 1998 data from The World Bank Group"){txt}
{pstd}
See {helpb graph combine} for more information.
{pstd}
Returning to our tour, {cmd:twoway,} {cmd:by()} can produce graphs that look like this
{cmd:. sysuse auto, clear}
{cmd:. scatter mpg weight, by(foreign, total row(1))}
{it:({stata "gr_example auto: scatter mpg weight, by(foreign, total row(1))":click to run})}
{* graph mpgweightbyt1r}{...}
{pstd}
or this
{cmd:. scatter mpg weight, by(foreign, total col(1))}
{it:({stata "gr_example auto: scatter mpg weight, by(foreign, total col(1))":click to run})}
{* graph mpgweightbyt1c}{...}
{pstd}
or this
{cmd:. scatter mpg weight, by(foreign, total)}
{it:({stata "gr_example auto: scatter mpg weight, by(foreign, total)":click to run})}
{* graph mpgweightbyt}{...}
{pstd}
See {it:{help by_option}}.
{pstd}
{cmd:by()} is another of those options that is common across all graph
families. If you know how to use it one one type of graph, then you know how
to use it on any type of graph.
{pstd}
There are lots of plottypes within the {cmd:twoway} family, including areas,
bars, spikes, dropped lines, and dots. Just to illustrate a few:
{cmd}. sysuse sp500, clear
. replace volume = volume/1000
. twoway
rspike hi low date ||
line close date ||
bar volume date, barw(.25) yaxis(2) ||
in 1/57
, yscale(axis(1) r(900 1400))
yscale(axis(2) r( 9 45))
ylabel(, axis(2) grid)
ytitle(" Price -- High, Low, Close")
ytitle(" Volume (millions)", axis(2) bexpand just(left))
legend(off)
subtitle("S&P 500", margin(b+2.5))
note("Source: Yahoo!Finance and Commodity Systems, Inc."){txt}
{it:({stata "gr_example2 tworspike":click to run})}
{* graph tworspike}{...}
{pstd}
The above graph is explained in {helpb twoway rspike}. See
{help graph twoway} for a listing of all available {cmd:twoway} plottypes.
{pstd}
Moving outside the {cmd:twoway} family, {cmd:graph} can draw scatterplot
matrices, box plots, pie charts, and bar and dot plots. Here are examples of
each.
{pstd}
A scatterplot matrix of the variables {cmd:popgr}, {cmd:lexp}, {cmd:lgnppc},
and {cmd:safe}:
{cmd:. sysuse lifeexp, clear}
{cmd:. generate lgnppc = ln(gnppc)}
{phang2}
{cmd:. graph matrix popgr lexp lgnppc safe}{p_end}
{it:({stata "gr_example2 matrix3a":click to run})}
{* graph matrix3a}{...}
{pstd}
Or, with grid lines and more axis labels:
{cmd}. graph matrix popgr lexp lgnppc safe,
maxes(ylab(#4, grid) xlab(#4, grid))}{txt}
{it:({stata "gr_example2 matrix3":click to run})}
{* graph matrix3}{...}
{pstd}
See {helpb graph matrix}.
{pstd}
A box plot of blood pressure, variable {cmd:bp}, over each group in the
variable {cmd:when} and each group in the variable {cmd:sex}:
{cmd:. sysuse bplong, clear}
{cmd:. graph box bp, over(when) over(sex)}
{it:({stata "gr_example2 grbox1a":click to run})}
{* graph grbox1a}{...}
{pstd}
Or, for a graph with complete titles:
{cmd}. graph box bp, over(when) over(sex)
ytitle("Systolic blood pressure")
title("Response to Treatment, by Sex")
subtitle("(120 Preoperative Patients)" " ")
note("Source: Fictional Drug Trial, StataCorp, 2003"){txt}
{it:({stata "gr_example2 grbox1":click to run})}
{* graph grbox1}{...}
{pstd}
See {helpb graph box}.
{pstd}
A pie chart showing the proportions of the variables {cmd:sales},
{cmd:marketing}, {cmd:research}, and {cmd:development}.
{cmd:. graph pie sales marketing research development}
{it:({stata "gr_example2 pie1a":click to run})}
{* graph grpie1a}{...}
{pstd}
Or, for a graph with nice titles and better labeling of the pie slices:
{cmd:. graph pie sales marketing research development,}
{cmd:plabel(_all name, size(*1.5) color(white))}
{cmd:legend(off)}
{cmd:plotregion(lstyle(none))}{col 66}
{cmd:title("Expenditures, XYZ Corp.")}
{cmd:subtitle("2002")}
{cmd:note("Source: 2002 Financial Report (fictional data)")}
{it:({stata "gr_example2 pie1":click to run})}
{* graph grpie1}{...}
{pstd}
See {helpb graph pie}.
{pstd}
A horizontal bar chart of average wages over each group in the variables
{cmd:smsa}, {cmd:married}, and {cmd:collgrad}:
{cmd:. sysuse nlsw88, clear}
{phang2}
{cmd:. graph bar wage, over(smsa) over(married) over(collgrad)}{p_end}
{it:({stata "gr_example2 grbar5b0":click to run})}
{* graph grbar5b0}{...}
{pstd}
Or, for a prettier graph, with overlapping bars, titles, and better labels:
{cmd}. graph bar wage,
over( smsa, descend gap(-30) )
over( married )
over( collgrad, relabel(0 "Not college graduate"
1 "College graduate" ) )
ytitle("")
title("Average Hourly Wage, 1988, Women Aged 34-46")
subtitle("by College Graduation, Marital Status,
and SMSA residence")
note("Source: 1988 data from NLS, U.S. Dept of Labor,
Bureau of Labor Statistics"){txt}
{it:({stata "gr_example2 grbar5b":click to run})}
{* graph grbar5b}{...}
{pstd}
A vertical bar chart of private versus public spending over countries:
{cmd:. sysuse educ99gdp, clear}
{cmd:. generate total = private + public}
{cmd:. graph hbar (asis) public private , over(country)}
{it:({stata "gr_example2 grbar7a":click to run})}
{* graph grbar7a}{...}
{pstd}
Or, the same information with stacked bars, an informative sorting of total
spending, and nice titles.
{cmd}. graph hbar (asis) public private,
over(country, sort(total) descending)
stack
title("Spending on tertiary education as % of GDP,
1999", span position(11) )
subtitle(" ")
note("Source: OECD, Education at a Glance 2002", span){txt}
{it:({stata "gr_example2 grbar7":click to run})}
{* graph grbar7}{...}
{pstd}
See {helpb graph bar}.
{pstd}
A dot chart of average hourly over occupation, variable {cmd:occ}, with
separate subgraphs for college graduates and not college graduates, variable
{cmd:collgrad}:
{cmd:. sysuse nlsw88, clear}
{cmd:. graph dot wage, over(occ) by(collgrad)}
{it:({stata gr_example2 grdotby0:click to run})}
{* graph grdotby0}{...}
{pstd}
Or, for a plot that orders the occupations by wage and has nice titles:
{cmd}. graph dot wage,
over(occ, sort(1))
by(collgrad,
title("Average hourly wage, 1988, women aged 34-46", span)
subtitle(" ")
note("Source: 1988 data from NLS, U.S. Dept. of Labor,
Bureau of Labor Statistics", span)
){txt}
{it:({stata gr_example2 grdotby:click to run})}
{* graph grdotby}{...}
{pstd}
See {helpb graph dot}.
{pstd}
Have fun. Follow our advice in the {hi:Suggested reading order} above: turn to
{helpb graph}, then {helpb twoway}, and then {helpb scatter}.
{marker menus}{...}
{title:Using the menus}
{pstd}
In addition to using the command-line interface, you can access most of
{cmd:graph}'s features by Stata's pull-down menus. To start, load a dataset,
pull down {bf:Graphics}, and select what interests you.
{pstd}
When you have finished filling in the dialog box (do not forget to click on
the tabs{hline 2}lots of useful features are hidden there!), rather than click
{bf:OK}, click {bf:Submit}. This way, once the graph appears, you can easily
modify it and click {bf:Submit} again.
{pstd}
Feel free to experiment. Clicking {bf:Submit} (or {bf:OK}) never hurts; if
you have left a required field blank, you will be told. The dialog boxes
make it easy to spot what you can change.
{marker graph7}{...}
{title:A note for those familiar with "old" graphics}
{pstd}
This section is intended for users of Stata versions earlier than Stata 8.
{pstd}
If you are only familiar with Stata graphics prior to Stata 8, what's new?
Everything. There is not one little bit that is not new, even if it seems
familiar.
{pstd}
Before you panic, let us tell you that all the old graphics are still
in Stata. If you type
{cmd:. graph7} ...
{pstd}
or
{cmd:. gr7} ...
{pstd}
you will be back to using the old {cmd:graph} command; see {helpb graph7}.
Moreover, the old {cmd:graph} command is still invoked under version control;
see {helpb version}. If you set your version to 7.0 or earlier,
{cmd:graph} behaves the way it did in that version, which means that old
do-files and ado-files continue to work.
{pstd}
One new feature requires some adjustment. What used to be called symbols are
now called markers, and marker symbols are the shapes of the markers. Thus
you no longer specify the {cmd:symbol()} or {cmd:s()} option; you specify the
{cmd:msymbol()} or {cmd:ms()} option. In addition, the old {cmd:s(.)} for
specifying the dot symbol is now {cmd:ms(p)} ({cmd:p} stands for point).
{cmd:ms(.)} means to use the default.
{title:Also see}
{psee}
Manual: {bf:[G] graph intro}
{psee}
Online: {helpb graph}, {help graph other}
{p_end}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -