Multiple regression lines in ggpairs

February 16, 2016

(This article was first published on data prone - R, and kindly contributed to R-bloggers)


Plots including multiple regression lines are added to a matrix of plots generated with the GGally package in R.1


Built upon ggplot2, GGally provides templates
for combining plots into a matrix through the ggpairs function.
Such a matrix of plots can be useful for quickly exploring the
relationships between multiple columns of data in a data frame.
The lower and upper arguments to the ggpairs function specifies
the type of plot or data in each position of the lower or upper diagonal
of the matrix, respectively.
For continuous X and Y data, one can specify the smooth option to
include a regression line.
To include more than one regression line in a plot (or to customize the
plot in any way beyond one of the predefined types), one simply
defines a function that returns the desired plot.
The function can then be included in the list provided to upper or

The code

The following code demonstrates inclusion of two regression lines
in each plot in the lower diagonal elements in which both X and Y
data are continuous.

The plot

The plot generated by the above code is included here.
Note that the default transparency of the
confidence interval works fairly well for being able to compare
the two types of regression curves.

Plot generated by above code

  1. Credit to an excellent answer on Stack Overflow.

To leave a comment for the author, please follow the link and comment on their blog: data prone - R. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


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)