help for floatplot

Floating or sliding stacked bar plot for frequencies, proportions, or percents

floatplot
numvar
[weight]
[if exp]
[in range]

,
{
centre(#) |
center(#) |
highnegative(#)
}

{
fcolors(colors) | fcolours(colours)
}

[
vertical

proportions
frequencies

over(overvar)
by(byvar, hyopts)

barwidth(#)
{ lcolors(colors) | lcolours(colours) }
baropts(rbar_options)

format(format)
textoffset(#)
showvalopts(marker_label_options)

twoway_options
]

fweights and aweights may be specified.

Description

floatplot produces a floating or sliding stacked bar plot showing
percents (or optionally proportions or frequencies) of categories of a
numeric outcome variable numvar by zero, one, or two other
categorical variables. The plot is most helpful if categories of an
outcome variable have a natural or conventional pre-defined order.
Although there is no formal check, the design of the plot tacitly
assumes a modest number of distinct categories, say between 2 and 9. The
command is a wrapper for a call to twoway rbar for showing bars and
twoway scatter for showing text.

The bar plot is horizontal by default but may optionally be vertical.
The choice is a matter of personal taste, although in general horizontal
displays make it easier to show and read values or labels of categories.

The most basic option choice concerns which bars are stacked below a
zero axis, which if any bar straddles zero, and which bars are stacked
above a zero axis. This is best explained by example. Suppose the
outcome of interest is a 5 point scale with 1 = strongly disagree, 2 =
disagree, 3 = neither disagree nor agree, 4 = agree, 5 = strongly agree.
Then a conventional and possibly congenial choice would be that a bar
representing the percent (proportion, frequency) of answer 3 would
straddle zero; bars representing answers 1 and 2 would be stacked
negatively and bars representing 4 and 5 would be stacked positively.
The syntax ensuring this would be centre(3) or center(3).
Other way round, suppose the scale is a 4 point scale with 1 and 2 to be
plotted as negative categories and 3 and 4 to be plotted as positive
categories. The syntax for ensuring that would be highnegative(2)
indicating the highest value to be plotted as negative.

Remarks

The history of this plot depends on its definition. For example, bars
floating relative to an axis have long been used to indicate (say) the reigns
of monarchs, the lives of famous people, radio and television schedules, high
and low prices, or the durations allocated to or consumed by various tasks
(so-called Gantt charts, for example).

A plot does not absolutely need a name if a widely agreed name does not
exist, but a Stata command certainly does. Brinton (1939) briefly
showed what he called bilateral bar charts and similar designs appear
under that and other names in many bar charts showing paired variables
(such as pyramids showing age and sex breakdown of populations). The
focus here is rather on bar charts in which several ordered categories
are shown at once. Greater credit must be given to Stouffer et al.
(1949a, 1949b) who showed many examples of such charts, without ever
naming them so far as I can tell.

Spear (1952) and Schmid (1954) both used the terms sliding bar (for
horizontal plots) and floating column (for vertical plots). The
terminology may well be older or perhaps both authors devised those
terms independently. Either way, Spear gave no literature references,
while Schmid did give literature references but did not cite Spear. The
terms are repeated in later works, Spear (1969) on one side and Schmid
and Schmid (1979) and Schmid (1983) on another. In her later book Spear
did give some literature references but not to any work by Schmid, while
none of the Schmid sequels cite Spear either. Be that as it may, terms
such as sliding, slide, or floating have been repeated by others, such
as Lockwood (1969) and Robertson (1988).

In their papers giving a big push to the idea, Robbins and
Heiberger (2011) and Heiberger and Robbins (2014) talk of
diverging stacked bar charts, a term that is much more informative, but
also a little more clunky in my view. See also Heiberger and Holland (2015).

Contrariwise, the example given
by Rahlf (2017, pp.108-110) underlines that such graphs can easily
be given without using any special name.

A problem for me as author is that I wrote a Stata command
slideplot in 2003 as a wrapper for graph bar or
graph hbar. It should remain accessible, so that name is taken,
and a slightly different name such as slidebar seems likely to be
confusing. Hence, with some small pleasure in a whimsical name that
should make sense once people see the results, I have called this
command floatplot.

Although neither option is required, the most interesting and useful
plots show how the distribution of an outcome varies with one or two
predictors, for which over() and by() options are supplied.

Note that numvar and overvar if specified are temporarily
mapped to numeric variables that are integers 1 up, regardless of their
existing values.

Categories that do not appear in the data may be considered as shown
with bars of zero length, which should be regarded as defined but
invisible. Text displays of 0 for percents, proportions, or frequencies
are suppressed too. Note that text labels may overlap whenever adjacent
categories are infrequent. Users finding this puzzling or unclear, for
themselves or their readers, may wish to turn to a different design. In
particular, tabplot from the Stata Journal makes zeros
discernible as holes in a display and small frequencies discernible as
short bars.

Here even more than usually, the command is offered as
indicative, not definitive. In particular, the design codifies
prejudices in favour of a hybrid graph and table display.

Options

How bars should be arranged

centre() or center() specifies a value of numvar for a
bar to straddle zero. As explained in the Description, an example such
as centre(3) specifies that the bar representing the abundance of
numvar value 3 should straddle zero, so that bars for lower values
will plot negatively and bars for higher values will plot positively.

highnegative() specifies the highest value of numvar
to be plotted negatively. As explained in the Description, an example
such as highnegative(2) specifies that bars representing the
abundance of numvar value 2 or lower should be plotted negatively
and so also that bars representing higher values should be plotted
positively.

One (and only one) of centre(), center() or
highnegative() must be specified.

vertical specifies vertical bars (columns). The default
is horizontal.

What should be plotted

Options proportions or frequencies specify that proportions
(between 0 and 1) or frequencies should be plotted, rather than the
default percents. The options are exclusive.

If analytic weights are specified, the frequencies need not be integers.
An important nuance here is that floatplot calculates percents and
proportions from the data given. If you wish data or summaries available
to you to be echoed exactly, use the frequencies option. For
example, suppose the data available are percents of strongly
agree to strongly disagree with also percents of don't know and/or other
"missing" categories, and you wish to ignore the missings, but to show the
percents as percents of the total number of respondents, not of those
answering with an agreement category. frequencies is the option
you need.

How bars should be rendered

fcolours() or fcolors() is a required option
specifying fill colo[u]rs for each bar. As many colo[u]rs should be
specified as there are distinct categories of the outcome.

lcolours() or lcolors() optionally specifies
line colo[u]rs for each bar. The default is to use the choices of
fcolours() or fcolors(), as explained just above.

barwidth() specifies the width of all bars. The
default is 0.5.

baropts() is for specifying other options of
twoway rbar.

Other variables

over() specifies a categorical variable (numeric or
string) defining different groups of observations to be shown in
parallel within one graph panel.

by() specifies that bars are to be shown in separate
panels for distinct values of a categorical variable (numeric or
string). The usual suboptions are allowed, as explained in the help for
by_option. However, although total is legal, it is useless
and doesn't do anything helpful.

Text display of percents, proportions, or frequencies

format() specifies a display format for the text display
of percents, proportions, or frequencies. The default format
is %2.0f, except if the option proportions is specified, when it
is %3.2f. See help for format if more detail is needed.

textoffset() specifies an offset distance relative to the
midpoint of each bar to be subtracted
for the position of text. The default is 0.35, which with the default of
barwidth(0.5) places text 0.1 below each horizontal bar, or
above it with ysc(reverse), or 0.1 to the left of each vertical
bar, or to the right with xsc(reverse). textoffset(0) places
text at the midpoint of each bar, which can work well if colo[u]rs are
subdued. Negative offsets are allowed to flip text above or to the right of
each bar.

showvalopts() are other marker_label_options,
except that as a special case none suppresses the display of text.

twoway_options refers to other options of
graph twoway.

Examples

. set scheme s1color

A canonical example for Stata users arises with the auto data.
How does the distribution of grades 1 to 5 for repair record rep78
vary with whether cars are foreign?

. sysuse auto, clear

. floatplot rep78, centre(3) fcolors(red red*0.5 gs12 blue*0.5 blue) over(foreign) note(% centred on record 3) name(float1, replace)

. floatplot rep78, centre(3) vertical fcolors(red red*0.5 gs12 blue*0.5 blue) over(foreign) note(% centred on record 3) name(float2, replace)

The next example shows how to get an extra display showing all observations. See also Cox (2014).

. preserve

. expand 2

. replace foreign = 2 in 75/L

. label def origin 2 "All cars", modify

. floatplot rep78, centre(3) vertical fcolors(red red*0.5 gs12 blue*0.5 blue) over(foreign) note(% centred on record 3) name(float3, replace)

. restore

Fienberg (1980, 54-55) reports data from
Duncan, Schuman and Duncan (1973) from 1959 and 1971 surveys of a large
American city asking "Are the radio and TV networks doing a good job, just a
fair job, or a poor job?".

. clear

. input float(year freq) long(Opinion race)

1959 81 3 2

1959 23 2 2

1959 4 1 2

1959 325 3 1

1959 253 2 1

1959 54 1 1

1971 224 3 2

1971 144 2 2

1971 24 1 2

1971 600 3 1

1971 636 2 1

1971 158 1 1

end

. label values Opinion Opinion

. label def Opinion 1 "Poor" 2 "Fair" 3 "Good"

. label values race race

. label def race 1 "White" 2 "Black"

. floatplot Opinion [fw=freq] , over(race) by(year, note("") col(1)) centre(2) fcolors(red gs12 blue) subtitle(, pos(9) fcolor(none) nobexpand nobox) ytitle("") name(float4, replace)

. floatplot Opinion [fw=freq] , over(year) by(race, note("") row(1) legend(pos(3))) centre(2) fcolors(red gs12 blue) subtitle(, fcolor(none) nobexpand nobox) xtitle("") vertical name(float5, replace)

Aitkin et al. (1989, 242; 2005, 299; 2009, 311) reported data
from a survey of student opinion on the Vietnam War taken at the University of
North Carolina in Chapel Hill in May 1967. Students were classified by sex,
year of study, and the policy they supported, given choices of

A The US should defeat the power of North Vietnam by widespread bombing
of its industries, ports and harbours and by land invasion.

B The US should follow the present policy in Vietnam.

C The US should de-escalate its military activity, stop bombing North
Vietnam, and intensify its efforts to begin negotiation.

D The US should withdraw its military forces from Vietnam immediately.

(They also report response rates (1989, 243; 2005, 298; 2009, 310), averaging 26% for males and 17%
for females.)

For other plots and analyses, see for example Friendly (2000) and Friendly
and Meyer (2016).

. clear

. input str1 policy float freq long(female year)

"A" 175 0 1

"B" 116 0 1

"C" 131 0 1

"D" 17 0 1

"A" 160 0 2

"B" 126 0 2

"C" 135 0 2

"D" 21 0 2

"A" 132 0 3

"B" 120 0 3

"C" 154 0 3

"D" 29 0 3

"A" 145 0 4

"B" 95 0 4

"C" 185 0 4

"D" 44 0 4

"A" 118 0 5

"B" 176 0 5

"C" 345 0 5

"D" 141 0 5

"A" 13 1 1

"B" 19 1 1

"C" 40 1 1

"D" 5 1 1

"A" 5 1 2

"B" 9 1 2

"C" 33 1 2

"D" 3 1 2

"A" 22 1 3

"B" 29 1 3

"C" 110 1 3

"D" 6 1 3

"A" 12 1 4

"B" 21 1 4

"C" 58 1 4

"D" 10 1 4

"A" 19 1 5

"B" 27 1 5

"C" 128 1 5

"D" 13 1 5

end

. label values female female

. label def female 0 "male" 1 "female"

. label values year year

. label def year 5 "Graduate"

. encode policy, gen(Preference)

. floatplot Preference [fw=freq], over(year) by(female, note("")) highneg(2) fcolors(red red*0.5 blue*0.5 blue) subtitle(, fcolor(green*0.2)) name(float6, replace)

Box, Hunter and Hunter (1978, 145-149; 2005, 112-116)
gave data on five hospitals on the degree of restoration (no improvement,
partial functional restoration, complete functional restoration) of certain
joints impaired by disease effected by a certain surgical procedure. (It is
not clear whether these data are real.) Hospital E is a referral hospital.
Box et al. carry out chi-square analyses, focusing on the difference between
Hospital E and the others.

. clear

. input str1 hospital float freq long restore

"A" 13 1

"B" 5 1

"C" 8 1

"D" 21 1

"E" 43 1

"A" 18 2

"B" 10 2

"C" 36 2

"D" 56 2

"E" 29 2

"A" 16 3

"B" 16 3

"C" 35 3

"D" 51 3

"E" 10 3

end

. label values restore restore

. label def restore 1 "none" 2 "partial" 3 "complete"

. label var restore "How far restored?"

. floatplot restore [w=freq], over(hospital) centre(2) fcolors(red gs12 blue) ysc(reverse) name(float7, replace)

. floatplot restore [fw=freq], over(hospital) centre(2) fcolors(red*0.4 gs12*0.4 blue*0.4) lcolors(red black blue) textoffset(0) showvalopts(mlabsize(medlarge)) subtitle(percents) name(float8, replace)

To compare several variables, reshape long and apply floatplot, over():

. use http://www.stata-press.com/data/cirtms/anxiety5items, clear

. rename (*) (answer=)

. gen id = _n

. reshape long answer, i(id) j(question) string

. egen prneg = mean(answer <= 2), by(question)

. myaxis question2=question, sort(mean prneg)

. label def question2 1 "at ease", modify

. floatplot ans, over(question2) center(3) fcolors(red red*0.5 gs12 blue*0.5 blue) name(float9, replace)

Acknowledgments

Eric Melse made several helpful and encouraging comments.
He suggested the problem of comparing several variables. Marc Kaulisch
reported a bug with by() and flagged the difficulty with
by(, total), which led to a new example using expand.

Author

Nicholas J. Cox, University of Durham

n.j.cox@durham.ac.uk

Also see

Help for

tabplot (Stata Journal) (if installed)

distplot (Stata Journal) (if installed)

qplot (Stata Journal) (if installed)

myaxis (Stata Journal) (if installed)

slideplot (SSC) (if installed)

catplot (SSC) (if installed)

References

Aitkin, M., D. Anderson, B. Francis, and J. Hinde. 1989.
Statistical Modelling in GLIM. Oxford: Oxford University Press.

Aitkin, M., B. Francis and J. Hinde. 2005.
Statistical Modelling in GLIM 4.
Oxford: Oxford University Press.

Aitkin, M., B. Francis, J. Hinde and R. Darnell. 2009.
Statistical Modelling in R.
Oxford: Oxford University Press.

Bentley, J. L. 1984.
Programming Pearls: Graphic output.
Communications, Association for Computing Machinery
27: 529-536.

Bentley, J. L. 1988.
More Programming Pearls: Confessions of a Coder.
Reading, MA: Addison-Wesley.

Box, G. E. P., J. S. Hunter and W. G. Hunter. 2005.
Statistics for Experimenters: Design, Innovation, and Discovery.
Hoboken, NJ: John Wiley.

Box, G. E. P., W. G. Hunter, and J. S. Hunter. 1978.
Statistics for Experimenters: An Introduction to Design, Data Analysis, and Model Building. New York: Wiley.

Brinton, W. C. 1939.
Graphic Presentation.
New York: Brinton Associates.

Cox, N. J. 2004a.
Speaking Stata: Graphing distributions.
Stata Journal 4: 66-88.

Cox, N. J. 2004b.
Speaking Stata: Graphing categorical and compositional data
Stata Journal 4: 190-215.

Cox, N. J. 2014.
Stata tip 119: Expanding datasets for graphical ends.
Stata Journal 14: 230-235.

Cox, N. J. 2016.
Speaking Stata: Multiple bar charts in table form
Stata Journal 16: 491-510.

Duncan, O. D., H. Schuman, and B. Duncan. 1973.
Social Change in a Metropolitan Community.
New York: Russell Sage Foundation.

Evergreen, S. D. H. 2017.
Effective Data Visualization: The Right Chart for the Right Data.
Thousand Oaks, CA: SAGE. [second edition 2020]

Fienberg, S. E. 1980.
The Analysis of Cross-Classified Categorical Data.
Cambridge, MA: MIT Press.

Friendly, M. 2000.
Visualizing Categorical Data.
Cary, NC: SAS Institute.

Friendly, M. and D. Meyer. 2016.
Discrete Data Analysis with R: Visualization and Modeling Techniques for Categorical and Count Data.
Boca Raton, FL: CRC Press.

Heiberger, R. M. and B. Holland. 2015.
Statistical Analysis and Data Display: An Intermediate Course with Examples in R. New York: Springer.

Heiberger, R. M. and N. B. Robbins. 2014.
Design of diverging stacked bar Charts for Likert scales and other applications.
Journal of Statistical Software 57(5): 1-32.
doi:10.18637/jss.v057.i05

Lockwood, A. 1969.
Diagrams: A Visual Survey of Graphs, Maps, Charts and Diagrams for the Graphic Designer.
London: Studio Vista.

Robertson, B. 1988.
Learn to Draw Charts and Diagrams Step by Step.
London: Macdonald.

Rahlf, T. 2017.
Data Visualization with R: 100 Examples.
Cham: Springer.

Robbins, N. B. and R. M. Heiberger. 2011.
Plotting Likert and other rating scales.
JSM Proceedings, Section on Survey Research Methods, 1058-1066.
Alexandria, VA: American Statistical Association.
https://www.amstat.org/membersonly/proceedings/2011/papers/300784_64164.pdf
Google for copies if this official version is inaccessible to you.

Schmid, C. F. 1954.
Handbook of Graphic Presentation.
New York: Ronald Press.

Schmid, C. F. 1983.
Statistical Graphics: Design Principles and Practices.
New York: John Wiley.

Schmid, C. F. and S. E. Schmid. 1979.
Handbook of Graphic Presentation.
New York: John Wiley.

Spear, M. E. 1952.
Charting Techniques.
New York: McGraw-Hill.

Spear, M. E. 1969.
Practical Charting Techniques.
New York: McGraw-Hill.

Stouffer, S. A., E. A. Suchman, L. C. DeVinney, S. A. Star, and R. M.
Williams, Jr. 1949a.
The American Soldier: Adjustment During Army Life.
Princeton, NJ: Princeton University Press.

Stouffer, S. A., A. A. Lumsdaine, M. H. Lumsdaine, R. M. Williams, Jr.,
M. B. Smith, I. L. Janis, S. A. Star, and L. S. Cottrell. 1949b.
The American Soldier: Combat and its Aftermath.
Princeton, NJ: Princeton University Press.