Compute a KDE surface based on the density of points along an x- and y-axis.

kde_surface(xyz, n = 50, extend_kde = 1, rescale_z = TRUE)

Arguments

xyz

A data.frame containing the columns "x" and "y" (required). Can also include a third column "z" (optional).

n

Number of grid points in each direction. Can be scalar or a length-2 integer vector.

extend_kde

Extend the limits of the KDE area by setting extend_kde>1. This makes the "mountains' of the KDE end more smoothly along the edges of the x/y coordinates.

rescale_z

Rescale the z-axis so that the KDE can be positioned below the original data points (when plotting them together).

Value

  • x, y: The x and y coordinates of the grid points, vectors of length n.

  • z: An n[1] by n[2] matrix of the estimated density: rows correspond to the value of x, columns to the value of y.

Examples

xyz <- data.frame(x=stats::rnorm(50),
                  y=stats::rnorm(50),
                  z=stats::rnorm(50))
kd <- kde_surface(xyz = xyz)
#> Loading required namespace: MASS