Nick wants arrows

Nick Cox has asked me several times
about the ability to draw arrows in twoway
graphs. This seems like a perfect excuse to learn another secret, this
time about twoway plottypes.

A secret about graph twoway

o designed to find properly named files and use
them just like official twoway
plottypes

o at a minimum twoway needs to find:

yxtype_<our_plot>.style

and

yxtype_<our_plot>_draw.ado

We can leverage that information to create a new twoway plottype, one
that places arrows at the ends of lines.

There are a number of ways to add plottypes to twoway with the simplest
requiring only two files - a properly named ado
program to draw the plots and
a .style file that telling twoway that our new plottype exists. Let's
look first at the .style file by clicking on below.
This file contains a single line, and even that is not required, the file
could be empty. The sequence number merely tells the graphics system
where to place the style name in any dialog box controls in which it may
need to appear.

Unlike graph bar, which required us to create a new class file,
adding a new plottype to twoway only requires an ado-file to draw the
plots. We will call our new plottype sarrows with the s
standing for simple or perhaps sequential. Click on
below to see our new ado-file.

- an ado program to draw our
plot type

- a style file that signals
twoway that our plot type exists.

Again, we will not delve into the drawing program in detail, but like
the drawing programs for graph balbar and graph stbar it uses
low-level gdi calls to draw our lines and arrows.

Our new plottype expects two variables, a Y and X-variable. It connects
sequential (Y,X) observations and draws an arrowhead at the end whenever
an observation is encountered that is missing either the Y or X value.

Let's input some data to test our new plottype. (Click on the
drop _all and input y x links below. Then,
click on the twoway sarrows y x to draw the arrows. You can
see that our new sarrows plottype is used just like all of
twoways native plottypes (e.g., scatter or line).

. drop _all
. input y x
. 0 0
. 1 1
. . .
. 0 0
. -1 1
. end

. twoway sarrows y x

Well, that looks just fine.

Let's try a more more complicated example
by clicking on the next 4 commands.

. drop _all
. set mem 20m
. use alaska

. do alaska do-file

The outline of Alaska took a bit to draw because it has almost 60,000 points
in almost 800 polygons. Even so, by clicking on
do-file we can see that the state was drawn with a
single area plot. We can also see that our ocean current vectors were
drawn as a single sarrows plot, using our new plottype.

The command looks rather daunting because
we included lots of options to get colors and scaling pleasant, but the
underlying command is quite simple. The command includes an if
qualifier to clean up the data
because there are a few too many points in the polygon of the main
body of Alaska and because each polygon also includes its center point.

Keeping the if, but ignoring the other options, our twoway graph
command can be stripped down to a simple area plot and sarrow
plot.

. twoway area latitude longitude if !cpoint & (poly != 1 | mod(_n,5)==0) , nodropbase cmissing(no) || sarrows lat_v long_v

That's about all we have time for. We have just scratched the surface of what
lies beneath the graph command. This level will remain undocumented for
now, but we hopefully have a better understanding of the structure of Stata's
graphs and we have learned a few "secrets" that we can may find useful.

<< index >>