| CorPairsPlot | R Documentation |
Draws a grid of pairwise scatter plots for selected numeric columns, arranged in a scatterplot matrix layout. The upper or lower triangle displays correlation tiles while the opposite triangle shows scatter plots with regression lines. Diagonal cells can show density plots, violin plots, histograms, box plots, or a simple diagonal line.
NOTE: The facet_by parameter is not supported
in CorPairsPlot (an error is raised if provided). Use
split_by instead to create separate correlation pair matrices
per group.
The function supports four layout orientations (layout),
three correlation methods, configurable diagonal plots via
other plotthis functions, custom correlation tile formatting, and
splitting into separate sub-plots via split_by.
CorPairsPlot(
data,
columns = NULL,
group_by = NULL,
group_by_sep = "_",
group_name = NULL,
split_by = NULL,
split_by_sep = "_",
diag_type = NULL,
diag_args = list(),
layout = c(".\\", "\\.", "/.", "./"),
cor_method = c("pearson", "spearman", "kendall"),
cor_palette = "RdBu",
cor_palcolor = NULL,
cor_size = 3,
cor_format = "corr: {round(corr, 2)}",
cor_fg = "black",
cor_bg = "white",
cor_bg_r = 0.1,
theme = "theme_this",
theme_args = list(),
palette = ifelse(is.null(group_by), "Spectral", "Paired"),
palcolor = NULL,
palreverse = FALSE,
title = NULL,
subtitle = NULL,
facet_by = NULL,
legend.position = "right",
legend.direction = "vertical",
seed = 8525,
combine = TRUE,
nrow = NULL,
ncol = NULL,
byrow = TRUE,
axes = NULL,
axis_titles = axes,
guides = NULL,
design = NULL,
...
)
data |
A data frame. |
columns |
A character vector of column names to include in the
pairs plot. When |
group_by |
Columns to group the data for plotting
For those plotting functions that do not support multiple groups,
They will be concatenated into one column, using |
group_by_sep |
The separator for multiple group_by columns. See |
group_name |
A character string used as the colour legend title
in the scatter plots. When |
split_by |
The column(s) to split the data by and produce separate
sub-plots. Multiple columns are concatenated with |
split_by_sep |
A character string to separate concatenated
|
diag_type |
A character string specifying the plot type for
diagonal cells. One of |
diag_args |
A named list of additional arguments passed to the
diagonal plot function ( |
layout |
A character string specifying the layout orientation.
One of the following codes (dot = scatter, backslash/slash =
diagonal): |
cor_method |
A character string specifying the correlation method
for the fill tiles. One of |
cor_palette |
A character string specifying the colour palette for
the correlation fill tiles. Default: |
cor_palcolor |
A character vector of custom colours used to create
the correlation tile palette. When |
cor_size |
A numeric value specifying the font size of the
correlation text in the fill tiles. Default: |
cor_format |
A character string specifying a glue template for
formatting the correlation text. The template is evaluated by
|
cor_fg |
A character string specifying the colour of the
correlation text. Default: |
cor_bg |
A character string specifying the background colour of
the correlation text boxes. Default: |
cor_bg_r |
A numeric value specifying the corner radius of the
correlation text background boxes. Default: |
theme |
A character string or a theme class (i.e. ggplot2::theme_classic) specifying the theme to use. Default is "theme_this". |
theme_args |
A list of arguments to pass to the theme function. |
palette |
A character string specifying the palette to use.
A named list or vector can be used to specify the palettes for different |
palcolor |
A character string specifying the color to use in the palette.
A named list can be used to specify the colors for different |
palreverse |
A logical value indicating whether to reverse the palette. Default is FALSE. |
title |
A character string specifying the title of the plot. A function can be used to generate the title based on the default title. This is useful when split_by is used and the title needs to be dynamic. |
subtitle |
A character string specifying the subtitle of the plot. |
facet_by |
A character string specifying the column name of the data frame to facet the plot.
Otherwise, the data will be split by |
legend.position |
A character string specifying the position of the legend.
if |
legend.direction |
A character string specifying the direction of the legend. |
seed |
A numeric seed for reproducibility. |
combine |
Logical; when |
ncol, nrow |
Integer number of columns / rows for the combined layout. |
byrow |
Logical; fill the combined layout by row. Default
|
axes |
A character string specifying how axes should be treated across the combined layout. |
axis_titles |
A character string specifying how axis titles should
be treated across the combined layout. Defaults to |
guides |
A character string specifying how guides (legends) should be collected across panels. |
design |
A custom layout design for the combined plot. |
... |
Additional arguments. |
A patchwork object (when combine = TRUE) or a
named list of patchwork objects (when combine = FALSE),
each with height and width attributes in inches.
When split_by is provided:
The split_by column is validated via
check_columns() with force_factor = TRUE.
Empty levels are dropped.
The data frame is split by split_by (preserving level
order). If split_by is NULL, the data is wrapped
in a single-element list with name "...". The
split_by column is removed from each split's data before
plotting.
Per-split palette, palcolor,
legend.position, and legend.direction are resolved
via check_palette(), check_palcolor(),
and check_legend().
CorPairsPlotAtomic() is called for each split.
When title is a function, it receives the split level
name and can generate dynamic titles.
Results are combined via combine_plots() (when
combine = TRUE) or returned as a named list.
set.seed(8525)
data <- data.frame(x = rnorm(100))
data$y <- rnorm(100, 10, sd = 0.5)
data$z <- -data$x + data$y + rnorm(100, 20, 1)
data$g <- sample(1:4, 100, replace = TRUE)
# Histogram diagonal, slash layout
CorPairsPlot(data, diag_type = "histogram",
diag_args = list(bins = 30, palette = "Paired"),
layout = "/.")
# No diagonal with axis title styling
CorPairsPlot(data, group_by = "g", diag_type = "none", layout = "./",
theme_args = list(axis.title = element_textbox(
color = "black", box.color = "grey20", size = 16, halign = 0.5,
fill = "grey90", linetype = 1,
width = grid::unit(1, "npc"),
padding = ggplot2::margin(5, 5, 5, 5))))
# Violin diagonal with custom format
CorPairsPlot(data, group_by = "g", diag_type = "violin", layout = "\\.",
cor_format = "{x}\n{y}\ncorr: {round(corr, 2)}")
# Per-split with bottom legend
CorPairsPlot(data, split_by = "g", diag_type = "none", layout = ".\\",
legend.position = "bottom", legend.direction = "horizontal",
group_name = "group")
# Per-split with custom palette colours
CorPairsPlot(data, split_by = "g",
palcolor = list("1" = "red", "2" = "blue", "3" = "green",
"4" = "yellow"))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.