Skip to content

Instantly share code, notes, and snippets.

@astatham
Created February 27, 2013 23:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save astatham/5052769 to your computer and use it in GitHub Desktop.
Save astatham/5052769 to your computer and use it in GitHub Desktop.
Lineplot of densities
plotDensities <- function(s, col=NULL, xlim=NULL, ylim=NULL, na.rm=TRUE, lty=rep(1, length(s)), ...) {
if (!is.list(s)) s <- list(s)
if (is.null(col)) col <- rainbow(length(s))
sD <- lapply(s, density, na.rm=na.rm)
if (is.null(xlim)) xlim <- range(sapply(sD, function(x) range(x$x)))
if (is.null(ylim))ylim <- range(sapply(sD, function(x) range(x$y)))
plot(0, type="n", xlim=xlim, ylim=ylim, ...)
for (i in 1:length(sD)) lines(sD[[i]], col=col[i], lty=lty[i], ...)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment