[R] Density estimation graphs

Charilaos Skiadas skiadas at hanover.edu
Thu Mar 15 17:54:19 CET 2007


On Mar 15, 2007, at 12:37 PM, Mark Wardle wrote:

> Dear all,
>
> I'm struggling with a plot and would value any help!
>
> I'm attempting to highlight a histogram and density plot to show a
> proportion of cases above a threshold value. I wanted to cross- 
> hatch the
> area below the density curve. The breaks and bandwidth are deliberate
> integer values because of the type of data I'm looking at.
>
> I've managed to do this, but I don't think it is very good! It  
> would be
> difficult, for example, to do a cross-hatch using this technique.

Don't know about a cross-hatch, but in general I use "polygon" for  
highlighting areas like that:

allele.plot <- function(x, threshold=NULL, hatch.col='black',
hatch.border=hatch.col, lwd=par('lwd'),...) {
	h <- hist(x, breaks=max(x), plot=F)
	d <- density(x, bw=1)
	plot(d, lwd=lwd, ...)	
	if (!is.null(threshold)) {
		d.t <- d$x>threshold
		d.x <- d$x[d.t]
		d.y <- d$y[d.t]
		polygon(c(d.x[1],d.x,d.x[1]),c(0,d.y,0), col=hatch.col,lwd=1)
	}
}
# some pretend data
s8 = rnorm(100, 15, 5)
threshold = 19			# an arbitrary cut-off
allele.plot(s8, threshold, hatch.col='grey',hatch.border='black')


Perhaps this can help a bit. Btw, what was d.l for?

Haris Skiadas
Department of Mathematics and Computer Science
Hanover College



More information about the R-help mailing list