Correlation matrix using pairs plots

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.

Getting ready

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.

How to do it...

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)

How it works...

We have basically used the pairs() function to create ...

Get R: Data Analysis and Visualization now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.