x<-rnorm(100)
y<-x+rnorm(100)
scatter.hist<-function(x,y,pch=21)
{
## start by saving the original graphical parameters
def.par <- par(no.readonly = TRUE)
## then change the margins around each plot to 1
par("mar" = c(1,1,1,1))
## then set the layout of the graphic
layout(matrix(c(2,1,1,2,1,1,4,3,3), 3, 3, byrow = TRUE))
maxx <- x[which.max(x)]
maxy <- y[which.max(y)]
minx <- x[which.min(x)]
miny <- y[which.min(y)]
plot(x, y, xlab = "", ylab = "", pch = pch, bty = "n",
xlim = c(minx, maxx), ylim = c(miny,maxy))
breaks <- 10
k<-c(seq(from=range(y)[1],to=range(y)[2],maxy/breaks),maxy)
yh <- hist(y, breaks = k, plot = FALSE)
barplot(-(yh$intensities),space=0,horiz=T, axes = FALSE)
axis(1,at=seq(min(-yh$intensities),max(-yh$intensities),.1),labels=sort(seq(min(yh$intensities),max(yh$intensities),.1),decreasing=TRUE))
breaks <- 10
k<-c(seq(from=range(x)[1],to=range(x)[2],maxx/breaks),maxx)
xh <- hist(x, breaks = k, plot = FALSE)
barplot(-(xh$intensities),space=0,horiz=F, axes = FALSE)
axis(2,at=seq(min(-xh$intensities),max(-xh$intensities),.1),labels=sort(seq(min(xh$intensities),max(xh$intensities),.1),decreasing=TRUE))
## reset the graphics display to default
par(def.par)
}
scatter.hist(x,y)
edited from
http://www.r-bloggers.com/quick-scatterplot-with-associated-histograms/