Galton’s data on the heights of parents and their children

library(HistData)
dta <- HistData::Galton
help(Galton)
## starting httpd help server ... done
dim(dta)
## [1] 928   2
str(dta)
## 'data.frame':    928 obs. of  2 variables:
##  $ parent: num  70.5 68.5 65.5 64.5 64 67.5 67.5 67.5 66.5 66.5 ...
##  $ child : num  61.7 61.7 61.7 61.7 61.7 62.2 62.2 62.2 62.2 62.2 ...
#創一個2列的矩陣
zones <- matrix(c(2, 0, 1, 3), ncol=2, byrow=TRUE)
zones
##      [,1] [,2]
## [1,]    2    0
## [2,]    1    3
# layout()是矩陣,寬度4:1,高度1:4
layout(zones, widths=c(4/5, 1/5), heights = c(1/5, 4/5))

xh <- with(dta, hist(parent, plot=FALSE))

yh <- with(dta, hist(child, plot=FALSE))
# max()最大值
ub <- max(c(xh$counts, yh$counts))
# 設定邊界
par(mar=c(3, 3, 1, 1))
# 繪製向日葵圖  # (花瓣越多,數據重疊越多)
with(dta, sunflowerplot(parent, child))

# 繪製parent長條圖
par(mar=c(0, 3, 1, 1))
barplot(xh$counts, axes=FALSE, ylim=c(0, ub), space=0)

# 繪製child長條圖
par(mar=c(3, 0, 1, 1))
barplot(yh$counts, axes=FALSE, xlim=c(0, ub), space=0, horiz=TRUE)


par(oma=c(3, 3, 0, 0))
# mtext()可在現有圖形的四個邊緣之一加上文字
#文字放在圖形下方、line=2距離約2的位置,outer=TRUE設置在圖形外
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))))

# 在圖形旁邊添加文字(文字放在圖形左方、距離約2的位置,設置在圖形外)
par(oma=c(3, 3, 0, 0))

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))))