Two (related) solutions to adding marginal dot-plots to a scatter plot:
## This scripts is to show how to draw scatter plot with marginal dot diagram. ## I googled and got hints from http://tolstoy.newcastle.edu.au/R/help/04/03/1613.html ##### Exercise 1.2 ## Input the data x1 <- c(1,2,3,4,5,6,7,8,9,11) x2 <- c(18.95,19.00,17.95,15.54,14.00,12.95,8.94,7.49,6.00,3.99) ## Define a layout. Here we devide the plotting area to four subareas. For details, read the help ## file for function "layout". nf <- layout(matrix(c(3,1,0,2),2,2,byrow=TRUE), c(1,5), c(5,1), TRUE) ## Show the layout which give an idea about how the plots will distribute before plotting them. layout.show(nf) ## Plot scatter plot first. ## par(mar = c()) is used to leave proper margins around plots. par(mar=c(5,4,2,2)) plot(x1, x2, xlim=c(0,12), ylim=c(0,20), xlab="x1, Age", ylab="x2, Prices, thousands of dollars") ## Then plot dot diagram under the scatter plot, and dot diagram to the left of it. ## Some setting of axis is just to make the plots look better. par(mar=c(3,4,2,2)) plot(x1, rep(1,10), xlim=c(0,12), ylim=c(1,1), xlab="", ylab="",axes = F) axis(side = 3,at = seq(from = 0, to = 12, by = 2)) par(mar=c(5,3,2,2)) plot(rep(1,10), x2, xlim=c(1,1), ylim=c(0,20), xlab="", ylab="",axes = F) axis(side = 4,at = seq(from = 0, to = 20, by = 5))
The R code included creates the type of scatterplot with marginal dotplots as shown in the book. I basically consulted the help file for the layout function and adapted the code from the second example. It should be noted that I had to manually set the heights on the dotplots (base1 and base2 vectors) by looking at the scatterplot, so this method would need refinement before being applied so a very large large dataset.
#remember to change directory
##1.4
set<-read.table("P1-4.dat")
base1<-c(0,0,0,0,1,0,1,0,0,0)
base2<-c(0,0,0,0,0,0,0,0,0,1)
nf <- layout(matrix(c(2,0,1,3),2,2,byrow=TRUE), c(3,1), c(1,3), TRUE)
layout.show(nf)
par(mar=c(3,3,1,1))
plot(set[,1],set[,2],pch=16)
par(mar=c(3,3,1,1))
plot(set[,1],base1,ylim=c(0,1),pch=16)
plot(base2,set[,2],xlim=c(0,1),pch=16)