# Re: st: overlaying two histograms (or distribution curves)

 From Austin Nichols To statalist@hsphsun2.harvard.edu Subject Re: st: overlaying two histograms (or distribution curves) Date Fri, 20 Nov 2009 10:47:06 -0500

```Maarten--
I think that is the same graph I gave for comparison purposes, but I
don't think it compares well with -byhist- unless one takes a bit more
care on the -kdensity- side--the kernel density estimates should at
least use the same bandwidths, and perhaps the same estimation points
if we really wish to compare them.  Other considerations might apply
if Ariel told us something about the theoretical distribution of the
variable filling the role of "price" (is it discrete? does it have a
finite range?).

clear all
sysuse auto
ren foreign f
byhist price, by(f) name(b) tw(leg(col(1)))
loc l leg(lab(1 "Domestic") lab(2 "Foreign") col(1)) xti(Price)
tw kdensity price if !f, bw(900)||kdensity price if f, name(k) bw(900) `l'
gr combine k b, nocopies name(compar)
tw kdensity price if !f, bw(600)||kdensity price if f, bw(600) `l'
gr combine k Graph, nocopies name(compark)
kdensity price, nogr gen(x pdf)
loc b=r(bwidth)
kdensity price if !f, nogr bw(`b') gen(f0) at(x)
kdensity price if f, nogr bw(`b') gen(f1) at(x)
line f0 f1 x
g df=f1-f0
kdensity price if !f, nogr bw(900) gen(g0) at(x)
kdensity price if f, nogr bw(900) gen(g1) at(x)
line g0 g1 x, name(c9)
g dg=g1-g0
line df dg x, name(diffdpf)

Try also:
ssc inst vioplot, replace
vioplot price, over(f) horiz

On Fri, Nov 20, 2009 at 10:09 AM, Maarten buis <maartenbuis@yahoo.co.uk> wrote:
> --- On Fri, 20/11/09, Ariel Linden, DrPH wrote:
>> Is there a way to overlay one histogram (or just the
>> density curve)over another so that they align accordingly
>
> *------------- begin example ---------------
> sysuse auto, clear
> twoway kdensity price if foreign ==0 || ///
>       kdensity price if foreign == 1
> *-------------- end example ----------------
>

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```