# Re: st: Graph overlay order

 From n j cox To statalist@hsphsun2.harvard.edu Subject Re: st: Graph overlay order Date Tue, 14 Feb 2006 17:05:37 +0000

Vince Wiggins posted a solution on 18 Nov 2005, assuming Stata 9.
The implication for Stata 8 users is to play with -plot()- (not -addplot()-).

--------------------------------------------------------------------------------

> 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 lines).

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- specification.
--------------------------------------------------------------------------------
Bradley D. De Groot, D.V.M., Ph.D.

I'm still using STATA 8.2.

I have plotted a histogram with a normal curve centered at the mean and scaled to the standard deviation using:

graph twoway histogram Wgt if Lot == 9,freq width(25) xline(575) ||function SD = normden((x - 575)/49)*30, range(420 750) xscale(range(350 900)) xlabel(400(100)800) title("Lot 9 Histogram, Mean, Standard Deviation")

The 'xline(575) produces a line from the x axis that is just the way I want it to look, except it is behind the histogram rather than in front of it.

I've tried placing the 'xline' option in the histogram statement and in the function statement but in either case the line is plotted behind the histogram.

Is it possible to control the overlay order of added lines? I've looked for control of overlay orders for lines in added_line_options, addedlinestyle, lines and linestyle. Have I missed it in there somewhere? I'll be grateful for any advice.
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/