DataM: Homework Exercise 0413 - 1

The following R script illustrates how to split the plot region to include histograms on the margins of a scatter diagram using the Galton{HistData} data set. Compile it as a html document with comments on each code chunk.

Load the package

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

Load the data set in package HistData

dta <- HistData::Galton

Plot

# Create a matrix ``zones`` for layout setting
zones <- matrix(c(2, 0, 1, 3), ncol=2, byrow=TRUE)
layout(zones, widths=c(4/5, 1/5), heights = c(1/5, 4/5))

# Plot the histograms
xh <- with(dta, hist(parent, plot=FALSE))
yh <- with(dta, hist(child, plot=FALSE))
ub <- max(c(xh$counts, yh$counts))

# Adjus the inner margins of the plot
par(mar=c(3, 3, 1, 1))

# Plot the sunflower scatter plot
with(dta, sunflowerplot(parent, child))

# Adjus the inner margins of the plot
par(mar=c(0, 3, 1, 1)) 

# Plot barplot ub without coordinate axis, specific y range, and no interval
barplot(xh$counts, axes=FALSE, ylim=c(0, ub), space=0)

# Adjus the inner margins of the plot
par(mar=c(3, 0, 1, 1))

# Plot barplot ub without coordinate axis, specific y range, no interval, and adjusted direction
barplot(yh$counts, axes=FALSE, xlim=c(0, ub), space=0, horiz=TRUE)

# Adjus the outer margins of the plot
par(oma=c(3, 3, 0, 0))

# Add texts (label of two dimensions) on the plot with assigned locations
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))))

Jay Liao

2020-04-17