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

Re: st: Kdensity functions on the axes of a scatterplot


From   [email protected] (Jeff Pitblado, StataCorp LP)
To   [email protected]
Subject   Re: st: Kdensity functions on the axes of a scatterplot
Date   Fri, 02 Apr 2004 18:56:28 -0600

"Daniel Egan" <[email protected]> ask about resting kernel density
estimates of two variables on the respective axis in a twoway scatter plot:

> I am interested in creating a scatterplot graph which would include kdensity
> functions aligned along the respective variable axes. This could also be
> (less functionally done) using scatter points along the axes to indicate
> frequency, but this is not optimal.
> 
> The command lines below are as close as I have come to what I want, but I
> would guess that there is some form of tick rule that could be used to
> create these, or at least I hope.
> sysuse auto
> 
> gen xnought = 130
> 
> gen ynought = 30
> 
> twoway (scatter turn length, msymbol(circle) msize(medium)) (scatter ynought
> length, sort msymbol(circle) msize(vsmall)) (scatter turn xnought,
> msymbol(circle) msize(vsmall))
> 
> What I would very much like to end up with is a kdensity function on both
> axes, similar to the one created for the x-axis using:
> twoway (scatter turn length, msize(vsmall)) (kdensity length, yaxis(2) epan
> clpat(shortdash) clwidth(vthin)) (scatter turn xnought), yscale(range(0.0
> 2) axis(2))
> 
> Since the above command line uses up the right axes, I would prefer not to
> use it if possible.  I am using StateSE 8.

Some preliminary work needs to be done to get the KDE's to hug the respective
axis.  Here is an example using Daniel's example.  Note the -horizontal-
option used in the second -kdensity- plot.

***** BEGIN: ex.do
sysuse auto, clear

// add observations to get smoother KDE's
set obs 200

// figure out an upper bound to get KDE's to hug the axis
kdensity length, gen(x h) nograph
sum h
local max_len = (r(max)-r(min))*10
drop x h
kdensity turn, gen(x h) nograph
sum h
local max_turn = (r(max)-r(min))*10

twoway	(scatter turn length,						///
		msize(vsmall)						///
		xtitle("")						///
		yvarlab("`:var lab turn' VS. `:var lab len'")		///
		ytitle("`:var lab turn'")				///
		xtitle("`:var lab length'")				///
	)								///
	(kdensity length,						///
		clpat(shortdash)					///
		yvarlab("kernel density: `:var lab length'")		///
		xaxis(2)						///
		yaxis(2)						///
		xscale(axis(2) off)					///
		yscale(axis(2) range(0 `max_len') off)			///
	)								///
	(kdensity turn,							///
		horizontal						///
		clpat(shortdash)					///
		yvarlab("kernel density: `:var lab turn'")		///
		xaxis(3)						///
		yaxis(3)						///
		xscale(axis(3) range(0 `max_turn') off)			///
		yscale(axis(3) off)					///
	)								///
	||,								///
	// blank

***** END: ex.do


--Jeff
[email protected]
*
*   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/



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