# Galton's data on the heights of parents and their children
#load and see
library(HistData)

dta <- HistData::Galton
help(Galton)
## starting httpd help server ... done
head(dta)
##   parent child
## 1   70.5  61.7
## 2   68.5  61.7
## 3   65.5  61.7
## 4   64.5  61.7
## 5   64.0  61.7
## 6   67.5  62.2
## show a matrix 2x2 with (2, 0, 1, 3) four element
zones <- matrix(c(2, 0, 1, 3), ncol=2, byrow=TRUE)

zones
##      [,1] [,2]
## [1,]    2    0
## [2,]    1    3
##
#use layout to draw matrix picture with widths 4:1 and heights 1:4
layout(zones, widths=c(4/5, 1/5), heights = c(1/5, 4/5))
# Define xh and yh with parent and child. Don't draw picture.
xh <- with(dta, hist(parent, plot=FALSE))

yh <- with(dta, hist(child, plot=FALSE))
# Define ub as max values of counts
ub <- max(c(xh$counts, yh$counts))
#Setting edge distance 
par(mar=c(3, 3, 1, 1))
# draw sunflowerplot with parent and child
with(dta, sunflowerplot(parent, child))
#Setting edge distance
par(mar=c(0, 3, 1, 1))
# draw barplot. Don't set coordinate axis . set range of y. Finally, interval of barplot = 0.
barplot(xh$counts, axes=FALSE, ylim=c(0, ub), space=0)
#Setting edge distance
par(mar=c(3, 0, 1, 1))
# draw barplot. Don't set coordinate axis . set range of x. interval of barplot = 0. Finally, adjust picture direction.

barplot(yh$counts, axes=FALSE, xlim=c(0, ub), space=0, horiz=TRUE)
#Setting edge distance
par(oma=c(3, 3, 0, 0))
## Add the words outside the picture , "at" show Tick marks location.

mtext("Average height of parents (in inch)", side=1, line=2, 
      outer=TRUE, adj=0, 
      at=.4 * (mean(dta$parent) - min(dta$parent))/(diff(range(dta$parent))))
mtext("Height of child (in inch)", side=2, line=2, 
      outer=TRUE, adj=0,
      at=.4 * (mean(dta$child) - min(dta$child))/(diff(range(dta$child))))

###