Skip to content
Prev 307106 / 398506 Next

Multiple graphs > boxplot

Hello,

I've once written a function that does more or less what you want, but 
it has no formula interface.

# Input:
#    x - matrix or data.frame of numeric vectors to graph
#    by - a factor or coercible to factor
multi.boxplot <- function(x, by, col=0, ...){
     x <- as.data.frame(x)
     uniq.by <- unique(by)
     by <- factor(by)
     len <- length(uniq.by) - 1
     n <- ncol(x)
     n1 <- n + 1
     col <- rep(col, n)[seq_len(n)]
     boxplot(x[[ 1 ]] ~ by, at = 0:len*n1 + 1,
         xlim = c(0, (len + 1)*n1), ylim = range(unlist(x)), xaxt = "n", 
col=col[1], ...)
     for(i in seq_len(n)[-1])
         boxplot(x[[i]] ~ by, at = 0:len*n1 + i, xaxt = "n", add = TRUE, 
col=col[i], ...)
     axis(1, at = 0:len*n1 + n1/2, labels = uniq.by, tick = TRUE)
}


a <- matrix(data=runif(300,max=2), nrow=100, ncol=3)
fac <- sample(letters[1:4], 100, TRUE)

multi.boxplot(a, fac)


Hope this helps,

Rui Barradas
Em 05-10-2012 17:01, David Gramaje escreveu: