User Tools

Site Tools


You are not allowed to perform this action
en:customized_functions:pairs2

pairs (with panel functions)

Author: Francois Gillet

Draws an advanced pairplot (see example below). Based on the base function pairs, with original code written by R-core members and panel functions added by Francois Gillet.

To define the function, source the panelutils.r file from the book Numerical Ecology with R:

source ('https://raw.githubusercontent.com/zdealveindy/anadat-r/master/scripts/NumEcolR2/panelutils.R')

Example of use (using environmental data from the dataset Vegetation of Carpathian wetlands, after removing the 15th variable which is slope and not water chemistry):

source ('https://raw.githubusercontent.com/zdealveindy/anadat-r/master/scripts/NumEcolR2/panelutils.R')
chem <- read.delim ('https://raw.githubusercontent.com/zdealveindy/anadat-r/master/data/chemistry.txt', row.names = 1) # reads the ''chem'' dataset
pairs(chem[,-15], lower.panel=panel.smooth, upper.panel=panel.cor, diag.panel=panel.hist)

Panels in the upper-right triangle list correlation coefficients and their significance (displayed by the number of stars), where red indicates positive relationships and blue indicates negative relationships; values in bold are significant at P < 0.05. Panels on the diagonal include the names of variables and histograms of value distribution. Panels on the lower-left triangle show scatterplots with a loess smoother curve.

Most of the variables need to be log-transformed (they have a heavily right-skewed distribution); after the log-transformation, the result looks like this:

chem_t <- chem
chem_t[, 1:12] <- log (chem[, 1:12])
pairs(chem_t[,-15], lower.panel=panel.smooth, upper.panel=panel.cor, diag.panel=panel.hist)

Note: Consider using function ggpairs from the package GGally (much fancier ggplot2 version of pairs function). There exists also an older package car that contains the function scatterplotMatrix, which behaves similarly to pairs (but is rather outdated).

en/customized_functions/pairs2.txt · Last modified: by David Zelený

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki