border.hist <- function(x, y) { #=============================== # Create a graph with a histogram # of the data on the border. #=============================== oldpar <- par(no.readonly = TRUE) # Read the current state xhist <- hist(x, plot=FALSE, breaks = 20) # Create histograms yhist <- hist(y, plot=FALSE, breaks = 20) top <- max(c(xhist$counts, yhist$counts)) # Get max count xrange <- range(x) # min,max yrange <- range(y) # Create a plot canvas that has 2 rows and 2 columns # on which the c(0,2,3,1) gives the order of plotting # 0 = no plot, and c(1,3), c(1,3) give the row and col # size ratios. nf <- layout(matrix(c(0,2,3,1),2,2,byrow=TRUE), c(1,3), c(1,3), TRUE) layout.show(nf) # Set the margin sizes (bottom, left, top, right) par(mar=c(3,3,1,1)) plot(x, y, xlim=xrange, ylim=yrange, xlab="", ylab="") par(mar=c(0,3,1,1)) barplot(xhist$counts, axes=FALSE, ylim=c(0, top), space=0, col=heat.colors(length(xhist$breaks))) par(mar=c(3,0,1,1)) barplot(yhist$counts, axes=FALSE, xlim=c(top, 0), space=0, horiz=TRUE, col=heat.colors(length(yhist$breaks))) par(oldpar) # reset to default }