Book Image

R Graphs Cookbook Second Edition

Book Image

R Graphs Cookbook Second Edition

Overview of this book

Table of Contents (22 chapters)
R Graphs Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

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 the graph, but in addition to the dataset, we also set the upper.panel argument to panel.cor, which is a function that we define beforehand. The upper.panel argument refers to the squares in the top-right half of the preceding...