Skip to content

Instantly share code, notes, and snippets.

@astatham
Created July 5, 2012 05:58
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/3051652 to your computer and use it in GitHub Desktop.
Save astatham/3051652 to your computer and use it in GitHub Desktop.
lboxplot function - boxplots lists of lists
lboxplot <- function(x, cols=2:(length(x)+1), ...) {
xl <- length(x)
if (xl<2) stop("Length of x must be >=2")
xn <- sapply(x, length)
if (!all(xn==xn[1])) stop ("Element lengths of x must all be the same")
xnames <- sapply(x, names)
if (!all(apply(xnames, 1, function(y) all(y==y[1])))) stop("Each element of x must have matching names")
xn <- xn[1]
xu <- unlist(x, recursive=FALSE)[rep(1:xn, each=xl)+rep((1:xl-1)*xn, xn)]
boxplot(xu, col=cols, xaxt="n", ...)
axis(1, seq(1+(xl-1)/2, length(xu)-(xl-1)/2, length.out=xn), names(x[[1]]), las=2)
abline(v=1:(xn-1)*xl+0.5, lty=2, col="red")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment