help side_histogram

Title

side_histogram -- Side by side histograms for two groups

Syntax

side_histogram
varname
[if] [in]
[weight]
,
over(groupvar)
[,
start(#)
width(#)
{ fraction |
frequency }
discrete
bar1options(bar_options)
bar2options(bar_options)
squeeze(#)
graph_options
]

fweights are allowed: see weight.

Description

side_histogram draws side-by-side histograms for varname and
precisely two groups of groupvar. Suppose the bin width is w.
Then histogram bars for each bin and group are by default placed side
by side, each with bar width w/2.

Remarks

This command is presented without endorsement of whether it is a good
idea either in general or for particular applications.

Generalisation to grouping variables with three or more distinct
categories is not planned. You may get what you wish with
graph bar, but be warned that that command has limited scope to
plot bars for bins not populated in the data.

Options

over() specifies a numeric or string variable groupvar with
precisely two distinct non-missing values in the data chosen. It is a
required option.

start() specifies the start of binning. It defaults to the minimum
observed value.

width() specifies the bin width to be used. Its default implies a
maximum of 20 bins.

Most users should want to take control of bin start and width. That
choice should benefit from looking at results of summarize and/or
a rough histogram.

fraction specifies that the vertical axis should show
fractions (proportions) of the total frequency.

frequency specifies that the vertical axis should show
frequencies or counts.

These options may not be specified together.

discrete specifies that varname should be regarded
as discrete, not continuous.

bar1options() and bar2options() specify options of
twoway bar controlling the first and second sets of bars
respectively.

squeeze() may be used to squeeze bars so that a thin space is
added on either side of each pair of bars. This action would be cosmetic
or psychological. The default is 1, meaning no squeezing, so that all
bars touch by default. Otherwise, a number such as 0.8 may have
appealing results.

graph_options are other options of twoway. Good examples would be
name() or saving().

Examples

. sysuse auto, clear

. side_histogram mpg, over(foreign) start(10) width(1) freq legend(row(1) pos(12)) name(mpg, replace)

. side_histogram rep78, over(foreign) discrete width(1) squeeze(0.8) freq legend(row(1) pos(12)) name(rep78, replace)

. * this example stimulated by Alexander (2023, pp.246-247)

. clear

. set obs 1000

. set seed 314159

. gen which = _n >= 500

. label def which 1 No 0 Yes

. label val which which

. gen Outcome = rnormal(cond(which == 1, 5, 6), 1)

. side_histogram Outcome, width(0.2) over(which) freq xla(2/9) name(side, replace)

. twoway histogram Outcome if which == 0, freq ///

lcolor(stc1*2) fcolor(stc1%25) start(1.8) width(0.2) xla(2/9) ///

|| histogram Outcome if which == 1, freq ///

lcolor(stc2*2) fcolor(stc2%25) start(1.8) width(0.2) ///

legend(order(1 "Yes" 2 "No")) name(super, replace)

. qplot Outcome, over(which) legend(off) trscale(invnormal(@)) ///

addplot(scatteri 8.8 2 "Yes", ms(none) mlabsize(large) mlabc(stc1) ///

|| scatteri 6.6 2 "No", ms(none) mlabsize(large) mlabc(stc2)) yla(2/9) xla(-3/3) ///

xtitle(Standard normal deviate) name(qplot, replace)

Author

Nicholas J. Cox, Durham University

n.j.cox@durham.ac.uk

References

Alexander, R. 2023.
Telling Stories with Data: With Examples in R.
Boca Raton, FL: CRC Press.
pp. 247, 319, 363, 364, 432, 497.

Also see

twoway__histogram_gen

twoway bar

qplot (Stata Journal) (if installed)