I want another axis on my box plot

This example came from a question Nick Cox posted on statalist. Nick wanted to
add a second X-axis on his horizontal box plot. graph twoway supports
multiple X-axes, but graph box and graph hbox do not.

Original graph

Let's look at some data and a box plot representative of Nick's problem by
clicking the next two commands.

. sysuse census , clear

. graph hbox medage, over(region, total)

Identify all outliers using standard options

Nick was mainly concerned with the outside value falling to the right, because
the two to the left fell within the whiskers of the West region. Ignoring
this, we might just decide to label all of the outside values using the
standard marker(... mlabel()) option of graph .smcl. (Click the
command below.)

. graph hbox medage, over(region, total) marker(1, mlabel(state) mlabpos(11))

That wasn't at all what Nick wanted, and here is Nick's solution using
standard graph syntax.

Nick's solution:

- make room above the graph by specifying an empty subtitle

- use text() option to place text outside the plotregion

. list state medage if medage > 34

. graph hbox medage, over(region, total) subtitle(" ") text( 34.7 109 "Florida")

Adding a real axis (foregoing the _gm_edit prefix)

That was a very workable solution, but it doesn't do everything an axis could;
so let's try and get Nick a real axis. We will not prefix our commands with
_gm_edit to conserve space.

We re-draw the original graph, then insert a new axis and show the
results by clicking the next 3 commands.

. graph hbox medage, over(region, total)

. .Graph.insert (.xaxis2 = .axis.new , position(above)) above plotregion1

. graph display

First, we note that there is not much to see, just a new line at the top of
the plot region, and we clearly have more work to do. More importantly, there
is much happening in that .Graph.insert command and we should probably
take a closer look.

Looking first inside the parentheses at
.xaxis2 = .axis.new , position(above), this part of the command pretty
much does what it says. It creates a brand new axis and names that axis
.xaxis2. The option position(above) merely informs the axis that
it will be located above rather than below or leftof or
rightof a plotregion. In this way the axis know where to point its
ticks and where to place its labels. This would be a perfectly good command
to be issued in the Stata command window, the axis just would not be
associated with anything. Instead, the new axis has been immediately inserted
into our graph (.Graph) using the .insert member function
of graphs. The remaining argument, above plotregion1, just tells
.insert where to put the newly inserted object - above the object
called plotregion1.

So, our graph now contains a new object, an axis, and that axis' full name is
.Graph.xaxis2.

Let's see if we can get that axis to behave the way we want. Currently the
axis doesn't know anything about its environment, and vice-versa. The axis
doesn't even know what scale its ticks are on. We can associate our new axis
and the existing plotregion of the graph using the .set_axis_plotregion
member program of graphs. This program informs the plotregion that an axis is
associated with it and does the same for the axis, while also setting the
scale of the axis to be that of the associated plotregion. (Click next 2
commands.)

. .Graph.set_axis_plotregion xaxis2 plotregion1 x

. graph display rule

We feel better about our axis, but we need to tell it to do something. Let's
add a tick for the Florida outside value using the .add_ticks member
program on the major tickset of our axis. Not that the arguments to this
program are exactly like the values argument to the standard
xlabel() and .smcl() options of graph. That is not a
coincidence. (Click the next two commands.)

. .Graph.xaxis2.major.add_ticks 34.7 "Florida"

. graph display

Done. We have a real axis for Nick.

We can change the look of our axis, using what we learned
earlier. (Click the next 2 commands.)

. .Graph.xaxis2.style.editstyle majorstyle(tickstyle(textstyle( color(red) size(large) ))) editcopy

. graph display

We can add more labeled ticks and a title to out axis.
(Click the next 3 commands.)

. .Graph.xaxis2.major.add_ticks 26.1 "Alaska" 24.2 "Utah"

. .Graph.xaxis2.title.text[1] = "States with unusual age distributions"

. graph display

<< index >>