### Friday, February 17, 2006

## st: RE: line in fron of histogram

This came up only a few days ago:

Re: st: Graph overlay order --------------------------------------------------------------------------------

From: n j cox <n.j.cox@durham.ac.uk> 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()-).

-------------------------------------------------------------------------------- Eric G. Wruck <ewruck@econalytics.com> 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 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.

Nick n.j.cox@durham.ac.uk

Feiveson, Alan > I would appreciate it if someone would tell me if there is a > simple option to make a line appear in front of a histogram instead of > partially covered by it. For example in > > hist b if sot==6,xline(.50,lwidth(thick)) bcolor(ltblue) > blcolor(black) > > the line is partially covered. Is there an option to move the xline to > the foreground, or for that matter make the bars transparent? I have > spent much time searching the manual, help files, findit, to no avail.

* * 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/

Tag: statalist