Creating a matrix of scatter plots in R

Scatter plots are 2 dimensional plots that show the relationship between two variables. Here I demonstrate how we can create a matrix of scatter plots in R for datasets that have more than two variables. This is particularly useful when we want to visually inspect whether there are associations between variables.


#store random set of numbers in four variables
a <- runif(10,0,100)
b <- runif(10,0,100)
c <- runif(10,0,100)
d <- runif(10,0,100)
#create one big data.frame
data <- data.frame(a,b,c,d)
data
#          a          b        c         d
#1  81.47277 38.6969373 64.10036 83.194996
#2  18.99613  9.0549361 37.18465 79.644227
#3  22.16861  2.4355091 24.33679 77.087804
#4  64.18384  0.3513288 22.03435 39.354341
#5  57.88510 71.0003202 37.55931 45.650394
#6  21.13935 93.0838322 44.20160 37.522434
#7  35.74084 65.3453410 15.43229 28.034387
#8  25.55078 23.3961467 82.24938 58.981237
#9  57.99774 82.5590614 64.13307 49.083873
#10  5.55767 35.8025412 92.86938  5.454501
#viewing all pairs of scatterplots
pairs(~a+b+c+d,data=data)

To display correlations on the lower panel (since the plots are redundant anyway):


#store random set of numbers in four variables
a <- runif(10,0,100)
b <- runif(10,0,100)
c <- runif(10,0,100)
d <- runif(10,0,100)
#create one big data.frame
data <- data.frame(a,b,c,d)

#define panel.cor function
#code adapted from the R pairs man page
panel.cor <- function(x, y, digits=2, prefix="", cex.cor, ...){
   usr <- par("usr"); on.exit(par(usr))
   par(usr = c(0, 1, 0, 1))
   r = (cor(x, y))
   txt <- format(c(r, 0.123456789), digits=digits)[1]
   txt <- paste(prefix, txt, sep="")
   text(0.5, 0.5, txt,cex=2)
}

pairs(data, lower.panel=panel.cor)




Creative Commons License
This work is licensed under a Creative Commons
Attribution 4.0 International License
.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.