# Galton's data on the heights of parents and their children

##已經安裝完成install.packages("HistData")
##開啟HistData中的Galton資料
dta <- HistData::Galton
##matrix() 函數並指定參數 nrow = 2 將一維的數字向量(1 到 6)轉換成一個 2x3 的矩陣
zones <- matrix(c(2, 0, 1, 3), ncol=2, byrow=TRUE)
##layout()是矩陣,數字代表畫圖的顺序;”0”代表空缺,不畫圖形
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設定詢問繪圖參數
par(mar=c(3, 3, 1, 1))
##sunflowerplot散點圖中的每個點對應一個(x, y)對,如果同一(x, y)對出現多次,點會重疊
with(dta, sunflowerplot(parent, child))

par(mar=c(0, 3, 1, 1))
##barplot()繪製bar chart,barplot()可以設定繪製水平與垂直圖形。 
barplot(xh$counts, axes=FALSE, ylim=c(0, ub), space=0)

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()可在現有圖表的四個邊緣之一加上文字
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))))

###