# Load datasets first
data(baseball)
data(auto)
# Most of the following examples are found in the paper by Michael
# Friendly.
# Figure 2
vars2 <- c("Assists","Atbat","Errors","Hits","Homer","logSal",
"Putouts","RBI","Runs","Walks","Years")
corrgram(baseball[,vars2], order=TRUE,
main="Baseball data PC2/PC1 order",
lower.panel=panel.shade, upper.panel=panel.pie,
text.panel=panel.txt)
# Figure 3
baseball.cor <- cor(baseball[,vars2], use='pair')
baseball.eig <- eigen(baseball.cor)$vectors[,1:2]
e1 <- baseball.eig[,1]
e2 <- baseball.eig[,2]
plot(e1,e2,col='white', xlim=range(e1,e2), ylim=range(e1,e2))
arrows(0, 0, e1, e2, cex=0.5, col="red", length=0.1)
text(e1,e2, rownames(baseball.cor), cex=0.75)
# Figure 4a
corrgram(baseball[,vars2], main="Baseball data (alphabetic order)")
# Figure 4b. panel functions explicitly called
corrgram(baseball[,vars2], order=TRUE,
main="Baseball data (PC order)",
panel=panel.shade, text.panel=panel.txt)
# Figure 5
vars5 <- setdiff(colnames(baseball), c("Name","League","Team","Position"))
corrgram(baseball[, vars5], order=TRUE,
main="Baseball data (PC order)")
# Figure 6. Arrangement is slightly different from Friendly.
vars6 <- setdiff(colnames(auto), c("Model", "Origin"))
corrgram(auto[, vars6], order=TRUE, main="Auto data (PC order)")
# Figure 7, 8, 9
# To re-create figure 7, 8 and 9, we need to re-write corrgram to
# work with a correlation matrix instead of a data matrix
# Not yet implemented
# Figure 11.
corrgram(baseball[,vars2], order=TRUE,
main="Baseball correlation ellipses",
panel=panel.ellipse, text.panel=panel.txt, diag.panel=panel.minmax)
# Reverse diagonal, use points in lower part
corrgram(baseball[,vars2], order=TRUE, row1attop=FALSE,
main="Baseball correlation ellipses",
upper.panel=panel.ellipse, lower.panel=panel.pts, diag.panel=panel.minmax)
Run the code above in your browser using DataLab