📄 graph_bar.hlp
字号:
In this case, we need to make the chart taller, and that is the job of
the {cmd:region_option} {cmd:ysize()}. Below we make a chart that is
7 inches tall:
{cmd:. sysuse nlsw88, clear}
{cmd}. graph hbar wage, over(ind, sort(1)) over(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)
ysize(7){txt}
{it:({stata gr_example2 grbartall:click to run})}
{* graph grbartall}{...}
{pstd}
The important option in the above is {cmd:ysize(7)}, which made the graph
taller than usual; see {it:{help region_options}}.
Concerning the other options:
{phang2}
{cmd:over(ind, sort(1)) over(collgrad)}{break}
{cmd:sort(1)} is specified so that the bars would be sorted on mean wage.
The {cmd:1} says to sort on the first {it:yvar}; see
{hi:Reordering the bars} below.
{phang2}
{cmd:title("Average hourly wage, 1988, women aged 34-46", span)}{break}
{cmd:span} is specified so that the title, rather than being centered over
the plot region, would be centered over the entire graph. In this case,
the plot region (the part of the graph where the real chart appears,
ignoring the labels) is very narrow, and centering over that was not going
to work. See {it:{help region_options}} for a description of
the graph region and plot region, and see {it:{help title_options}}
and {it:{help textbox_options}} for a description of {cmd:span}.
{phang2}
{cmd:subtitle(" ")}{break}
We specified this because the title looked too close to the graph without
it. We could have done things properly and specified a {cmd:margin()}
suboption within the {cmd:title()}, but we often find it easier to
include a blank subtitle. Note that we typed {cmd:subtitle(" ")} and not
{cmd:subtitle("")}. We had to include the blank, or the subtitle
would not have appeared.
{phang2}
{cmd:note("Source: 1988 data from NLS, ...", span)}{break}
{cmd:span} is specified so that the note would be left-justified in
the graph rather than just in the plot region.
{marker remarks10}{...}
{title:How bars are ordered}
{pstd}
The default is to place the bars in the order of the {it:yvars} and to order
each set of {cmd:over(}{it:varname}{cmd:)} groups according to the values of
{it:varname}. Let us consider some examples:
{phang}
{cmd:graph bar (sum) revenue profit}{break}
Bars appear in the order specified: {cmd:revenue} and {cmd:profit}.
{phang}
{cmd:graph bar (sum) revenue, over(division)}{break}
Bars are ordered according to the values of variable {cmd:division}.
{pmore}
If {cmd:division} is a numeric variable, the lowest division number
comes first, followed by the next lowest, and so on. This is true
even if variable {cmd:division} has a value label. Say that
division 1 has been labeled "Sales" and division 2 is labeled "Development".
The bars will be in the order Sales followed by Development.
{pmore}
If {cmd:division} is a string variable, the bars will be ordered by the sort
order of the values of division (meaning alphabetically, but with
capital letters placed before lowercase letters). If variable {cmd:division}
contains the values "Sales" and "Development", the bars will be in the
order Development followed by Sales.
{phang}
{cmd:graph bar (sum) revenue profit, over(division)}{break}
Bars appear in the order specified, revenue and profit, and are
repeated for each division, which will be ordered as explained above.
{phang}
{cmd:graph bar (sum) revenue, over(division) over(year)}{break}
Bars appear ordered by the values of division, as previously explained,
and then that is repeated for each of the years. The years are ordered
according to the values of the variable {cmd:year}, following the same rules
as applied to the variable {cmd:division}.
{phang}
{cmd:graph bar (sum) revenue profit, over(division) over(year)}{break}
Bars appear in the order specified, profit and revenue, repeated
for division ordered on the values of variable {cmd:division}, repeated
for year ordered on the values of variable {cmd:year}.
{marker remarks11}{...}
{title:Reordering the bars}
{pstd}
There are three ways you may wish to reorder the bars.
{phang2}
1. You want to control the order in which the elements of each {cmd:over()}
group appear. Your divisions might be named Development, Marketing,
Research, and Sales, alphabetically speaking, but you want them to
appear in the more logical order Research, Development, Marketing,
and Sales.
{phang2}
2. You wish to order the bars according to their heights. You wish to draw
the graph
{col 16}{cmd:. graph bar (sum) empcost, over(division)}
{pmore2}
and you want the divisions ordered by total employee cost.
{phang2}
3. You wish to order on some other derived value.
{pstd}
We will consider each of these desires separately.
{marker remarks12}{...}
{title:Putting the bars in a prespecified order}
{pstd}
We have drawn the graph
{cmd:. graph (sum) bar empcost, over(division)}
{pstd}
Variable {cmd:division} is a string containing "Development", "Marketing",
"Research", and "Sales". We want to draw the chart placing the divisions in
the order Research, Development, Marketing, and Sales.
{pstd}
To do that, we create a new numeric variable that orders division as we
would like:
{cmd:. gen order = 1 if division=="Research"}
{cmd:. replace order = 2 if division=="Development"}
{cmd:. replace order = 3 if division=="Marketing"}
{cmd:. replace order = 4 if division=="Sales"}
{pstd}
We can name the variable and create it however we wish, but we must be sure
that there is a one-to-one correspondence between the new variable and the
{cmd:over()} group's values. We then specify the {cmd:over()}'s
{cmd:sort(}{it:varname}{cmd:)} option:
{phang2}
{cmd:. graph bar (sum) empcost, over( division, sort(order) )}
{pstd}
If you want to reverse the order, you may specify the {cmd:descending}
suboption:
{phang2}
{cmd:. graph bar (sum) empcost, over(division, sort(order) descending)}
{marker remarks13}{...}
{title:Putting the bars in height order}
{pstd}
We have drawn the graph
{phang2}
{cmd:. graph bar (sum) empcost, over(division)}
{pstd}
and now wish to put the bars in height order, shortest first. We type
{phang2}
{cmd:. graph bar (sum) empcost, over( division, sort(1) )}
{pstd}
If we wanted the tallest first, type
{phang2}
{cmd:. graph bar empcost, over(division, sort(1) descending)}
{pstd}
The {cmd:1} in {cmd:sort(1)} refers to the first (and in this case, only)
{it:yvar}. If we had multiple {it:yvars}, we might type
{phang2}
{cmd:. graph bar (sum) empcost othcost, over( division, sort(1) )}
{pstd}
and we would have a chart showing employee cost and other cost, sorted
on employee cost. If we typed
{phang2}
{cmd:. graph bar (sum) empcost othcost, over( division, sort(2) )}
{pstd}
the graph would be sorted on other cost.
{pstd}
We can use {cmd:sort(}{it:#}{cmd:)} on the second {cmd:over()} group as well:
{cmd:. graph bar (sum) empcost, over( division, sort(1) )}
{cmd:over( country, sort(1) )}
{pstd}
Country will be ordered on the sum of the heights of the bars.
{marker remarks14}{...}
{title:Putting the bars in a derived order}
{pstd}
We have employee cost broken into two categories: empcost_direct and
empcost_indirect. Variable emp_cost is the sum of the two. We wish to make
a chart showing the two costs, stacked, over division, and we want the bars
ordered on the total height of the stacked bars. We type
{cmd:. graph bar (sum) empcost_direct empcost_indirect,}
{cmd:stack}
{cmd:over( division, sort((sum) empcost) {cmd:descending} )}
{marker remarks15}{...}
{title:Reordering the bars, example}
{pstd}
We have a dataset showing the spending on tertiary education as a percent of
GDP from the 2002 edition of
{it:Education at a Glance: OECD Indicators 2002}.
{cmd}. sysuse educ99gdp, clear
. list
{txt}
{c TLC}{hline 15}{c -}{hline 8}{c -}{hline 9}{c TRC}
{c |} {res} country public private {txt}{c |}
{c LT}{hline 15}{c -}{hline 8}{c -}{hline 9}{c RT}
1. {c |} {res} Australia .7 .7 {txt}{c |}
2. {c |} {res} Britain .7 .4 {txt}{c |}
3. {c |} {res} Canada 1.5 .9 {txt}{c |}
4. {c |} {res} Denmark 1.5 .1 {txt}{c |}
5. {c |} {res} France .9 .4 {txt}{c |}
{c LT}{hline 15}{c -}{hline 8}{c -}{hline 9}{c RT}
6. {c |} {res} Germany .9 .2 {txt}{c |}
7. {c |} {res} Ireland 1.1 .3 {txt}{c |}
8. {c |} {res} Netherlands 1 .4 {txt}{c |}
9. {c |} {res} Sweden 1.5 .2 {txt}{c |}
10. {c |} {res}United States 1.1 1.2 {txt}{c |}
{c BLC}{hline 15}{c -}{hline 8}{c -}{hline 9}{c BRC}
{pstd}
We wish to graph total spending on education and simultaneously show the
distribution of that total between public and private expenditures. We want
the bar sorted on total expenditures:
{cmd}. 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}
Alternatively, perhaps we wish to disguise the total expenditures and
focus the graph exclusively on the share of spending that is public and
private:
{cmd}. generate frac = private/(private + public)
. graph hbar (asis) public private,
over(country, sort(frac) descending) stack percent
title("Public and private spending on tertiary education, 1999",
span pos(11) )
subtitle(" ")
note("Source: OECD, Education at a Glance 2002", span){txt}
{it:({stata gr_example2 grbar8:click to run})}
{* graph grbar8}{...}
{pstd}
The only differences between the two {cmd:graph} {cmd:hbar} commands are as
follows:
{phang2}
1. The {cmd:percentage} option was added to change the {it:yvars}
{cmd:public} and {cmd:private} from spending amounts to percent each is of
the total.
{phang2}
2. The order of the bars was changed.
{phang2}
3. The title was changed.
{marker remarks16}{...}
{title:Use with by()}
{pstd}
{cmd:graph} {cmd:bar} and {cmd:graph} {cmd:hbar} may be used with {cmd:by()},
but in general, you want to use {cmd:over()} in preference to {cmd:by()}.
Bar charts are explicitly categorical and do an excellent job of presenting
summary statistics for multiple groups in a single chart.
{pstd}
A good use of {cmd:by()}, however, is when you are ordering the bars and
you wish to emphasize that the ordering is different for different groups.
For instance,
{cmd:. sysuse nlsw88, clear}
{cmd:. graph hbar wage, over(occ, sort(1)) by(union)}
{it:({stata "gr_example nlsw88: graph hbar wage, over(occ, sort(1)) by(union)":click to run})}
{* graph barby}{...}
{pstd}
The above graph orders the bars by height (hourly wage);
note that the orderings are different for union and nonunion workers.
{* index histories}{...}
{* index Playfair, William}{...}
{* index Tufte}{...}
{* index Beniger and Robyn}{...}
{marker remarks18}{...}
{title:History}
{pstd}
The first published bar chart appeared in William Playfair's
{it:Commercial and Political Atlas} (1786).
See Tufte (1983, 32-33) or Beniger and Robyn (1978) for additional historical
information.
{title:Also see}
{psee}
Manual: {bf:[G] graph bar}
{psee}
Online:
{helpb graph dot};
{helpb collapse},
{helpb table}
{p_end}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -