R pairs & ggpairs Plot Functions | 5 Example Codes (Color, Labels, Panels & by Group)

 

Basic R Syntax:

pairs(data)

 

The pairs R function returns a plot matrix, consisting of scatterplots for each variable-combination of a data frame. The basic R syntax for the pairs command is shown above.

In the following tutorial, I’ll explain in five examples how to use the pairs function in R.

If you want to learn more about the pairs function, keep reading…

 

Example 1: Basic Application of pairs() in R

I’m going to start with a very basic application of the pairs R function. Let’s first create some random data for this example:

set.seed(525354)                       # Set seed for reproducibility
N <- 1000                              # Sample size of 1000
 
x1 <- rnorm(N)                         # Create variable
x2 <- x1 + rnorm(N, 0, 3)              # Create correlated variable
x3 <- 2 * x1 - x2 + rnorm(N, 0, 2)     # Create another correlated variable
 
data <- data.frame(x1, x2, x3)         # Combine all variables to data.frame

Our example data contains three numeric variables and 1,000 rows.

Now, let’s apply the pairs function in R:

pairs(data)                            # Apply pairs function

 

R Pairs Plot Basic

Figure 1: Basic pairs() R Plot.

 

As you can see, we are able to produce a relatively complex matrix of scatterplots with only one line of code. So, what does this pairs plot actually contain?

  • The diagonal shows the names of the three numeric variables of our example data.
  • The other cells of the plot matrix show a scatterplot (i.e. correlation plot) of each variable combination of our data frame. The middle graphic in the first row illustrates the correlation between x1 & x2; The right graph in the first row illustrates the correlation between x1 & x3; The left figure in the second row illustrates the correlation between x1 & x2 once more and so on…

In this first example, I have shown you the most basic usage of pairs in R. Let’s modify the options of the function a little bit…

 

Example 2: Selecting Variables of pairs Plot

Often, you will only be interested in the correlations of a few of your variables. Fortunately, this can be done easily by specifying a formula within the pairs command:

pairs(~ x1 + x2 + x3, data = data)     # Produces same plot as in Example 1

With the code above, we can create exactly the same plot as in Example 1. However, we can simply remove the variables from the formula, for which we don’t want to produce a scatterplot:

pairs(~ x1 + x3, data = data)          # Leave out one variable

 

Leaving Out a Variable of pairs R Plot

Figure 2: Pairs Plot with Selection of Variables.

 

In this example, I deleted x2 from the formula, leading to a plot matrix that contains only the scatterplots of x1 and x3.

More modifications? Let’s do it!

 

Example 3: Modify Color, Shape of Points, Labels & Title

In this example, I’m going to modify many different things:

pairs(data[ , 1:3],
      col = "red",                                         # Change color
      pch = 18,                                            # Change shape of points
      labels = c("var1", "var2", "var3"),                  # Change labels of diagonal
      main = "This is a nice pairs plot in R")             # Add a main title

 

Pairs Plot in R with Color

Figure 3: R Pairs Plot with Manual Color, Shape of Points, Labels, and Main Title.

 

The modified pairs plot has a different color, diamonds instead of points, user-defined labels, and our own main title. For even more options, have a look at the help documentation of pairs by typing ?pairs to the RStudio console.

 

Example 4: Modify pairs R Plot by Group

Quite often you will have different subsets or subgroups in your data. Let’s add a group indicator (three groups 1, 2 & 3) to our example data to simulate such a situation:

group <- NA
group[data$x1 < - 0.5] <- 1
group[data$x1 >= - 0.5 & data$x1 <= 0.5] <- 2
group[data$x1 > 0.5] <- 3

Now, let’s apply the pairs function again, but this time dependent on the group variable:

pairs(data[ , 1:3],
      col = c("red", "cornflowerblue", "purple")[group],   # Change color by group
      pch = c(8, 18, 1)[group],                            # Change points by group
      labels = c("var1", "var2", "var3"),
      main = "This is an even nicer pairs plot in R")

 

R Programming Plot via Pairs Function with Modified Color by Group

Figure 4: pairs() Plot with Color & Points by Group.

 

As you can see in Figure 4, we colored the plots and changed the shape of our data points according to our groups.

You need even more options? No problem, let’s move on…

 

Example 5: ggpairs R Function [ggplot2 & GGally]

So far, we have only used the pairs function that comes together with the base installation of R. However, the ggplot2 and GGally packages provide an even more advanced pairs function, which is called ggpairs(). Let’s install and load the packages:

install.packages("ggplot2")            # Packages need to be installed only once
install.packages("GGally")
 
library("ggplot2")                     # Load ggplot2 package
library("GGally")                      # Load GGally package

The basic application of ggpairs is similar to the pairs function of base R. You simply have to write the following R code:

ggpairs(data)                          # Apply ggpairs function

 

ggpairs R plot via ggplot2 & GGally

Figure 5: ggpairs R Plot via ggplot2 & GGally packages.

 

Main difference to the pairs function of base R: The diagonal consists of the densities of the three variables and the upper panels consist of the correlation coefficients between the variables.

Even better than pairs of base R, isn’t it? However, there is even more to explore. In case, you want to know more about the R ggpairs function, I can recommend the following YouTube video of the channel Dragonfly Statistics:

 

 

Further Reading

 



 

Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.

Menu