densityToPolygon: A function for turning a kernel density estimate into x,y...

View source: R/densityToPolygon.R

densityToPolygonR Documentation

A function for turning a kernel density estimate into x,y points that can be interpretted as a polygon

Description

densityToPolygon takes a kernel density estimate object (returned by stats::density) and turns this into x and y coordinates that then be plotted using R's polygon function.

Usage

densityToPolygon(d)

Arguments

d

a kernel density estimate object from the R function kde.

Value

The function returns a list containing two numeric vectors named x and y. These are the x and y coordinates for the vertices of a polygon that can be plotted as a probability density function.

Examples

samples <- rnorm(1000)
d <- density(samples)
p <- densityToPolygon(d)
m = max(d$y)
plot(d, ylim = c(0, m*1.25), xlab = "Estimate", col = "white", main = "Hatch Probability (Sterile Mated)", ylab = "Density")
polygon(p$x, p$y, col = "blue")

dpagendam/friedsIndex documentation built on July 17, 2022, 5:41 a.m.