In this recipe, we will learn how to create a correlation matrix, which is a handy way of quickly finding out which variables in a dataset are correlated with each other.
To try out this recipe, simply type it in the command prompt. You can also choose to save the recipe as a script so that you can use it again later on.
We will use the iris
flowers dataset that we first used in the pairs plot recipe in Chapter 1, R Graphics:
panel.cor <- function(x, y, ...) { par(usr = c(0, 1, 0, 1)) txt <- as.character(format(cor(x, y), digits=2)) text(0.5, 0.5, txt, cex = 6* abs(cor(x, y))) } pairs(iris[1:4], upper.panel=panel.cor)