ggpairs in R- A Brief Introduction to ggpairs

[This article was first published on Methods – finnstats, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

In this article, we are going to compare pairs and ggpairs functions in R.

1. pairs() in R

pairs() function mainly used to plot a scatter diagram corresponding to each data frame.

Syntax: pairs(data)

This will return with Color, Labels, Panels, and by Group in-pairs plot.

We will make use of mtcars package here.

Let’s store some variable into data.

Naive Bayes Classification in R » Prediction Model »

data<-dplyr::select(mtcars,mpg,disp,hp)
pairs(data)
pairs in R

The diagonal boxes are column variables and the remaining combination of variables scatter plots.

How to clean the datasets in R? » janitor Data Cleansing »

You can modify the color, Shape of Points, Labels & Title

pairs(data,
      col = "blue",                                       
      pch = 8,                                          
      abels = c("var1", "var2", "var3"),                 
      main = "Piar Plot in R")
pair plot with colors

You can learn more about pch here. You can directly call from mtcars data set also.

Normality Test in R » How to Perform » Easy Steps »

pairs(~ mpg + disp + hp+wt, data = mtcars)
pairs from dataset

2. ggpairs in R

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

If you are not installed the ggplot2 and GGally, Let’s install it.

install.packages("ggplot2")       
install.packages("GGally")
library("ggplot2")                     
library("GGally")
ggpairs(data)+theme_bw()
ggpairs in r plots

You can visualize differently also,,

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

Let’s make use of some other dataset for better visulization.

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"))
ggpairs better visualization
data(flea)
ggpairs(flea, columns = 2:4, ggplot2::aes(colour=species))
ggpairs

Conclusion

ggpairs plot provides the useful information and handy to use.

Kruskal Wallis test in R-One-way ANOVA Alternative »

Subscribe the Newsletter and COMMENT below!

The post ggpairs in R- A Brief Introduction to ggpairs appeared first on finnstats.

To leave a comment for the author, please follow the link and comment on their blog: Methods – finnstats.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)