Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: -onewayplot- on SSC

From   "Nick Cox" <>
To   <>
Subject   st: -onewayplot- on SSC
Date   Sat, 5 Jul 2003 23:45:58 +0100

Thanks to Kit Baum, a new -onewayplot- package is now downloadable
from SSC. Stata 8 is required. To install, type

. ssc inst onewayplot

This posting is a little long, but two flags:

(1) there is a detail of interest to users of Stata 6 or Stata 7

(2) there is stuff here _not_ in the help file for -onewayplot-,
so you would be wrong in guessing that all the substance here
would all be in the help file.

Oneway plots in Stata 8

One of the graph types in Stata before version 8 was -graph,
oneway-.  The main idea is to represent a set of values by marks
against a magnitude scale, specifically on a horizontal axis. Such
plots didn't make the cut in the production of the new graphics,
as a few postings on Statalist have elicited.

(They are still available, old-style, from -gr7, oneway-, but that
lacks the flexibility of the new graphics, and cannot be
integrated with it.)

One good reason for that decision is that oneway plots are, at
least in their simplest versions, a special case of scatter plots,
as is shown by

. scatter rep78 mpg

and even the vertical stroke or pipe symbol can be emulated, as by

. gen pipe = "|"
. scatter rep78 mpg, ms(none) mlabel(pipe) mlabpos(0)

However, some people using Stata 8, including myself,
have found that they missed oneway plots. When these are useful,
it is often in ways that mimic some of the features of dotplots,
for example, as implemented in -dotplot-. In 1999, I wrote a program
-onewplot- for Stata 6, which was my variant on -graph, oneway-, and
remains on SSC for any interested users of Stata 6 or Stata 7. Its
most notable feature is a -stack- option which allows multiple
instances of any value to be represented by vertically stacked
symbols, thus resembling a (horizontal) dotplot and also, clearly,
a histogram.

-onewayplot- is -onewplot- rewritten rather drastically for Stata
8 graphics.

The magnitude scale can now be horizontal or vertical. (With
-gr7, oneway- the magnitude axis is always horizontal.  With
-dotplot- the magnitude axis is always vertical.)

By default, there is no binning of data, but you can bin the way I
most like to do it, by specifying a -width()- so that classes are
defined by

	width * floor(varname/width)

With -width(5)- mpg values 10-14 are classed as 10, 15-19 as 15,
etc.  In -dotplot- you can tune the number of bins with the -ny()-

-centre- and -center- options are allowed to mimic the -dotplot,
center- option. In fact, -dotplot, centre- is also legal, but

Inevitably, there are some limitations:

1. -gr7, oneway rescale- stretches each set of data marks to
extend over the whole horizontal range of the graph. There is no
equivalent with -onewayplot- (or indeed -dotplot-), although a
wrapper program to do that would not be difficult.

2. -onewayplot- has no options for showing means, means +/- SDs,
medians or quartiles, in contrast to -gr7, oneway box- or
-dotplot-. I don't particularly like the way that -dotplot- adds
markers for these statistics, so I didn't want to emulate that.
I do like the box-dot or dot-box plots of Leland Wilkinson (1994)

and of Christopher Wild and George Seber in "Chance encounters",
Wiley 2000, in my opinion the best introductory statistics book on the
market. The latter's version is cleaner. I haven't implemented
those extra boxes, as yet.

Using -plot()- to add your own extras

Nevertheless there is a not too tricky way of adding
your own extras to -onewayplot-. This is not documented
in the help file as yet, although in due course it will

-onewayplot- has a -plot()- option, on which see
[G] plot_option or the online help for -plot_option-.

An example will show something of the possibilities.

sysuse auto
egen median = median(mpg) , by(rep78)
egen loq = pctile(mpg) , by(rep78) p(25)
egen upq = pctile(mpg) , by(rep78) p(75)
gen rep78_2 = rep78 - 0.2

onewayplot mpg, by(rep78) stack f(0.5)
	plot(scatter rep78_2 median || rcap upq loq rep78_2,
	hor legend(off) xtitle("Mileage (mpg)"))

If this looks pretty inscrutable, then there are two
ways to scrute it, clearly:

1. Copy the commands into your do-file editor
and see what it does, noting that the last command
starting -onewayplot- should all be on one command

2. In broad terms,

onewayplot mpg, by(rep78) stack f(0.5)

is a variant on a scatter plot of -rep78-
against -mpg-.

What we do is superimpose a plot -- in fact
two plots -- on top of it using -plot()-.

The first plot shows the medians as point

scatterplot rep78_2 median

where -rep78_2- is just 0.2 below -rep78-.

The second plot shows the quartiles:

rcap upq loq rep78_2, hor

draws a horizontal interval


after which we just need to clean up
some of the stuff we don't want with
two extra options, -legend(off) xtitle("Mileage (mpg)")-

If you are still with me, then you'll see that the
same sort of trick can be used to add (e.g.) means and
confidence intervals to the oneway plot, except that we
will have to do a little work to get the confidence limits
into variables:

-ci- leaves r(lb) and r(ub) in its wake, so here is one
way (!) to do it:

egen mean = mean(mpg), by(rep78)
gen ub = .
gen lb = .
qui forval i = 1/5 {
	ci mpg if rep78 == `i'
	replace ub = r(ub) if rep78 == `i'
	replace lb = r(lb) if rep78 == `i'

onewayplot mpg, by(rep78) stack f(0.5)
	plot(scatter rep78_2 mean || rcap ub lb rep78_2,
	hor legend(off) xtitle("Mileage (mpg)"))

There are yet other possibilities. These examples
put the intervals (loq -- median -- upq or
lower limit -- mean -- upper limit) just below
each group of dots. You might prefer just above.
That's the main point: you can customise your own

Program name too long?

Just possibly, you like -onewayplot-, but the name is
too long for your taste. This problem is no problem: write a
wrapper, say

	program owp
		version 8
		onewayplot `0'

and store this as owp.ado wherever you would store onewayplot.ado.
The technicality here is that Stata stores whatever follows a
command name in local macro 0: you can pick up all that this
contains and pass it along to -onewayplot-. In short, if
you type

. owp <whatever>

it will exactly as if you typed

. onewayplot <whatever>


	.h onewayplot

(that ".h" is literal)

would be the entire contents of a help file, say owp.hlp,
pointing you to the help for -onewayplot-.

There are many variants on this trick. In fact, all user
programming in Stata is in a sense just some variant on
this trick.


*   For searches and help try:

© Copyright 1996–2022 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index