📄 graph_intro.hlp
字号:
{smcl}
{* 07mar2005}{...}
{cmd:help graph intro}
{hline}
{title:Title}
{p2colset 5 24 26 2}{...}
{p2col :{hi:[G] graph intro} {hline 2}}Introduction to graphics{p_end}
{p2colreset}{...}
{title:Remarks}
{pstd}
Remarks are presented under the headings
{hi:{help graph intro##reading:Suggested reading order}}
{hi:{help graph intro##tour:A quick tour}}
{hi:{help graph intro##menus:Using the menus}}
{hi:{help graph intro##graph7:A note for those familiar with "old" graphics}}
{marker reading}{...}
{title:Suggested reading order}
{pstd}
We recommend that you read the entries in this manual in the following order:
Read {help graph intro##tour:A QUICK TOUR} below and then ...
Entry{col 30}description
{hline 70}
{helpb graph}{...}
{col 30}Overview of the {cmd:graph} command
{helpb twoway}{...}
{col 30}Overview of the {cmd:graph twoway} command
{helpb scatter}{...}
{col 30}Overview of the {cmd:graph twoway scatter} command
{hline 70}
{pstd}
When reading those sections, follow references to other entries that interest
you. They will take you to such useful topics as
Entry{col 30}description
{hline 70}
{it:{help marker_label_options}}{...}
{col 30}Options for specifying marker labels
{it:{help by_option}}{...}
{col 30}Option for repeating graph command
{it:{help title_options}}{...}
{col 30}Options for specifying titles
{it:{help legend_option}}{...}
{col 30}Option for specifying legend
{hline 70}
{pstd}
We could list many, many more, but you will find them on your own.
Follow the references that interest you and ignore the rest. Afterward,
you will have a working knowledge of twoway graphs. Now glance at each of
Entry{col 30}description
{hline 70}
{helpb line}{...}
{col 30}Overview of the {cmd:graph twoway line} command
{helpb twoway connected}{...}
{col 30}Overview of the {cmd:graph twoway connected} command
etc.
{hline 70}
{pin}
Turn to {helpb twoway}, which lists all the different {cmd:graph}
{cmd:twoway} plottypes, and browse the manual entry for each.
{pstd}
Now is the time to understand schemes, which have a great effect on
how graphs look. You may want to specify a different scheme
before printing your graphs.
Entry{col 30}description
{hline 70}
{help schemes}{...}
{col 30}Schemes and what they do
{helpb set printcolor}{...}
{col 30}Set how colors are treated when graphs are printed
{helpb graph print}{...}
{col 30}Printing graphs the easy way
{helpb graph export}{...}
{col 30}Exporting graphs to other file formats
{hline 70}
{pstd}
Now you are an expert on the {cmd:graph} {cmd:twoway} command, and
you can even print the graphs it produces.
{pstd}
To learn about the other types of graphs, see
Entry{col 30}description
{hline 70}
{helpb graph matrix}{...}
{col 30}Scatterplot matrices
{helpb graph bar}{...}
{col 30}Bar and dot charts
{helpb graph box}{...}
{col 30}Box plots
{helpb graph dot}{...}
{col 30}Dot charts
{helpb graph pie}{...}
{col 30}Pie charts
{hline 70}
{pstd}
To learn tricks of the trade, see
Entry{col 30}description
{hline 70}
{helpb graph save}{...}
{col 30}Saving graphs to disk
{helpb graph use}{...}
{col 30}Redisplaying graphs from disk
{helpb graph describe}{...}
{col 30}Finding out what is in a .gph file
{it:{help name_option}}{...}
{col 30}How to name a graph in memory
{helpb graph display}{...}
{col 30}Redisplaying a named graph
{helpb graph dir}{...}
{col 30}Obtaining directory of named graphs
{helpb graph rename}{...}
{col 30}Renaming a named graph
{helpb graph copy}{...}
{col 30}Copying a named graph
{helpb graph drop}{...}
{col 30}Eliminating graphs in memory
{helpb discard}{...}
{col 30}Clearing memory
{hline 70}
{marker tour}{...}
{title:A quick tour}
{pstd}
{cmd:graph} is easy to use:
{cmd:. sysuse auto, clear}
{cmd:. graph twoway scatter mpg weight}
{it:({stata "gr_example auto: graph twoway scatter mpg weight":click to run})}
{* graph mpgweight}{...}
{pstd}
All the commands documented in this manual begin with the word {cmd:graph},
but in many instances the {cmd:graph} is optional. You could get the same
graph by typing
{cmd:. twoway scatter mpg weight}
{pstd}
and, in the case of {cmd:scatter}, you could omit the {cmd:twoway}, too:
{cmd:. scatter mpg weight}
{pstd}
We, however, will continue to type {cmd:twoway} to emphasize when the
graphs we are demonstrating are in the twoway family.
{pstd}
Twoway graphs can be combined with {cmd:by()}:
{cmd:. twoway scatter mpg weight, by(foreign)}
{it:({stata "gr_example auto: twoway scatter mpg weight, by(foreign)":click to run})}
{* graph mpgweightby}{...}
{pstd}
Graphs in the {cmd:twoway} family can also be overlaid. The members of the
{cmd:twoway} family are called {it:plottypes}; {cmd:scatter} is a plottype, and
another plottype is {cmd:lfit}, which calculates the linear prediction and
plots it as a line chart. When we want one plottype overlaid on
another, we combine the commands, putting {cmd:||} in between:
{cmd:. twoway scatter mpg weight || lfit mpg weight}
{it:({stata "gr_example auto: twoway scatter mpg weight || lfit mpg weight":click to run})}
{* graph grintro3}{...}
{pstd}
Another notation for this is called the {cmd:()}-binding notation:
{phang2}
{cmd:. twoway (scatter mpg weight) (lfit mpg weight)}
{pstd}
It does not matter which notation you use.
{pstd}
Overlaying can be combined with {cmd:by()}. This time, substitute
{cmd:qfitci} for {cmd:lfit}. {cmd:qfitci} plots the prediction based on a
quadratic regression, and it adds a confidence interval. Then add the
confidence interval based on the standard error of the forecast:
{phang2}
{cmd:. twoway (qfitci mpg weight, stdf) (scatter mpg weight), by(foreign)}
{p_end}
{it:({stata "gr_example auto: twoway (qfitci mpg weight, stdf) (scatter mpg weight), by(foreign)":click to run})}
{* graph grintro4}{...}
{pstd}
We used the {cmd:()}-binding notation just because it makes it easier to
see what modifies what:
{cmd:stdf} {it:is an option}
{it:of} {cmd:qfitci}
{c TLC}{hline 16}{c TRC}
{c BT} {c BT}
{cmd:. twoway (qfitci mpg weight, stdf) (scatter mpg weight), by(foreign)}
{c TT} {c BLC}{hline 23}{c BRC} {c BLC}{hline 18}{c BRC} {c TT}
{c |} {it:overlay this with this} {c |}
{c |} {c |}
{c BLC}{hline 58}{c BRC}
{cmd:by(foreign)} {it:is an option of} {cmd:twoway}
{pstd}
We could just as well have typed this command using the {cmd:||}-separator
notation,
{phang2}
{cmd:. twoway qfitci mpg weight, stdf || scatter mpg weight ||, by(foreign)}
{pstd}
and, as a matter of fact, we do not have to separate the {cmd:twoway} option
{cmd:by(foreign)} (or any other {cmd:twoway} option)
from the {cmd:qfitci} and {cmd:scatter} options, so we can type
{phang2}
{cmd:. twoway qfitci mpg weight, stdf || scatter mpg weight, by(foreign)}
{pstd}
or even
{phang2}
{cmd:. twoway qfitci mpg weight, stdf by(foreign) || scatter mpg weight}
{pstd}
All these syntax issues are discussed in {helpb twoway}. In our
opinion, the {cmd:()}-binding notation is easier to read, but the
{cmd:||}-separator notation is easier to type. You will see us using both.
{pstd}
It was not an accident that we put {cmd:qfitci} first and
{cmd:scatter} second. {cmd:qfitci} shades an area, and had we done it the
other way around, that shading would have been put right on top of our
scattered points and erased (or at least hidden) them!
{pstd}
Plots of different types or the same type may be overlaid:
{cmd:. sysuse uslifeexp, clear}
{cmd:. twoway line le_wm year || line le_bm year}
{it:({stata "gr_example uslifeexp: twoway line le_wm year || line le_bm year":click to run})}
{* graph grintro5}{...}
{pstd}
Here is a rather fancy version of the same graph:{cmd}
. generate diff = le_wm - le_bm
. label var diff "Difference"
. twoway line le_wm year, yaxis(1 2) xaxis(1 2)
|| line le_bm year
|| line diff year
|| lfit diff year
||,
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}
{it:({stata gr_example2 line3a:click to run})}
{* graph line3}{...}
{pstd}
There are a lot of options on this command! Strip away the obvious ones, such
as {cmd:title()}, {cmd:subtitle()}, and {cmd:note()}, and you are left
with{cmd}
. twoway line le_wm year, yaxis(1 2) xaxis(1 2)
|| line le_bm year
|| line diff year
|| lfit diff year
||,
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) )
legend( label(1 "White males") label(2 "Black males") ){txt}
{pstd}
Let's take the longest option first:
{cmd:ylabel( 0(5)20, axis(2) grid gmin angle(horizontal) )}
{pstd}
The first thing to note is that options have options:
{cmd:ylabel( 0(5)20, axis(2) grid gmin angle(horizontal) )}
{hline 2}{c TT}{hline 2} {hline 19}{c TT}{hline 15}
{c |} {c |}
{c BLC}{hline 36}{c BRC}
{cmd:axis(2) grid gmin angle(horizontal)}
{it:are options of} {cmd:ylabel()}
{pstd}
Now look back at our graph. It has two {it:y} axes, one on the right and
a second on the left. Typing
{cmd:ylabel( 0(5)20, axis(2) grid gmin angle(horizontal) )}
{pstd}
caused the right axis{hline 2}{cmd:axis(2)}{hline 2}to have labels at
0, 5, 10, 15, and 20{hline 2}{cmd:0(5)20}. {cmd:grid} requested grid lines
for each labeled tick on this right axis, and {cmd:gmin} forced the grid line
at 0 because, by default, {cmd:graph} does not like to draw grid lines too
close to the axis. {cmd:angle(horizontal)} made the 0, 5, 10, 15, and 20
horizontal rather than, as usual, vertical.
{pstd}
You can now guess what
{cmd:ylabel( 0 20(10)80, gmax angle(horizontal) )}
{pstd}
did. It labeled the left {it:y} axis{hline 2}{cmd:axis(1)} in the
jargon{hline 2}but we did not have to specify an {cmd:axis(1)} suboption since
that is what {cmd:ylabel()} assumes. The purpose of
{cmd:xlabel( 1918, axis(2) )}
{pstd}
is now obvious, too. That labeled a value on the second {it:x} axis.
{pstd}
So now we are left with{cmd}
. twoway line le_wm year, yaxis(1 2) xaxis(1 2)
|| line le_bm year
|| line diff year
|| lfit diff year
||,
ytitle( "", axis(2) )
xtitle( "", axis(2) )
legend( label(1 "White males") label(2 "Black males") ){txt}
{pstd}
Options {cmd:ytitle()} and {cmd:xtitle()} specify the axis titles.
We did not want titles on the second axes, so we got rid of them.
The {cmd:legend()} option,
{cmd:legend( label(1 "White males") label(2 "Black males") )}
{pstd}
merely respecified the text to be used for the first two keys. By default,
{cmd:legend()} uses the variable label, which in this case would be the labels
of variables {cmd:le_wm} and {cmd:le_bm}. In our dataset, those labels are
"Life expectancy, white males" and "Life expectancy, black males". It
was not necessary{hline 2}and undesirable{hline 2}to repeat "Life expectancy",
so we specified an option to change the label. It was either that or change
the variable label.
{pstd}
So now we are left with{cmd}
. twoway line le_wm year, yaxis(1 2) xaxis(1 2)
|| line le_bm year
|| line diff year
|| lfit diff year{txt}
{pstd}
and that is almost perfectly understandable. The {cmd:yaxis()} and
{cmd:xaxis()} options caused the creation of two {it:y} and two {it:x}
axes rather than, as usual, one.
{pstd}
Understand how we arrived at{cmd}
. twoway line le_wm year, yaxis(1 2) xaxis(1 2)
|| line le_bm year
|| line diff year
|| lfit diff year
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -