Panel function for correlation in scatter plot matrix

This function adds a panel routine to the pairs() command. The panel can be the upper or the lower panel. The result is a text entry that shows the correlation coefficient, p-value and number of observations. You can alter the text size and the method of correlation used.

Keywords

Scatter plot matrix, pairs plot, correlation, coefficient, panel function, graphics

Download

You can download the function code using this link (click to view the text, right-click to select download): <Pairs-panel-cor.R>. The file is an R source code file, which is readable by any text editor.

To get the function working for your copy of R you’ll need to use the source() function. Put the Pairs-panel-cor.R.R file in your working directory and type:

source("Pairs-panel-cor.R")

Alternatively, you can use:

source(file.choose())

 

This will open a file browser so you can find and select the file. If you are using RStudio you can use menu: Code > Source File...

Description

A scatter plot matrix is a graphic that displays scatter plots of all the pairwise combinations of variables. Panel functions can be used to draw into the upper or lower “triangles”. This panel function allows you to display text, giving some information about the pairwise correlation for each pair of variables. You can alter the method of correlation and the size of the displayed text.

Usage

panel.cor(x, y, cex.cor = 0.8, method = "pearson", ...)

Arguments

There are two main arguments you can alter:

x, y The variables, these are taken from pairs() and cannot be altered.
cex.cor The expansion factor for the text size, default = 0.8.
method The method for the correlation: see cor(). The options are: "pearson" (default), "spearman" and "Kendall".
... Other graphical parameters can be passed to pairs().

Value

The result of the panel function is only applied when used as an argument to pairs(). The appropriate panels are drawn with text (centered) displaying the correlation coefficient, the p-value and the number of observations.

Notes

The panel function will generate warning messages, because you are trying to pass parameters to pairs() that are not “regular” graphical arguments. These warnings can be safely ignored.

See Also

pairs() for plotting of scatter plot matrices, panel.smooth() as an example of a built-in panel function. See cor() for details about correlation.

Code

Here is the code for the function:

# Panel function for pairs()
# Mark Gardener 2020
# www.dataanalytics.org.uk

panel.cor <- function(x, y, cex.cor = 0.8, method = "pearson", ...) {
options(warn = -1)                   # Turn of warnings (e.g. tied ranks)
usr <- par("usr"); on.exit(par(usr)) # Saves current "usr" and resets on exit
par(usr = c(0, 1, 0, 1))             # Set plot size to 1 x 1
r <- cor(x, y, method = method, use = "pair")               # correlation coef
p <- cor.test(x, y, method = method)$p.val                  # p-value
n <- sum(complete.cases(x, y))                              # How many data pairs
txt <- format(r, digits = 3)                                # Format r-value
txt1 <- format(p, digits = 3)                                 # Format p-value
txt2 <- paste0("r= ", txt, '\n', "p= ", txt1, '\n', 'n= ', n) # Make panel text
text(0.5, 0.5, txt2, cex = cex.cor, ...)                      # Place panel text
options(warn = 0)                                             # Reset warning
}
## END

Examples

Here are some examples of the code in operation:

# Default method ("pearson")
pairs(trees, lower.panel = panel.cor, cex.cor = 2)
Scatterplot matrix

Pearson correlation statistics added to a pairs plot.

# Spearman correlation with separate panel for upper
# and additional graphical parameters
pairs(airquality[, 1:4], upper.panel = panel.smooth, lower.panel = panel.cor, method = "spearman", cex.cor = 1, col = "blue", pch = 21, bg = "gray80")
Scatter plot matrix

Spearman Rho statistics added to a pairs plot with additional graphical parameters.

Links

Data examples:

Custom R functions:

General data science articles:

  • DataAnalytics Knowledge Base. For general topics and articles about data science, including Learning R: the statistical programming language
  • DataAnalytics Tips and Tricks. for articles covering a range of topics in data science, including Using R, Using Excel, quantitative data analysis, predictive data analysis and a lot more besides.

See our Publications Page for an overview of our book on Ecology, Environmental Science and R: the statistical programming language.