📄 graph_bar.hlp
字号:
{pmore}
If you also plan on specifying {cmd:graph} {cmd:bar}'s or {cmd:graph}
{cmd:hbar}'s {cmd:missing} option,
{phang3}
{cmd:. graph bar} ...{cmd:,} ... {cmd:missing over(sex, relabel(}...{cmd:))}
{pmore}
then type
{cmd:. tabulate sex, missing}
{pmore}
to determine the coding. See {helpb tabulate}.
{pmore}
Relabeling the values does not change the order in which the bars are
displayed.
{pmore}
You may create multiple-line labels using quoted strings within quoted
strings:
{cmd:over(}{it:varname}{cmd:, relabel(1 `" "Male" "patients" "'}
{cmd:2 `" "Female" "patients" "'))}
{pmore}
When specifying quoted strings within quoted strings, remember to
use compound double quotes {cmd:`"} and {cmd:"'} on the outer level.
{pmore}
{cmd:relabel()} may also be specified inside {cmd:yvaroptions()}.
By default, the identity of the {it:yvars} is revealed in the legend, so
specifying {cmd:yvaroptions(relabel())} changes the legend. Because it is
the legend that is changed, using {cmd:legend(label())} is preferred; see
{hi:legending_options} above. In any case, specifying
{cmd:yvaroptions(relabel(1 "Males" 2 "Females"))}
{pmore}
changes the text that appears in the legend for the first
{it:yvar} and the second {it:yvar}. Note that {it:#} in
{cmd:relabel(}{it:#} ...{cmd:)} refers to {it:yvar} number.
In this case, you may not use the nested quotes to create multiline
labels; use the {cmd:legend(label())} option because it provides multiline
capabilities.
{phang} {cmd:label(}{it:cat_axis_label_options}{cmd:)} determines other
aspects of the look of the category labels on the {it:x} axis. With the
exception of {cmd:label(labcolor())} and {cmd:label(labsize())}, these options
are seldom specified; see {it:{help cat_axis_label_options}}.
{phang}
{cmd:axis(}{it:cat_axis_line_options}{cmd:)}
specifies how the axis line is rendered. This is a rarely specified
option. See {it:{help cat_axis_line_options}}.
{phang}
{cmd:gap(}{it:#}{cmd:)} and
{cmd:gap(*}{it:#}{cmd:)}
specify the gap between the bars in this {cmd:over()} group.
{cmd:gap(}{it:#}{cmd:)} is specified in percentage-of-bar-width units, so
{cmd:gap(67)} means two-thirds the width of a bar.
{cmd:gap(*}{it:#}{cmd:)} allows modifying the default gap.
{cmd:gap(*1.2)} would increase the gap by 20%, and {cmd:gap(*.8)} would
decrease the gap by 20%.
{pmore}
To understand the distinction between
{cmd:over(}...{cmd:,} {cmd:gap())} and option {cmd:bargap()}, consider
{phang3}
{cmd:. graph bar revenue profit, bargap(}...{cmd:) over(division, gap(}...{cmd:))}
{pmore}
{cmd:bargap()} sets the distance between the revenue and profit bars.
{cmd:over(,gap())} sets the distance between the bars for the
first division and the second division, the second division and the third,
and so on. Similarly, in
{cmd:. graph bar revenue profit, bargap(}...{cmd:)}
{cmd:over(division, gap(}...{cmd:))}
{cmd:over(year, gap(}...{cmd:))}
{pmore}
{cmd:over(division, gap())} sets the gap between divisions and
{cmd:over(year, gap())} sets the gap between years.
{phang}
{cmd:sort(}{it:varname}{cmd:)},
{cmd:sort(}{it:#}{cmd:)}, and
{cmd:sort((}{it:stat}{cmd:)} {it:varname}{cmd:)}
control how bars are ordered. See
{help graph bar##remarks10:How bars are ordered}
and {help graph bar##remarks11:Reordering the bars} under {hi:Remarks}
below.
{pmore}
{cmd:sort(}{it:varname}{cmd:)} puts the bars in the order of {it:varname};
see {help graph bar##remarks12:Putting the bars in a prespecified order}
under {hi:Remarks} below.
{pmore}
{cmd:sort(}{it:#}{cmd:)} puts the bars in height order.
{it:#} refers to the {it:yvar} number on which the ordering should be
performed;
see {help graph bar##remarks13:Putting the bars in height order} under
{hi:Remarks} below.
{pmore}
{cmd:sort((}{it:stat}{cmd:)} {it:varname}{cmd:)}
puts the bars in an order based on a calculated statistic;
see {help graph bar##remarks14:Putting the bars in a derived order} under
{hi:Remarks} below.
{phang}
{cmd:descending}
specifies that the order of the bars{hline 2}default or as specified
by {cmd:sort()}{hline 2}be reversed.
{phang}
{cmd:reverse}
specifies that the categorical scale run from maximum to minimum rather
than the default minimum to maximum. Among other things, when combined
with {cmd:bargap(}-{it:#}{cmd:)}, {cmd:reverse} causes the sequence of
overlapping to be reversed.
{title:Remarks}
{pstd}
Remarks are presented under the headings
{help graph bar##remarks1:Introduction}
{help graph bar##remarks2:Examples of syntax}
{help graph bar##remarks3:Treatment of bars}
{help graph bar##remarks4:Treatment of data}
{help graph bar##remarks5:Multiple bars (overlapping the bars)}
{help graph bar##remarks6:Controlling the text of the legend}
{help graph bar##remarks7:Multiple over()s (repeating the bars)}
{help graph bar##remarks8:Nested over()s}
{help graph bar##remarks9:Charts with many categories}
{help graph bar##remarks10:How bars are ordered}
{help graph bar##remarks11:Reordering the bars}
{help graph bar##remarks12:Putting the bars in a prespecified order}
{help graph bar##remarks13:Putting the bars in height order}
{help graph bar##remarks14:Putting the bars in a derived order}
{help graph bar##remarks15:Reordering the bars, example}
{help graph bar##remarks16:Use with by()}
{help graph bar##remarks18:History}
{marker remarks1}{...}
{title:Introduction}
{pstd}
Let us show you some bar charts:
{cmd}. sysuse citytemp
. graph bar (mean) tempjuly tempjan, over(region)
bargap(-30)
legend( label(1 "July") label(2 "January") )
ytitle("Degrees Fahrenheit")
title("Average July and January temperatures")
subtitle("by regions of the United States")
note("Source: U.S. Census Bureau, U.S. Dept. of Commerce"){txt}
{it:({stata gr_example2 grbar1:click to run})}
{* graph grbar1}{...}
{cmd}. sysuse citytemp, clear
. graph hbar (mean) tempjan, over(division) over(region) nofill
ytitle("Degrees Fahrenheit")
title("Average January temperature")
subtitle("by region and division of the United States")
note("Source: U.S. Census Bureau, U.S. Dept. of Commerce"){txt}
{it:({stata gr_example2 grbar3:click to run})}
{* graph grbar3}{...}
{cmd}. sysuse nlsw88, clear
. graph bar (mean) wage, over(smsa) over(married) over(collgrad)
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 grbar5:click to run})}
{* graph grbar5}{...}
{cmd}. sysuse educ99gdp, clear
. generate total = private + public
. graph hbar (asis) public private,
over(country, sort(total) descending) stack
title( "Spending on tertiary education as % of GDP, 1999",
span pos(11) )
subtitle(" ")
note("Source: OECD, Education at a Glance 2002", span){txt}
{it:({stata gr_example2 grbar7:click to run})}
{* graph grbar7}{...}
{pstd}
In the sections that follow, we explain how each of the above
graphs{hline 2}and others{hline 2}works.
{marker remarks2}{...}
{title:Examples of syntax}
{pstd}
Below we show you some {cmd:graph} {cmd:bar} commands and tell you what each
would do:
{p 4 8 8}
{cmd:graph bar revenue}{break}
One big bar showing average revenue.
{p 4 8 8}
{cmd:graph bar revenue profit}{break}
Two bars, one showing average revenue and the other, average profit.
{p 4 8 8}
{cmd:graph bar revenue, over(division)}{break}
{it:#_of_divisions} bars showing average revenue for each division.
{p 4 8 8}
{cmd:graph bar revenue profit, over(division)}{break}
2*{it:#_of_divisions} bars showing average revenue and average
profit for each division.
The grouping would look like this (assuming 3 divisions):
{c TLC}{c -}{c TRC} {c TLC}{c -}{c TRC} {c TLC}{c -}{c TRC}
{c |} {c |} {c |} {c |} {c |} {c |}
{c |} {c LT}{c -}{c TRC} {c |} {c LT}{c -}{c TRC} {c |} {c LT}{c -}{c TRC}
{c |} {c |} {c |} {c |} {c |} {c |} {c |} {c |} {c |}
{hline 2}{c BT}{c -}{c BT}{c -}{c BT}{hline 5}{c BT}{c -}{c BT}{c -}{c BT}{hline 5}{c BT}{c -}{c BT}{c -}{c BT}{c -}
division division division
{p 4 8 8}
{cmd:graph bar revenue, over(division) over(year)}{break}
{it:#_of_divisions}*{it:#_of_years} bars showing average revenue
for each division, repeated for each of the years. The grouping would
look like this (assuming 3 divisions and 2 years):
{c TLC}{c -}{c TRC} {c TLC}{c -}{c TRC} {c TLC}{c -}{c TRC} {c TLC}{c -}{c TRC} {c TLC}{c -}{c TRC} {c TLC}{c -}{c TRC}
{c |} {c |} {c |} {c |} {c |} {c |} {c |} {c |} {c |} {c |} {c |} {c |}
{c |} {c |} {c |} {c |} {c |} {c |} {c |} {c |} {c |} {c |} {c |} {c |}
{hline 3}{c BT}{c -}{c BT}{hline 6}{c BT}{c -}{c BT}{hline 6}{c BT}{c -}{c BT}{hline 10}{c BT}{c -}{c BT}{hline 6}{c BT}{c -}{c BT}{hline 6}{c BT}{c -}{c BT}{hline 4}
division division division division division division
year year
{p 4 8 8}
{cmd:graph bar revenue, over(year) over(division)}{break}
same as above, but ordered differently. In the previous example we
typed {cmd:over(division)} {cmd:over(year)}. This time, we reverse it:
{c TLC}{c -}{c TRC} {c TLC}{c -}{c TRC} {c TLC}{c -}{c TRC} {c TLC}{c -}{c TRC} {c TLC}{c -}{c TRC} {c TLC}{c -}{c TRC}
{c |} {c |} {c |} {c |} {c |} {c |} {c |} {c |} {c |} {c |} {c |} {c |}
{c |} {c |} {c |} {c |} {c |} {c |} {c |} {c |} {c |} {c |} {c |} {c |}
{hline 2}{c BT}{c -}{c BT}{hline 5}{c BT}{c -}{c BT}{hline 7}{c BT}{c -}{c BT}{hline 5}{c BT}{c -}{c BT}{hline 7}{c BT}{c -}{c BT}{hline 5}{c BT}{c -}{c BT}{c -}
year year year year year year
division division division
{p 4 8 8}
{cmd:graph bar revenue profit, over(division) over(year)}{break}
2*{it:#_of_divisions}*{it:#_of_years} bars showing average revenue and
average profit for each division, repeated for each of the years.
The grouping would look like this (assuming 3 divisions and 2 years):
{c TLC}{c -}{c TRC} {c TLC}{c -}{c TRC} {c TLC}{c -}{c TRC} {c TLC}{c -}{c TRC} {c TLC}{c -}{c TRC} {c TLC}{c -}{c TRC}
{c |} {c |} {c |} {c |} {c |} {c |} {c |} {c |} {c |} {c |} {c |} {c |}
{c |} {c LT}{c -}{c TRC} {c |} {c LT}{c -}{c TRC} {c |} {c LT}{c -}{c TRC} {c |} {c LT}{c -}{c TRC} {c |} {c LT}{c -}{c TRC} {c |} {c LT}{c -}{c TRC}
{c |} {c |} {c |} {c |} {c |} {c |} {c |} {c |} {c |} {c |} {c |} {c |} {c |} {c |} {c |} {c |} {c |} {c |}
{hline 2}{c BT}{c -}{c BT}{c -}{c BT}{hline 5}{c BT}{c -}{c BT}{c -}{c BT}{hline 5}{c BT}{c -}{c BT}{c -}{c BT}{hline 5}{c -}{c BT}{c -}{c BT}{c -}{c BT}{hline 5}{c BT}{c -}{c BT}{c -}{c BT}{hline 5}{c BT}{c -}{c BT}{c -}{c BT}{c -}
division division division division division division
year year
{p 4 8 8}
{cmd:graph bar (sum) revenue profit, over(division) over(year)}{break}
2*{it:#_of_divisions}*{it:#_of_years} bars showing the sum of revenue and
sum of profit for each division, repeated for each of the years.
{p 4 8 8}
{cmd:graph bar (median) revenue profit, over(division) over(year)}{break}
2*{it:#_of_divisions}*{it:#_of_years} bars showing the median of revenue
and median of profit for each division, repeated for each of the years.
{p 4 8 8}
{cmd:graph bar (median) revenue (mean) profit, over(division) over(year)}{break}
2*{it:#_of_divisions}*{it:#_of_years} bars showing the median of revenue
and mean of profit for each division, repeated for each of the years.
{marker remarks3}{...}
{title:Treatment of bars}
{pstd}
Assume someone tells you that the average January temperature in the North
East of the United States is 27.9 degrees Fahrenheit, 27.1 degrees in the
North Central, 46.1 in the South, and 46.2 in the West. You could enter these
statistics and draw a bar chart:
{cmd}. input ne nc south west
{txt} ne nc south west
1{cmd}. 27.9 21.7 46.1 46.2
{txt} 2{cmd}. end{txt}
{cmd:. graph bar (asis) ne nc south west}
{it:({stata gr_example2 grbar0:click to run})}
{* graph grbar0}{...}
{pstd}
The above is admittedly not a great-looking chart, but specifying a few
options could fix that. The important thing to see right now is that, when we
specify multiple {it:yvars}, (1) the bars touch, (2) the bars are different
colors (or at least different shades of gray), and (3) the meaning of the bars
is revealed in the legend.
{pstd}
We could enter this data another way:
{cmd:. clear}
{cmd}. input str10 region float tempjan
{txt} region tempjan
1{cmd}. N.E. 27.9
{txt} 2{cmd}. "N. Central" 21.7
{txt} 3{cmd}. South 46.1
{txt} 4{cmd}. West 46.2
{txt} 5{cmd}. end{txt}
{cmd:. graph bar (asis) tempjan, over(region)}
{it:({stata gr_example2 grbar0b:click to run})}
{* graph grbar0b}{...}
{pstd}
Observe that, when we generate multiple bars via an {cmd:over()} option, (1)
the bars do not touch, (2) the bars are all the same color, and (3) the
meaning of the bars is revealed by how the categorical {it:x} axis is labeled.
{pstd}
This difference in the treatment of the bars in the multiple {it:yvars} case
and the {cmd:over()} case are general properties of {cmd:graph} {cmd:bar} and
{cmd:graph} {cmd:hbar}:
multiple {it:yvars} {cmd:over()} groups
{hline 61}
bars touch yes no
bars different colors yes no
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -