📄 histogram.hlp
字号:
wanted a halfwidth of 5, you would specify {cmd:kdenopts(gauss width(5))}.
{dlgtab:Bar labels}
{phang}
{opt addlabels} specifies that the top of each bar be labeled with the
density, fraction, or frequency, as determined by the {opt density},
{opt fraction}, and {opt frequency} options.
{phang}
{opt addlabopts(marker_label_options)} specifies how to render the labels atop
the bars. See {it:{help marker_label_options}}. Do not specify the
{it:marker_label_option} {opt mlabel(varname)}, which specifies the variable
to be used; this is specified for you by {cmd:histogram}.
{pmore}
{opt addlabopts()} will accept more options than those documented in
{it:{help marker_label_options}}. All options allowed by {helpb scatter}
are also allowed by {opt addlabopts()}. One particularly useful option
is {opt yvarformat()}; see {it:{help advanced_options}}.
{dlgtab:Add plot}
{phang}
{opt addplot(plot)} allows adding additional {opt graph} {opt twoway} plots to
the graph; see {it:{help addplot_option}}.
{dlgtab:Y-Axis, X-Axis, Title, Caption, Legend, Overall, By}
{phang}
{it:twoway_options} are any of the options documented in
{it:{help twoway_options}}. This includes, most importantly, options for
titling the graph (see {it:{help title_options}}), options for saving the
graph to disk (see {it:{help saving_option}}), and the {opt by()} option, which
will allow you to simultaneously graph histograms for different subsets of the
data (see {it:{help by_option}}).
{title:Remarks}
{p 4 4 2}
Remarks are presented under the headings
{hi:Histograms of continuous variables}
{hi:Overlaying normal and kernel-density estimates}
{hi:Histograms of discrete variables}
{hi:Use with by()}
{title:Histograms of continuous variables}
{pstd}
{cmd:histogram} assumes the variable is continuous, so you need only type
{cmd:histogram} followed by the variable name:
{cmd:. use sp500}
{cmd:. histogram volume}
{it:({stata "gr_example sp500: histogram volume":click to run})}
{pstd}
Note the small values reported for density on the {it:y} axis.
They are correct; if you added up the area of the bars, you would get 1.
Nevertheless, many people are used to seeing histograms scaled so that the
bar heights sum to 1,
{cmd:. histogram volume, fraction}
{it:({stata "gr_example sp500: histogram volume, fraction":click to run})}
{pstd}
and others are used to seeing histograms so that the bar height reflects the
number of observations:
{cmd:. histogram volume, frequency}
{it:({stata "gr_example sp500: histogram volume, frequency":click to run})}
{pstd}
Regardless the scale you prefer, we can specify some additional
options to make the graph look more impressive:
{cmd}. summarize volume
{txt} Variable {c |} Obs Mean Std. Dev. Min Max
{hline 13}{c +}{hline 56}
volume {c |}{res} 248 12320.68 2585.929 4103 23308.3
{txt}
{cmd}. histogram volume, freq
xaxis(1 2)
ylabel(0(10)60, grid)
xlabel(12321 "mean"
9735 "-1 s.d."
14907 "+1 s.d."
7149 "-2 s.d."
17493 "+2 s.d."
20078 "+3 s.d."
22664 "+4 s.d."
, axis(2) grid gmax)
xtitle("", axis(2))
subtitle("S&P 500, January 2001 - December 2001")
note("Source: Yahoo!Finance and Commodity Systems, Inc."){txt}
{it:({stata "gr_example2 hist1":click to run})}
{pstd}
For an explanation of the
{opt xaxis()} option{hline 2}it created the upper and lower {it:x}
axis{hline 2}see help {it:{help axis_choice_options}}. For an explanation
of the {opt ylabel()} and {opt xlabel()} options, see help
{it:{help axis_label_options}}. For an explanation of the {cmd:subtitle()}
and {cmd:note()} options, see help {it:{help title_options}}.
{title:Overlaying normal and kernel-density estimates}
{phang}
Specifying {opt normal} will overlay a normal density over the histogram.
It would be enough to type
{cmd}. histogram volume, normal{txt}
{phang}
but we will add the option to our more impressive rendition:
{cmd}. histogram volume, freq normal
xaxis(1 2)
ylabel(0(10)60, grid)
xlabel(12321 "mean"
9735 "-1 s.d."
14907 "+1 s.d."
7149 "-2 s.d."
17493 "+2 s.d."
20078 "+3 s.d."
22664 "+4 s.d."
, axis(2) grid gmax)
xtitle("", axis(2))
subtitle("S&P 500, January 2001 - December 2001")
note("Source: Yahoo!Finance and Commodity Systems, Inc."){txt}
{it:({stata "gr_example2 hist2":click to run})}
{pstd}
If we instead wanted to overlay a kernel-density estimate, we could
specify {cmd:kdensity} in place of {cmd:normal}.
{title:Histograms of discrete variables}
{pstd}
Specify {cmd:histogram}'s discrete option when you wish the data treated as
being discrete{hline 2}when you wish each unique value of the variable
assigned its own bin. For instance, in the automobile data, {cmd:mpg} is a
continuous variable, but the mileage rating have been measured to integer
precision. Were we to type
{cmd:. sysuse auto}
{cmd:. histogram mpg}
{pstd}
{cmd:mpg} would be treated as continuous and categorized into 8 bins by the
default number-of-bins calculation, which is based on the number of
observations, of which we have 74.
{pstd}
Adding the {opt discrete} option makes a histogram with a bin for each
of the 21 unique values:
{cmd:. histogram mpg, discrete}
{it:({stata "gr_example auto: histogram mpg, discrete":click to run})}
{pstd}
Note that just as in the continuous case, the {it:y}-axis was reported in
terms of density and we could specify the {cmd:fraction} or {cmd:frequency}
options if we wanted it reported differently. Below we specify
{cmd:frequency}, we specify {cmd:addlabels} to add a report of
frequencies printed above the bars, we specify {cmd:ylabel(,grid)} to add
horizontal grid lines, and we specify {cmd:xlabel(12(2)42)} to label the
values 12, 14, ..., 42 on the {it:x}-axis:
{cmd:. histogram mpg, discrete freq addlabels ylabel(,grid) xlabel(12(2)42)}
{it:({stata "gr_example auto: histogram mpg, discrete freq addlabels ylabel(,grid) xlabel(12(2)42)":click to run})}
{title:Use with by()}
{pstd}
{cmd:histogram} may be used with {opt graph} {opt twoway}'s {opt by()};
for example,
{cmd:. sysuse auto}
{cmd:. histogram mpg, discrete by(foreign)}
{it:({stata "gr_example auto: histogram mpg, discrete by(foreign)":click to run})}
{pstd}
In this case, results would be easier to compare if the graphs were presented
in a single column:
{cmd:. histogram mpg, discrete by(foreign, col(1))}
{it:({stata "gr_example auto: histogram mpg, discrete by(foreign, col(1))":click to run})}
{pstd}
{cmd:col(1)} is a {cmd:by()} suboption{hline 2}see {it:{help by_option}}{hline 2}
and there are other useful suboptions, such as {opt total}, which will add
an overall total histogram. Note that {opt total} is a suboption of
{opt by()}, not an option of {cmd:histogram}, so you type
{cmd:. histogram mpg, discrete by(foreign, total)}
{pstd}
and not "{cmd:histogram mpg, discrete by(foreign) total}".
{pstd}
As another example, Lipset (1993) reprinted data from the {it:New York Times},
November 5, 1992, of data collected by the Voter Research and Surveys based on
questionnaires completed by 15,490 presidential voters from 300 polling places
on election day.
{phang2}{cmd:. sysuse voter, clear}
{phang2}{cmd:. histogram candi [freq=pop], discrete fraction by(inc, total) gap(40) xlabel(2 3 4, valuelabel)}{p_end}
{phang2}{it:({stata "gr_example voter:histogram candi [freq=pop], discrete fraction by(inc, total) barwidth(.6) xlabel(2 3 4, valuelabel)":click to run})}
{pstd}
We specified {cmd:gap(40)} to reduce the width of the bars by 40%.
Also note our use of the {opt xlabel()}'s {opt valuelabel} suboption, which
caused our bars to be labeled Clinton, Bush, and Perot rather than 2, 3, and
4; see {it:{help axis_label_options}}.
{title:Also see}
{psee}
Manual: {bf:[R] histogram}
{psee}
Online: {helpb kdensity}, {helpb spikeplot}, {helpb twoway histogram}
{p_end}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -