p.hist <- 1/10
p.scatter <- 1-p.hist
space <- 4
hist.breaks <- 10
# Thanks to Paul Murrell for marginal histogram inspiration
# http://addictedtor.free.fr/graphiques/RGraphGallery.php?graph=78
generic.scatter.plot <- function(x,y,ann=NULL,pch=1,
fit.lty=0,axis.round=2,
lty.x.y=0,one.to.one=F,
marginal=T,
xlab='Label this with the "xlab" argument',
ylab='Label this with the "ylab" argument',
main='Label this with the "main" argument',
xlim=NULL,
ylim=NULL
) {
if(marginal) {
layout(matrix(c(2,0,1,3),ncol=2,byrow=TRUE),widths=c(p.scatter,p.hist),heights=c(p.hist,p.scatter))
} else {
par(mfrow=c(1,1))
}
par(mar=c(space,space,space,space))
if(one.to.one) {
xi <- yi <- range(x,y)
} else {
xi <- x
yi <- y
}
if(!is.null(xlim))xi <- xlim
if(!is.null(ylim))yi <- ylim
plot(xi,yi,type='n',bty='n',xaxt='n',yaxt='n',xlab=xlab,ylab=ylab,main=main)
axis(1,round(quantile(x),axis.round),lty=0,las=3)
axis(2,round(quantile(y),axis.round),lty=0,las=1)
if(fit.lty) {
fit <- lm(y ~ x)
abline(fit,lty=fit.lty)
}
abline(0,1,lty=lty.x.y)
if(!is.null(ann)) {
text(x,y,ann,xpd=NA)
} else {
points(x,y,pch=pch)
}
mtext(paste('n =',length(x)))
if(marginal) {
xhist <- hist(x,seq(min(xi),max(xi),l=hist.breaks),plot=FALSE)
yhist <- hist(y,seq(min(yi),max(yi),l=hist.breaks),plot=FALSE)
top <- max(c(xhist$counts, yhist$counts))
par(mar=c(0,space,1,space))
barplot(xhist$counts, axes=FALSE, ylim=c(0, top), space=0)
par(mar=c(space,0,space,1))
barplot(yhist$counts, axes=FALSE, xlim=c(0, top), space=0, horiz=TRUE)
}
}
##debug(generic.scatter.plot)