Fits a kernel density function to a data set and plots it.

1 2 3 |

`A` |
a vector of times of observations in |

`xscale` |
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 |

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

`add` |
If TRUE, the curve will be added to the existing plot. Use the same settings for xscale and xcenter as for the original plot. |

`rug` |
If TRUE, the original observations will be displayed as a rug at the bottom of the plot. |

`extend` |
If not NULL, the plot extends 3 hours before and after the main 24-hr period, and |

`n.grid` |
Number of points at which to estimate the density for plotting; 100 is usually adequate to give a smooth-looking curve. |

`kmax` |
maximum value of k for optimal bandwidth estimation. |

`adjust` |
bandwidth adjustment (scalar). |

`...` |
Further arguments passed to the plotting functions, such as |

Returns invisibly a data frame with x and y coordinates which can be used for further plotting or calculations; see examples.

Mike Meredith

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | ```
# Get example data:
data(simulatedData)
# Do basic plot with defaults:
densityPlot(pigObs)
# Prettier plots:
densityPlot(pigObs, extend=NULL, lwd=2)
densityPlot(pigObs, rug=TRUE, main="Simulated data", extend='gold')
densityPlot(tigerObs, add=TRUE, rug=TRUE, col='red')
legend('topleft', c("Tiger", "Pig"), lty=1, col=c('black', 'red'), bg='white')
# Add vertical dotted lines to mark sunrise (say 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:
densityPlot(pigObs, xcenter = "m")
# 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)
# Using object returned:
densityPlot(pigObs, rug=TRUE, lwd=3)
# Don't like the rug with lwd = 3?
pigDens <- densityPlot(pigObs, rug=TRUE)
lines(pigDens, lwd=3)
# What proportion of the density lies between 9:00 and 15:00 hrs?
wanted <- pigDens$x > 9 & pigDens$x < 15
mean(pigDens$y[wanted]) * 6 # probability mass for the 6 hr period.
# Plotting time in radians:
densityPlot(pigObs, xscale=NA, rug=TRUE)
densityPlot(tigerObs, xscale=NA, add=TRUE, rug=TRUE, col='red')
``` |

Questions? Problems? Suggestions? Tweet to @rdrrHQ or email at ian@mutexlabs.com.

Please suggest features or report bugs with the GitHub issue tracker.

All documentation is copyright its authors; we didn't write any of that.