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

Re: st: Additional lines on histogram

From (Vince Wiggins, StataCorp)
Subject   Re: st: Additional lines on histogram
Date   Fri, 18 Nov 2005 09:49:08 -0600

Eric G. Wruck <> asks,

> I am generating a histogram but want to superimpose additional lines
> for certain values on the x-axis.  However, the additional line is
> basically hidden behind one of the histogram bars, only appearing
> from the top of the bar to the top of the graph.  Is there a way to
> bring the additional line to the top so that it is drawn on top of
> the histogram?

I assume that Eric is using the -xline()- option to specify his vertical
lines.  If so, those lines are "laid down" before the histogram bars are drawn
and are therefore occluded by the bars.  In many (most?) cases this is what
you want -- your plots placed atop the vertical (or horizontal reference

The position of the -xlines()- cannot be changed, but not to worry.
-histogram- supports the -addplot()- option, so we can add any -twoway- plot
to our histogram, and because those plots will be drawn after the histogram
bars, they will overlay the bars.

Here is a simple example using the auto data set.

      . sysuse auto
      . hist mpg, addplot(pci 0 20 .1 20)

In the -addplot()- option, we specified the immediate form of the parallel
coordinates plot, -pci-.  (We could have used other plottypes, but this seemed
easiest.)  With the 2 supplied coordinates, we are just connecting the line
from the point (y=0,x=20) to (y=.1,x=20) -- a vertical line at x=20.  I had
already peeked at the histogram and seen that the y-axis ranged from 0 to .1.

If Eric wants more vertical lines, he can just add more sets of coordinates.
Eric may also want to add the option -legend(off)- because -histogram- assumes
that you want each of your plots identified, and I doubt that Eric does.

Note that if we add more coordinates pairs, each line will be drawn in the
same style -- color, thickness, and pattern.  All of the lines are a single
plot and thus share a style.  A little-known feature of -addplot()- is that
you can add more than one plot and thus draw plots with different styles.
Just separate the plots with a -||- as you would on the -twoway- command.
Expanding on our example,

      . hist mpg, addplot(pci 0 20 .1 20 0 30 .1 30 || pci 0 25 .1 25)

Draws vertical lines at 20 and at 30 with a common style, and another line at
25 (from the second -pci-) with a different style.  We can also change the
look of any of the plots using the standard -lcolor()-, -lwidth()-, and
-lpattern()- options allowed on all -pci- plots.

We can even add plots of a different type, say labeled marker symbols, by
adding something like -|| scatteri .05 35 "my text"- after the last -pci-

-- Vince

*   For searches and help try:

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