In this article we will be comparing pairs and ggpairs function in R.

  1. pairs() in R pairs() function mainly used to plot a scatter diagram corresponding to each data frame. This will return with Color, Labels, Panels, and by Group in-pairs plot. We take an example: We have in-built data of mtcars we will be using the same. The package required for it is mtcars
data <- dplyr::select(mtcars,mpg,disp,hp)
par(pairs(data))

## named list()
#Here we used par() function to resolve marginal error

Also we can modify the color, shape of points,labels and titles

pairs(data,
      col = "blue",                                           pch = 8,                                          
      abels = c("var1", "var2", "var3"),                      main = "Pair Plot in R")
## Warning in plot.window(...): "abels" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "abels" is not a graphical parameter
## Warning in title(...): "abels" is not a graphical parameter
## Warning in plot.window(...): "abels" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "abels" is not a graphical parameter
## Warning in title(...): "abels" is not a graphical parameter
## Warning in axis(side = side, at = at, labels = labels, ...): "abels" is not a
## graphical parameter
## Warning in plot.xy(xy.coords(x, y), type = type, ...): "abels" is not a
## graphical parameter
## Warning in plot.window(...): "abels" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "abels" is not a graphical parameter
## Warning in title(...): "abels" is not a graphical parameter
## Warning in axis(side = side, at = at, labels = labels, ...): "abels" is not a
## graphical parameter
## Warning in plot.xy(xy.coords(x, y), type = type, ...): "abels" is not a
## graphical parameter
## Warning in plot.window(...): "abels" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "abels" is not a graphical parameter
## Warning in title(...): "abels" is not a graphical parameter
## Warning in axis(side = side, at = at, labels = labels, ...): "abels" is not a
## graphical parameter
## Warning in plot.xy(xy.coords(x, y), type = type, ...): "abels" is not a
## graphical parameter
## Warning in plot.window(...): "abels" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "abels" is not a graphical parameter
## Warning in title(...): "abels" is not a graphical parameter
## Warning in plot.window(...): "abels" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "abels" is not a graphical parameter
## Warning in title(...): "abels" is not a graphical parameter
## Warning in plot.xy(xy.coords(x, y), type = type, ...): "abels" is not a
## graphical parameter
## Warning in plot.window(...): "abels" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "abels" is not a graphical parameter
## Warning in title(...): "abels" is not a graphical parameter
## Warning in axis(side = side, at = at, labels = labels, ...): "abels" is not a
## graphical parameter
## Warning in plot.xy(xy.coords(x, y), type = type, ...): "abels" is not a
## graphical parameter
## Warning in plot.window(...): "abels" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "abels" is not a graphical parameter
## Warning in title(...): "abels" is not a graphical parameter
## Warning in plot.xy(xy.coords(x, y), type = type, ...): "abels" is not a
## graphical parameter
## Warning in plot.window(...): "abels" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "abels" is not a graphical parameter
## Warning in title(...): "abels" is not a graphical parameter
## Warning in axis(side = side, at = at, labels = labels, ...): "abels" is not a
## graphical parameter

## Warning in axis(side = side, at = at, labels = labels, ...): "abels" is not a
## graphical parameter

Changing the pch value we will be able to change the shape of plot. Now, we change the pch value, observe the plot

pairs(data,
      col = "blue",                                           pch = 5,                                          
      abels = c("var1", "var2", "var3"),                      main = "Piar Plot in R")
## Warning in plot.window(...): "abels" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "abels" is not a graphical parameter
## Warning in title(...): "abels" is not a graphical parameter
## Warning in plot.window(...): "abels" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "abels" is not a graphical parameter
## Warning in title(...): "abels" is not a graphical parameter
## Warning in axis(side = side, at = at, labels = labels, ...): "abels" is not a
## graphical parameter
## Warning in plot.xy(xy.coords(x, y), type = type, ...): "abels" is not a
## graphical parameter
## Warning in plot.window(...): "abels" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "abels" is not a graphical parameter
## Warning in title(...): "abels" is not a graphical parameter
## Warning in axis(side = side, at = at, labels = labels, ...): "abels" is not a
## graphical parameter
## Warning in plot.xy(xy.coords(x, y), type = type, ...): "abels" is not a
## graphical parameter
## Warning in plot.window(...): "abels" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "abels" is not a graphical parameter
## Warning in title(...): "abels" is not a graphical parameter
## Warning in axis(side = side, at = at, labels = labels, ...): "abels" is not a
## graphical parameter
## Warning in plot.xy(xy.coords(x, y), type = type, ...): "abels" is not a
## graphical parameter
## Warning in plot.window(...): "abels" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "abels" is not a graphical parameter
## Warning in title(...): "abels" is not a graphical parameter
## Warning in plot.window(...): "abels" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "abels" is not a graphical parameter
## Warning in title(...): "abels" is not a graphical parameter
## Warning in plot.xy(xy.coords(x, y), type = type, ...): "abels" is not a
## graphical parameter
## Warning in plot.window(...): "abels" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "abels" is not a graphical parameter
## Warning in title(...): "abels" is not a graphical parameter
## Warning in axis(side = side, at = at, labels = labels, ...): "abels" is not a
## graphical parameter
## Warning in plot.xy(xy.coords(x, y), type = type, ...): "abels" is not a
## graphical parameter
## Warning in plot.window(...): "abels" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "abels" is not a graphical parameter
## Warning in title(...): "abels" is not a graphical parameter
## Warning in plot.xy(xy.coords(x, y), type = type, ...): "abels" is not a
## graphical parameter
## Warning in plot.window(...): "abels" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "abels" is not a graphical parameter
## Warning in title(...): "abels" is not a graphical parameter
## Warning in axis(side = side, at = at, labels = labels, ...): "abels" is not a
## graphical parameter

## Warning in axis(side = side, at = at, labels = labels, ...): "abels" is not a
## graphical parameter

2. ggpairs in R

The ggpairs() function requires GGally package that allows to build scatterplot matrix. Scatterplots of each pair visualized in left side of the plot and Pearson correlation value and significance displayed on the right side.

To use ggpairs() function we need to install and load packages required.

To install certain package the syntax is : install.packages(“packagename”) Here, we require ggplot2 and GGally package.. If not installed we install it using install.packages(“GGally”) command…similarly for other packages too..

#As have already installeed the package I will load package using library() function
library(ggplot2)
library(GGally)
## Registered S3 method overwritten by 'GGally':
##   method from   
##   +.gg   ggplot2
#We plot data
ggpairs(data)+theme_bw()

We can visualize it in different manners too..

ggpairs(data,
        upper = list(continuous = "density", combo = "box_no_facet"),
        lower = list(continuous = "points" , combo ="dot_no_facet"))

Lets plot for some other dataset We use tips from reshape package

data(tips, package = "reshape")
ggpairs(
tips[, c(1, 3, 4, 2)],
upper = list(continuous = "density", combo = "box_no_facet"),
lower = list(continuous = "points", combo = "dot_no_facet"))

data("flea")
ggpairs(flea, columns = 2:4, ggplot2::aes(colour=species))

Conclusion : ggpairs plots provide useful information and it is handy to use.