local_density_2d: 2d local density estimate.

Description Usage Arguments Examples

Description

2d local density estimate.

Usage

1
2
local_density_2d(x, y, weight, scale=TRUE, nn=0.7, h=0,
    kernel="tcub", degree=2, bounded=FALSE, na.rm=FALSE)

Arguments

x

a numeric vector of x positions

y

a numeric vector of y positions

weight

NULL or a numeric vector providing weights for each observation

h

constant bandwidth. Use if you want a constant bandwith, rather than a bandwidth that varies. If both nn and h are set, each is calculated for each location and the largest is used. If scale = TRUE the unit of this value is standard deviations, otherwise, it's in the units of the data.

nn

A vector of length two, giving the span, or nearest neighbour fraction, the proportion of the total used to compute the density.

scale

should x and y positions be scaled independently? Use FALSE when variables are measured on the same original scale.

kernel

Weight function, default is tricubic. Other choices are "rect", '"trwt"', '"tria"', '"epan"', '"bisq"' and '"gauss"'. Choices may be restricted when derivatives are required; e.g. for confidence bands and some bandwidth selectors.

degree

degree of polynomial used for smoothing

bounded

if TRUE bounds density to range of data, otherwise it takes non-zero values over the entire real plane.

na.rm

If TRUE missing values will be silently removed, otherwise they will be removed with a warning.

Examples

1
2
3
4
5
6
7
8
data(baseball, package = "plyr")
dens <- local_density_2d(baseball$g, baseball$ab)
plot(dens)
baseball$dens <- dens(baseball$g, baseball$ab)

plot(local_density_2d(baseball$g, baseball$ab, nn = 0.4))
plot(local_density_2d(baseball$g, baseball$ab, nn = 0.1))
plot(local_density_2d(baseball$g, baseball$ab, bounded = TRUE))

hadley/densityvis documentation built on May 17, 2019, 9:56 a.m.