| RidgePlot | R Documentation |
Ridge (joy) plot for visualising the distribution of a numeric variable across multiple groups. Each group is rendered as a partially overlapping density curve along the y-axis, making it easy to compare distribution shapes, central tendency, and spread across categories.
The function supports both long and wide data formats:
Long form (in_form = "long", default) — a numeric column (x) plus a
factor column (group_by) whose levels become the y-axis ridges.
Wide form (in_form = "wide") — multiple numeric columns listed in
group_by are gathered internally into long form.
Optional vertical reference lines (add_vline) can mark group means,
specific values, or per-group thresholds. Supports faceting, split-by
splitting, and full palette customisation.
RidgePlot(
data,
x = NULL,
in_form = c("long", "wide"),
split_by = NULL,
split_by_sep = "_",
group_by = NULL,
group_by_sep = "_",
group_name = NULL,
scale = NULL,
keep_na = FALSE,
keep_empty = FALSE,
add_vline = NULL,
vline_type = "solid",
vline_color = TRUE,
vline_width = 0.5,
vline_alpha = 1,
flip = FALSE,
alpha = 0.8,
theme = "theme_this",
theme_args = list(),
palette = "Paired",
palcolor = NULL,
palreverse = FALSE,
title = NULL,
subtitle = NULL,
xlab = NULL,
ylab = NULL,
x_text_angle = 90,
x_min = NULL,
x_max = NULL,
reverse = FALSE,
facet_by = NULL,
facet_scales = "fixed",
facet_ncol = NULL,
facet_nrow = NULL,
facet_byrow = TRUE,
aspect.ratio = 1,
legend.position = "none",
legend.direction = "vertical",
combine = TRUE,
nrow = NULL,
ncol = NULL,
byrow = TRUE,
seed = 8525,
axes = NULL,
axis_titles = axes,
guides = NULL,
design = NULL,
...
)
data |
A data frame. |
x |
A character string specifying the column name of the data frame to plot for the x-axis. |
in_form |
A character string specifying whether |
split_by |
The column(s) to split data by and plot separately. |
split_by_sep |
The separator for multiple split_by columns. See |
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 legend title for the
|
scale |
A numeric value controlling the vertical overlap of ridges.
Passed to |
keep_na |
A logical value or a character to replace the NA values in the data.
It can also take a named list to specify different behavior for different columns.
If TRUE or NA, NA values will be replaced with NA.
If FALSE, NA values will be removed from the data before plotting.
If a character string is provided, NA values will be replaced with the provided string.
If a named vector/list is provided, the names should be the column names to apply the behavior to,
and the values should be one of TRUE, FALSE, or a character string.
Without a named vector/list, the behavior applies to categorical/character columns used on the plot,
for example, the |
keep_empty |
One of FALSE, TRUE and "level". It can also take a named list to specify
different behavior for different columns. Without a named list, the behavior applies to the
categorical/character columns used on the plot, for example, the
|
add_vline |
A specification for vertical reference lines:
|
vline_type |
A character string specifying the line type for the
vertical reference lines. Passed as |
vline_color |
The colour of the vertical reference lines:
|
vline_width |
A numeric value for the thickness of the vertical
reference lines. Passed as |
vline_alpha |
A numeric value in |
flip |
A logical value. If |
alpha |
A numeric value specifying the transparency of the plot. |
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. |
xlab |
A character string specifying the x-axis label. |
ylab |
A character string specifying the y-axis label. |
x_text_angle |
A numeric value specifying the angle of the x-axis text. |
x_min, x_max |
Numeric limits for the x-axis. When |
reverse |
A logical value. If |
facet_by |
A character string specifying the column name of the data frame to facet the plot.
Otherwise, the data will be split by |
facet_scales |
Whether to scale the axes of facets. Default is "fixed"
Other options are "free", "free_x", "free_y". See |
facet_ncol |
A numeric value specifying the number of columns in the facet. When facet_by is a single column and facet_wrap is used. |
facet_nrow |
A numeric value specifying the number of rows in the facet. When facet_by is a single column and facet_wrap is used. |
facet_byrow |
A logical value indicating whether to fill the plots by row. Default is TRUE. |
aspect.ratio |
A numeric value specifying the aspect ratio of the plot. |
legend.position |
A character string specifying the position of the legend.
if |
legend.direction |
A character string specifying the direction of the legend. |
combine |
Whether to combine the plots into one when facet is FALSE. Default is TRUE. |
nrow |
A numeric value specifying the number of rows in the facet. |
ncol |
A numeric value specifying the number of columns in the facet. |
byrow |
A logical value indicating whether to fill the plots by row. |
seed |
The random seed to use. Default is 8525. |
axes |
A string specifying how axes should be treated. Passed to
|
axis_titles |
A string specifying how axis titltes should be treated. Passed to
|
guides |
A string specifying how guides should be treated in the layout. Passed to
|
design |
Specification of the location of areas in the layout, passed to |
... |
Additional arguments. |
A ggplot object (single plot), a patchwork / wrap_plots object
(when split_by is provided and combine = TRUE), or a list of ggplot
objects (when split_by is provided and combine = FALSE).
When split_by is specified, RidgePlot() executes the following pipeline:
Argument validation — validate_common_args() checks the seed and
facet-by consistency.
NA / empty normalisation — check_keep_na() / check_keep_empty()
convert keep_na / keep_empty to per-column lists.
Theme resolution — process_theme() resolves the theme string to a
theme function.
Split column resolution — check_columns() validates split_by
(force_factor, concat_multi).
Pre-filtering — process_keep_na_empty() removes NA / empty levels
from the split column, then data is split by split_by levels (order
preserved).
Per-split parameter resolution — check_palette(),
check_palcolor(), check_legend() resolve palette, palcolor,
legend.position, and legend.direction for each split.
Per-split dispatch — each split is passed to RidgePlotAtomic() with
its resolved parameters. Title defaults to the split level name unless
title is a function (in which case it is called with the default).
Combination — combine_plots() assembles the list of plots via
patchwork::wrap_plots(), applying nrow, ncol, byrow, axes,
axis_titles, guides, and design.
set.seed(8525)
data <- data.frame(
x = c(rnorm(250, -1), rnorm(250, 1)),
group = factor(rep(c("A", NA, LETTERS[3:5]), each = 100), levels = LETTERS[1:6])
)
# basic usage
RidgePlot(data, x = "x") # single ridge (no group_by)
RidgePlot(data, x = "x", add_vline = 0, vline_color = "black")
# grouped ridges
RidgePlot(data, x = "x", group_by = "group")
RidgePlot(data, x = "x", group_by = "group",
keep_na = TRUE, keep_empty = TRUE)
RidgePlot(data, x = "x", group_by = "group", reverse = TRUE)
RidgePlot(data, x = "x", group_by = "group",
add_vline = TRUE, vline_color = TRUE, alpha = 0.7)
# faceting
RidgePlot(data, x = "x", facet_by = "group",
keep_na = TRUE, keep_empty = TRUE)
# wide form
data_wide <- data.frame(
A = rnorm(100),
B = rnorm(100),
C = rnorm(100),
D = rnorm(100),
E = rnorm(100),
group = sample(letters[1:4], 100, replace = TRUE)
)
RidgePlot(data_wide, group_by = LETTERS[1:5], in_form = "wide")
RidgePlot(data_wide, group_by = LETTERS[1:5], in_form = "wide", facet_by = "group")
# split_by with per-split palettes
RidgePlot(data_wide, group_by = LETTERS[1:5], in_form = "wide", split_by = "group",
palette = list(a = "Reds", b = "Blues", c = "Greens", d = "Purples"))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.