overlapPlot: Plot overlapping kernel densities

View source: R/overlapPlot.R

overlapPlotR Documentation

Plot overlapping kernel densities


Fits kernel density functions to two data sets and plots them, shading the area corresponding to the coefficient of overlap.


overlapPlot(A, B, xscale = 24, xcenter = c("noon", "midnight"),
   linetype = c(1, 2), linecol = c("black", "blue"), linewidth = c(1, 1),
   olapcol = "lightgrey", rug=FALSE, extend=NULL,
   n.grid = 128, kmax = 3, adjust = 1, ...)


A, B

vectors of times of observations for species A and species B in radians, ie. scaled to [0, 2\pi]. Each must include at least 2 unique observations to fit a kernel density.


the scale for the x axis: 24 (the default) produces a curve with 0 to 24 hours. NA gives a scale in radians, labelled with pi.


the center of the plot on the x axis: 'noon' (default) or 'midnight'.


a vector of length 2 giving the line type for each species. Look for lty in par for ways to specify this.


a vector of length 2 giving the line colour for each species. See the Color Specification section in par for details.


a vector of length 2 giving the line width for each species.


the colour to use for the shaded area. See the Color Specification section in par for details.


if TRUE, the original observations will be displayed as a rug at the bottom of the plot, A below B.


If not NULL, the plot extends 3 hours before and after the main 24-hr period, and extend specifies the background colour; the plot is not extended if extend = NULL.


number of points at which to estimate the density for plotting; 100 is usually adequate to give a smooth-looking curve.


maximum value of k for optimal bandwidth estimation.


bandwidth adjustment (scalar).


Further arguments passed to the plotting functions such as main, xlab, ylab, ylim. Values for col, lwd, lty should be passed with linecol, linewidth, linetype.


Returns invisibly a data frame with columns:


a vector of equally-spaced times from midnight to midnight inclusive on the scale specified by xscale.


a vector of length x with the fitted kernel density for species A.


a similar vector for species B.


Mike Meredith

See Also

densityPlot for plotting a single density curve.


# Get example data:

# Do basic plot with defaults:
overlapPlot(pigObs, tigerObs)
# Make it prettier:
overlapPlot(tigerObs, pigObs, linet = c(1,1), linec = c("red", "blue"),
  rug=TRUE, extend="lightgreen", main="Simulated data")
legend("topleft", c("Tiger", "Pig"), lty=1, col=c("red", "blue"), bg="white")
# Add vertical dotted lines to mark sunrise (05:30) and sunset (18:47):
# (times must be in hours if the x-axis is labelled in hours)
abline(v=c(5.5, 18+47/60), lty=3)

# A plot centered on midnight:
overlapPlot(pigObs, tigerObs, xcenter = "m", rug=TRUE)
# Mark sunrise/sunset; values to the left of "00:00" are negative
# so subtract 24:
abline(v=c(5.5, (18+47/60) - 24), lty=3)

overlap documentation built on Nov. 18, 2023, 5:09 p.m.