##This mini-library defines a useful pair plotting function ##Define a few panel functions for use during the pair plotting (taken from pairs) panel.hist <- function(x, ...) { usr <- par("usr"); on.exit(par(usr)) par(usr = c(usr[1:2], 0, 1.5) ) try(h <- hist(x,plot=FALSE,breaks="fd"),silent=TRUE) #try and recoup otherwise if(length(grep("^h$",ls())) == 0 || #if hist failed, variable is *NOT* present !all.equal(class(h),"histogram")) { #or if h exists, but not as a histogram class h <- hist(x,plot=FALSE); #try again w/o fd breaks } breaks <- h$breaks; nB <- length(breaks) y <- h$counts; y <- y/max(y) rect(breaks[-nB], 0, breaks[-1], y, col="cyan", ...) } panel.cor <- function(x, y, digits=2, prefix="", cex.cor) { usr <- par("usr"); on.exit(par(usr)) par(usr = c(0, 1, 0, 1)) r <- cor(x, y,use="pair") txt <- format(c(r, 0.123456789), digits=digits)[1] txt <- paste(prefix, txt, sep="") if(missing(cex.cor)) cex.cor <- 0.8/strwidth(txt) if(is.na(r)) { rcol <- "gray"; } else { if(r > 0) { rcol <- rgb(r,0,0) #positive = red, negative = blue } else { rcol <- rgb(0,0,-r) #positive = red, negative = blue }} text(0.5, 0.5, txt,col=rcol,cex=cex.cor) } panel.smooth2 <- function(x, y, col = par("col"), bg = NA, pch = par("pch"), cex = 1, col.smooth = "red", span = 2/3, iter = 3, ...) { points(x, y, pch = pch, col = col, bg = bg, cex = cex,xlim=c(0,1),ylim=c(0,1)) #add the explicit limits to allow NA plots? ok <- is.finite(x) & is.finite(y) if (any(ok)) lines(stats::lowess(x[ok], y[ok], f = span, iter = iter), col = col.smooth, ...) } pairz <- function(x,...) { pairs(x,lower.panel=panel.smooth2,upper.panel=panel.cor,diag.panel=panel.hist,...) }