Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

plot.kde

Plot for kernel density estimate


Description

Plot for kernel density estimate for 1- to 3-dimensional data.

Usage

## S3 method for class 'kde'
plot(x, ...)

Arguments

x

object of class kde (output from kde)

...

other graphics parameters:

display

type of display, "slice" for contour plot, "persp" for perspective plot, "image" for image plot, "filled.contour" for filled contour plot (1st form), "filled.contour2" (2nd form) (2-d), "plot3D" (3-d), "rgl" (3-d)

cont

vector of percentages for contour level curves

abs.cont

vector of absolute density estimate heights for contour level curves

approx.cont

flag to compute approximate contour levels. Default is FALSE.

col

plotting colour for density estimate (1-d, 2-d)

col.cont

plotting colour for contours

col.fun

plotting colour function for contours

col.pt

plotting colour for data points

colors

vector of colours for each contour (3-d)

jitter

flag to jitter rug plot (1-d). Default is TRUE.

lwd.fc

line width for filled contours (2-d)

xlim,ylim,zlim

axes limits

xlab,ylab,zlab

axes labels

add

flag to add to current plot. Default is FALSE.

theta,phi,d,border

graphics parameters for perspective plots (2-d)

drawpoints

flag to draw data points on density estimate. Default is FALSE.

drawlabels

flag to draw contour labels (2-d). Default is TRUE.

alpha

transparency value of plotting symbol (3-d)

alphavec

vector of transparency values for contours (3-d)

size

size of plotting symbol (3-d).

Details

For kde objects, the function headers for the different dimensional data are

## univariate
  plot(fhat, xlab, ylab="Density function", add=FALSE, drawpoints=FALSE,
     col.pt="blue", col.cont=1, cont.lwd=1, jitter=FALSE, cont, abs.cont, 
     approx.cont=TRUE, ...) 
  
  ## bivariate
  plot(fhat, display="slice", cont=c(25,50,75), abs.cont, approx.cont=TRUE, 
     xlab, ylab, zlab="Density function", cex=1, pch=1, add=FALSE,
  	 drawpoints=FALSE, drawlabels=TRUE, theta=-30, phi=40, d=4, col.pt="blue", 
  	 col, col.fun, lwd=1, border=1, thin=3, lwd.fc=5, ...)

  ## trivariate
  plot(fhat, display="plot3D", cont=c(25,50,75), abs.cont, approx.cont=TRUE, 
  	 colors, col, col.fun, alphavec, size=3, cex=1, pch=1, theta=-30, phi=40, 
  	 d=4, ticktype="detailed", bty="f", col.pt="blue", add=FALSE, xlab, ylab, 
  	 zlab, drawpoints=FALSE, alpha, box=TRUE, axes=TRUE, ...)

For 1-dimensional data, the plot is a standard plot of a 1-d curve. If drawpoints=TRUE then a rug plot is added. If cont is specified, the horizontal line on the x-axis indicates the cont% highest density level set.

For 2-dimensional data, the different types of plotting displays are controlled by the display parameter. (a) If display="slice" then a slice/contour plot is generated using contour. (b) If display is "filled.contour" or "filled.contour2" then a filled contour plot is generated. The default contours are at 25%, 50%, 75% or cont=c(25,50,75) which are upper percentages of highest density regions. (c) If display="persp" then a perspective/wire-frame plot is generated. The default z-axis limits zlim are the default from the usual persp command. (d) If display="image" then an image plot is generated. Default colours are the default from the usual image command.

For 3-dimensional data, the plot is a series of nested 3-d contours. The default contours are cont=c(25,50,75). The default colors are heat.colors and the default opacity alphavec ranges from 0.1 to 0.5. For ks >= 1.12.0, base R graphics becomes the default plotting engine: to create an rgl plot like in previous versions, set display="rgl".

To specify contours, either one of cont or abs.cont is required. cont specifies upper percentages which correspond to probability contour regions. If abs.cont is set to particular values, then contours at these levels are drawn. This second option is useful for plotting multiple density estimates with common contour levels. See contourLevels for details on computing contour levels. If approx=FALSE, then the exact KDE is computed. Otherwise it is interpolated from an existing KDE grid, which can dramatically reduce computation time for large data sets.

Value

Plots for 1-d and 2-d are sent to graphics window. Plot for 3-d is sent to graphics/RGL window.

Examples

library(MASS)
data(iris)

## univariate example
fhat <- kde(x=iris[,2])
plot(fhat, cont=50, col.cont="blue", cont.lwd=2, xlab="Sepal length")

## bivariate example
fhat <- kde(x=iris[,2:3])
plot(fhat, display="filled.contour", cont=seq(10,90,by=10))
plot(fhat, display="persp", thin=3, border=1, col="white", ticktype="detailed")

## trivariate example
fhat <- kde(x=iris[,2:4])
plot(fhat)
if (interactive()) plot(fhat, display="rgl")

ks

Kernel Smoothing

v1.12.0
GPL-2 | GPL-3
Authors
Tarn Duong [aut, cre], Matt Wand [ctb], Jose Chacon [ctb], Artur Gramacki [ctb]
Initial release
2021-02-06

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.