Pairwise Scatterplot in R

Pairwise scatterplot in R, A plot matrix made up of scatterplots for each variable combination in a data frame is the output of the pairs R function.

The pairs command’s fundamental R syntax is displayed above.

We’ll demonstrate how to utilize R’s pairs function using five examples in the lesson that follows.

Continue reading to discover more about the pairs function.

Pairwise Scatterplot in R

Basic R Syntax:


Example 1: R’s pairs() Basic Application

We are going to start by using the pairs R function in a very simple application. For this example, let’s first generate some random data:

N <- 1000                              
x1 <- rnorm(N)                         
x2 <- x1 + rnorm(N, 0, 3)              
x3 <- 2 * x1 - x2 + rnorm(N, 0, 2)      
data <- data.frame(x1, x2, x3)         
        x1        x2         x3
1 -0.56047565 -3.547872  1.4037131
2 -0.23017749 -3.350043  3.3635634
3  1.55870831  1.504768  0.5294707
4  0.07050839 -0.326017  2.9054891
5  0.12928774 -7.518741  8.1255878
6  1.71506499  4.836785 -2.6371920

There are 1,000 rows and three numerical variables in our example data.

Let’s use R’s pairs function now:


As you can see, we just need one line of code to generate a surprisingly intricate matrix of scatterplots. What exactly does this plot of pairings contain, then?

The names of the three numerical variables in our example data are displayed on the diagonal.

Each variable combination in our data frame is represented by a scatterplot (also known as a correlation plot) in the other cells of the plot matrix.

Free Best Online Course For Statistics – Data Science Tutorials

The first row’s middle graph shows the relationship between x1 and x2; The correlation between x1 and x3 is shown in the right graph in the first row; the correlation between x1 and x2 is shown in the left figure in the second row; and so on.

Example 2: Choosing Plot Variables for Pairs

Frequently, only a handful of your variables’ correlations will be of interest to you. Fortunately, doing so is simple by including the following formula in the pairs command:

pairs(~ x1 + x2 + x3, data = data)

The plot in Example 1 may be perfectly recreated using the code above. However, we can just take the variables out of the calculation if we don’t want a scatterplot for them:

pairs(~ x1 + x3, data = data)

In this illustration, x2 was removed from the formula, leaving only the scatterplots for x1 and x3 in the plot matrix.

Example 3: Change Labels, Point Shape, Colour, and Title

We are going to tweak a variety of items in this example:

pairs(data[ , 1:3],
col = "red",
pch = 18, 
labels = c("var1", "var2", "var3"), 
main = "This is a nice pairs plot in R")  

How to make a connected scatter plot in R? »

You may also like...

Leave a Reply

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

eighteen + fifteen =