Learn R Programming

corrgram (version 1.3)

corrgram: Draw a correlogram

Description

The corrgram function produces a graphical display of a correlation matrix, called a correlogram. The cells of the matrix can be shaded or colored to show the correlation value.

Usage

corrgram(x, type=NULL, order = FALSE, labels, panel=panel.shade, ...,
  lower.panel=panel, upper.panel=panel, diag.panel=NULL,
text.panel=textPanel, label.pos=0.5, label.srt=0, cex.labels=NULL,
font.labels=1, row1attop=TRUE, dir="left", gap=0, abs=FALSE)

Arguments

x
A tall data frame with one observation per row. Only numeric values are allowed.
type
Use 'data' or 'cor'/'corr' to specify that 'x' is data or a correlation matrix.
order
Should variables be re-ordered? Use TRUE/"PCA" for PCA-based re-ordering, "OLO" for optimal leaf ordering.
labels
Labels to use (instead of data frame variable names) for diagonal panels
panel
function used to plot the contents of each panel
...
additional arguments passed to methods
lower.panel, upper.panel
Separate panel functions used below/above the diagonal
diag.panel, text.panel
panel function used on the diagonal
label.pos
vertical placement of label in diagonal panels
label.srt
String rotation for diagonal labels
cex.labels, font.labels
Graphics parameter for diagonal panels
row1attop
TRUE for diagonal like " ", FALSE for diagonal like " / ".
dir
Use dir="left" instead of 'row1attop'
gap
Distance between panels
abs
Use absolute value of correlations for clustering? Default FALSE

Value

  • No value is returned. A plot is created.

Details

Non-numeric columns in the data will be ignored. This function is basically a modification of the pairs.default function with the use of customized panel functions. The off-diagonal panels are specified with panel.pts, panel.pie, panel.shade, panel.bar, panel.ellipse, panel.conf. Diagonal panels are specified with panel.txt, panel.minmax, panel.density. The panel functions use col.corrgram to specify the colors. Change this function if you desire to use a different palette. TODO: legend, grid graphics version.

References

Friendly, Michael. 2002. Corrgrams: Exploratory Displays for Correlation Matrices. The American Statistician, 56, 316--324. url{http://www.math.yorku.ca/SCS/Papers/corrgram.pdf} A SAS macro by Michael Friendly is at http://www.math.yorku.ca/SCS/sasmac/corrgram.html.

See Also

The plotcorr function in the ellipse package has some similarities.

Examples

Run this code
# Demonstrate density panel, confidence panel
corrgram(iris, lower.panel=panel.pts, upper.panel=panel.conf,
         diag.panel=panel.density)

# Most of the following are from 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 4b
corrgram(baseball[,vars2], order=TRUE,
         main="Baseball data (PC order)",
         panel=panel.shade, text.panel=panel.txt)

# Figure 5
corrgram(baseball, order=TRUE, main="Baseball data (PC order)")

# Figure 6. Slightly different from Friendly.
corrgram(auto, order=TRUE, main="Auto data (PC order)")

# For figure 7, 8, see this package's test suite

# Figure 11.  
corrgram(baseball[,vars2], order=TRUE,
         main="Baseball correlation ellipses",
         panel=panel.ellipse, text.panel=panel.txt, diag.panel=panel.minmax)

Run the code above in your browser using DataLab