density2D: Create matrix representing 2D density.

View source: R/graphics2d.R

density2DR Documentation

Create matrix representing 2D density.

Description

For now, this function only allows a Gaussian kernel for 2D density estimates. This is probably most useful in making figures describing data with too many coordinates to put in a single scatter diagram ('graphics::points()').

Usage

density2D(
  x,
  y,
  bw = 1,
  weights = NULL,
  n = 100,
  from = NULL,
  to = NULL,
  cut = 3,
  na.rm = FALSE
)

Arguments

x

Vector of X coordinates for density estimation.

y

Vector of Y coordinates for density estimation of the same length as 'x'.

bw

Width of the Gaussian kernel, can be a single number or have two values, to be applied along x and y axes, respectively. In contrast to 'density()' there is no good default value (set to 1 for now).

n

Number of points at which to get density estimates, can be a single integer, or two, to be applied along the x and y axes respectively.

from

Start of the interval in which to do density estimation, can be a single value, or two, to be applied along the x and y axes respectively.

to

End of the interval in which to do density estimation, can be a single value, or two, to be applied along the x and y axes respectively.

cut

Used if 'from' and 'to' are not given: density is estimated for the minimum and maximum of the 'x' of the 'y' vectors plus/minus cut*bw.

na.rm

Boolean: remove NA values or not.

Details

?

Value

The functions returns a list with 3 entries: x: x coordinates of density estimates y: y coordinates of density estimates z: a matrix of size (x,y) with density estimates These can be used directly for input into 'contour()', 'filled.contour()', 'image()' and others (see 'lattice' package).

Examples

x <- c(rnorm(n=100, mean=5), rnorm(n=200, mean=3, sd=2))
y <- c(rnorm(n=100, mean=2), rnorm(n=200, mean=6, sd=2))
dens2d <- density2D(x=x, y=y, bw=0.5)
contour(x=dens2d$x, y=dens2d$y, z=dens2d$z)
points(x,y,col='red')

thartbm/SMCL documentation built on Oct. 23, 2022, 5:17 a.m.