📄 graph_twoway.hlp
字号:
{phang2}{cmd:. graph twoway (scatter y1 x) (scatter y2 x)}{p_end}
{pstd}
If we wanted y1 versus x plotted with solid circles and y2 versus x plotted
with hollow circles, formally we would type
{phang2}{cmd:. graph twoway (scatter y1 x, ms(O)) (scatter y2 x, ms(Oh))}{p_end}
{pstd}
If we wanted y1 versus x plotted with solid circles and wanted a line graph
for y2 versus x, formally we would type
{phang2}{cmd:. graph twoway (scatter y1 x, ms(O)) (line y2 x, sort)}{p_end}
{pstd}
The {cmd:sort} option is included under the assumption that the data are not
already sorted by x.
{pstd}
We have shown the formal way to type each of our requests, but nobody types
that. First, most users omit the {cmd:graph}:
{phang2}{cmd:. twoway (scatter y1 x) (scatter y2 x)}{p_end}
{phang2}{cmd:. twoway (scatter y1 x, ms(O)) (scatter y2 x, ms(Oh))}{p_end}
{phang2}{cmd:. twoway (scatter y1 x, ms(O)) (line y2 x, sort)}{p_end}
{pstd}
Second, most people use the {cmd:||}-separator notation rather than the
{cmd:()}-binding notation:
{phang2}{cmd:. twoway scatter y1 x || scatter y2 x}{p_end}
{phang2}{cmd:. twoway scatter y1 x, ms(O) || scatter y2 x, ms(Oh)}{p_end}
{phang2}{cmd:. twoway scatter y1 x, ms(O) || line y2 x, sort}{p_end}
{pstd}
Third, most people now omit the {cmd:twoway}:
{phang2}{cmd:. scatter y1 x || scatter y2 x}{p_end}
{phang2}{cmd:. scatter y1 x, ms(O) || scatter y2 x, ms(Oh)}{p_end}
{phang2}{cmd:. scatter y1 x, ms(O) || line y2 x, sort}{p_end}
{pstd}
And finally, most people quickly realize that {cmd:scatter} allows we to plot
more than one {it:y} variable against the same {it:x} variable:
{phang2}{cmd:. scatter y1 y2 x}{p_end}
{phang2}{cmd:. scatter y1 y2 x, ms(O Oh)}{p_end}
{phang2}{cmd:. scatter y1 x, ms(O) || line y2 x, sort}{p_end}
{pstd}
Note that the third example did not change: in that example, we are combining
a scatterplot and a line plot. Actually, in this particular case, there is
a way we can combine that, too:
{phang2}{cmd:. scatter y1 y2 x, ms(O i) connect(. l)}{p_end}
{pstd}
That we can combine {cmd:scatter} and {cmd:line} just happens to be an oddity
of the examples we picked. What is important to understand is that there is
nothing wrong with any of the above ways of typing our request, and sometimes
the wordier syntaxes are the only way to obtain what we want. If we wanted to
graph y1 versus x1 and y2 versus x2, the only way to type that is
{phang2}{cmd:. scatter y1 x1 || scatter y2 x2}{p_end}
{pstd}
or to type the equivalent in one of the wordier syntaxes above it.
We have to do this because {helpb scatter}
draws a scatterplot against a single {it:x} variable. Therefore, if we want two
different {it:x} variables, we need two different scatters.
{pstd}
In any case, we will often refer to the {cmd:graph} {cmd:twoway} command, even
though, when we give the command, we will seldom type the {cmd:graph}, and
mostly, we will not type the {cmd:twoway} either.
{marker remarks3}{...}
{title:Multiple if and in restrictions}
{pstd}
Each {it:plot} may have its own {cmd:if} {it:exp} and {cmd:in} {it:range}
restrictions:
{p 8 17 2}{cmd:. twoway (scatter mpg weight if foreign, msymbol(O))}{break}
{cmd:(scatter mpg weight if !foreign, msymbol(Oh))}
{pstd}
Multiple {it:plots} in a single {cmd:graph} {cmd:twoway} command draw one
graph with multiple things plotted in it. The above will produce a scatter of
{cmd:mpg} versus {cmd:weight} for foreign cars (making the points with solid
circles) and a scatter of {cmd:mpg} versus {cmd:weight} for domestic cars (using
hollow circles).
{pstd}
In addition, the {cmd:graph} {cmd:twoway} command itself can have
{cmd:if} {it:exp} and {cmd:in} {it:range} restrictions:
{p 8 17 2}{cmd:. twoway (scatter mpg weight if foreign, msymbol(O))}{break}
{cmd:(scatter mpg weight if !foreign, msymbol(Oh)) if mpg>20}
{pstd}
The {cmd:if} {cmd:mpg>20} restriction will apply to both scatters.
{pstd}
We have chosen to show these two examples using the {cmd:()}-binding notation
because it makes the scope of each {cmd:if} {it:exp} so clear. In
{cmd:||}-separator notation, the commands would read
{p 8 17 2}{cmd:. twoway scatter mpg weight if foreign, msymbol(O) ||}{break}
{cmd:scatter mpg weight if !foreign, msymbol(Oh)}{p_end}
{pin}and{p_end}
{p 8 17 2}{cmd:. twoway scatter mpg weight if foreign, msymbol(O) ||}{break}
{cmd:scatter mpg weight if !foreign, msymbol(Oh) || if mpg>20}
{pstd}
or even
{p 8 18 2}{cmd:. scatter mpg weight if foreign, msymbol(O) ||}{break}
{cmd:scatter mpg weight if !foreign, msymbol(Oh)}{p_end}
{pin}and{p_end}
{p 8 18 2}{cmd:. scatter mpg weight if foreign, msymbol(O) ||}{break}
{cmd:scatter mpg weight if !foreign, msymbol(Oh) || if mpg>20}
{pstd}
We may specify {cmd:graph} {cmd:twoway} restrictions only, of course:
{phang2}{cmd:. twoway (scatter mpg weight) (lfit mpg weight) if !foreign}
{phang2}{cmd:. scatter mpg weight || lfit mpg weight || if !foreign}
{marker remarks4}{...}
{title:twoway and plot options}
{pstd}
{cmd:graph} {cmd:twoway} allows options, and the individual {it:plots} allow
options. For instance, {cmd:graph} {cmd:twoway} allows the {cmd:saving()}
option, and {helpb scatter} allows the {cmd:msymbol()}
option, which specifies the marker symbol to be used. Nevertheless, we
do not have to keep track of which option belongs to which. If we type
{phang2}{cmd:. scatter mpg weight, saving(mygraph) msymbol(Oh)}
{pstd}
the results will be the same as if we more formally typed
{phang2}{cmd:. twoway (scatter mpg weight, msymbol(Oh)), saving(mygraph)}
{pstd}
Similarly, we could type
{phang2}{cmd:. scatter mpg weight, msymbol(Oh) || lfit mpg weight, saving(mygraph)}{p_end}
{pstd}or{p_end}
{phang2}{cmd:. scatter mpg weight, msymbol(Oh) saving(mygraph) || lfit mpg weight}
{pstd}
and, either way, the results would be the same as if we typed
{p 8 17 2}{cmd:. twoway (scatter mpg weight, msymbol(Oh))}{break}
{cmd:(lfit mpg weight), saving(mygraph)}
{pstd}
We may specify a {cmd:graph} {cmd:twoway} option "too deeply", but we cannot
go the other way. The following is an error:
{phang2}{cmd:. scatter mpg weight || lfit mpg weight ||, msymbol(Oh) saving(mygraph)}
{pstd}
It is an error because we specified a {cmd:scatter} option where only a
{cmd:graph} {cmd:twoway} option may be specified, and given what we typed,
there is insufficient information for {cmd:graph} {cmd:twoway} to determine
for which {it:plot} we meant the {cmd:msymbol()} option. Even when there is
sufficient information (say that option {cmd:msymbol()} were not allowed by
{cmd:lfit}), it would still be an error. {cmd:graph} {cmd:twoway} can reach
in and pull out its options, but it cannot take from its options and
distribute them back to the individual {it:plots}.
{title:Also see}
{psee}
Manual: {bf:[G] graph twoway}
{psee}
Online:
{helpb twoway area},
{helpb twoway bar},
{helpb twoway connected},
{helpb twoway dot},
{helpb twoway dropline},
{helpb twoway fpfit},
{helpb twoway fpfitci},
{helpb twoway function},
{helpb twoway histogram},
{helpb twoway kdensity},
{helpb twoway lfit},
{helpb twoway lfitci},
{helpb line},
{helpb twoway lowess},
{helpb twoway mband},
{helpb twoway mspline},
{helpb twoway qfit},
{helpb twoway qfitci},
{helpb twoway rarea},
{helpb twoway rbar},
{helpb twoway rcap},
{helpb twoway rcapsym},
{helpb twoway rconnected},
{helpb twoway rline},
{helpb twoway rscatter},
{helpb scatter},
{helpb twoway scatteri},
{helpb twoway spike},
{helpb twoway sunflower},
{helpb tsline},
{helpb tsrline}
{p_end}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -