| VolcanoPlot | R Documentation |
Produces a volcano plot — a scatter plot that displays statistical
significance (typically -log10 adjusted p-value) on the y-axis versus
magnitude of change (log2 fold change) on the x-axis. Points are coloured
automatically by significance category ("sig_pos_x",
"sig_neg_x", "insig") or by a user-supplied column. The
most significant features can be labelled automatically via
geom_text_repel(), and specific points can be
highlighted.
The function supports automatic labelling of top features (by
distance to origin), mirrored layout via
flip_negatives, x-axis trimming to reduce the influence
of extreme values, faceting, and splitting into
separate sub-plots via split_by with per-split colour palette and
legend control.
VolcanoPlot(
data,
x,
y,
ytrans = "-log10",
color_by = NULL,
color_name = NULL,
xlim = NULL,
flip_negatives = FALSE,
x_cutoff = NULL,
y_cutoff = 0.05,
split_by = NULL,
split_by_sep = "_",
label_by = NULL,
x_cutoff_name = NULL,
y_cutoff_name = NULL,
x_cutoff_color = "red2",
y_cutoff_color = "blue2",
x_cutoff_linetype = "dashed",
y_cutoff_linetype = "dashed",
x_cutoff_linewidth = 0.5,
y_cutoff_linewidth = 0.5,
pt_size = 2,
pt_alpha = 0.5,
pt_shape = 21,
pt_border_color = TRUE,
pt_border_size = 0.5,
nlabel = 5,
labels = NULL,
label_size = 3,
label_fg = "black",
label_bg = "white",
label_bg_r = 0.1,
highlight = NULL,
highlight_color = "red",
highlight_size = 2,
highlight_alpha = 1,
highlight_stroke = 0.5,
raster = NULL,
raster_dpi = c(512, 512),
trim = c(0, 1),
facet_by = NULL,
facet_scales = "fixed",
facet_ncol = NULL,
facet_nrow = NULL,
facet_byrow = TRUE,
theme = "theme_this",
theme_args = list(),
palette = "Spectral",
palcolor = NULL,
palreverse = FALSE,
title = NULL,
subtitle = NULL,
xlab = NULL,
ylab = NULL,
aspect.ratio = 1,
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. |
x |
A character string specifying the column name of the data frame to plot for the x-axis. |
y |
A character string specifying the column name of the data frame to plot for the y-axis. |
ytrans |
A function or a function name (as a string) to transform
the y-axis values before plotting. The transformed values are used for
both the y-axis and cutoff comparisons. Default: |
color_by |
A character string specifying the column name to colour
the points by. When |
color_name |
A character string for the colour legend title when
|
xlim |
A numeric vector of length 2 to set the x-axis limits.
Passed to |
flip_negatives |
A logical value. When |
x_cutoff |
A numeric value specifying the x-axis significance
cutoff. Both the negative and positive of this value are used as
vertical threshold lines. When |
y_cutoff |
A numeric value specifying the y-axis significance
cutoff in the original (untransformed) scale. The value is
transformed by |
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
|
label_by |
A character string specifying the column whose values
are used as label text. When |
x_cutoff_name |
A character string for the x-cutoff legend entry.
When |
y_cutoff_name |
A character string for the y-cutoff legend entry.
When |
x_cutoff_color |
A character string specifying the colour of the
x-axis cutoff line(s). Default: |
y_cutoff_color |
A character string specifying the colour of the
y-axis cutoff line(s). Default: |
x_cutoff_linetype |
A character string specifying the linetype of
the x-axis cutoff line(s). Default: |
y_cutoff_linetype |
A character string specifying the linetype of
the y-axis cutoff line(s). Default: |
x_cutoff_linewidth |
A numeric value specifying the linewidth of
the x-axis cutoff line(s). Default: |
y_cutoff_linewidth |
A numeric value specifying the linewidth of
the y-axis cutoff line(s). Default: |
pt_size |
A numeric value specifying the point size for all data
points. Default: |
pt_alpha |
A numeric value in |
pt_shape |
A numeric value specifying the point shape. Default: |
pt_border_color |
Controls the point border colour. For shapes 21–25:
|
pt_border_size |
A numeric value specifying the point border size
(stroke width, in mm). |
nlabel |
An integer specifying the number of top features to label
automatically. Points are ranked by Euclidean distance to the origin
within each |
labels |
A character vector of row names or integer indices
specifying which points to label. Overrides automatic |
label_size |
A numeric value specifying the font size of the
labels. Default: |
label_fg |
A character string specifying the text colour of the
labels. Default: |
label_bg |
A character string specifying the background colour of
the label boxes (passed to |
label_bg_r |
A numeric value specifying the corner radius of the
label background boxes (passed to |
highlight |
A character vector of row names or integer indices
specifying which points to highlight with an overlaid point layer in
|
highlight_color |
A character string specifying the colour of the
highlight points. Default: |
highlight_size |
A numeric value specifying the point size of the
highlight layer. Default: |
highlight_alpha |
A numeric value in |
highlight_stroke |
A numeric value specifying the stroke width of
the highlight point borders. Default: |
raster |
A logical value. If |
raster_dpi |
A numeric vector of length 2 |
trim |
A numeric vector of length 2 specifying quantile bounds for
winsorizing the x-axis values. Values below the first quantile are
clamped to that quantile; values above the second quantile are clamped
to that quantile. Both values must be in |
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. |
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. |
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. |
seed |
The random seed to use. Default is 8525. |
combine |
Logical; when |
ncol, nrow |
Integer number of columns / rows for the combined layout
(passed to |
byrow |
Logical; fill the combined layout by row. Default
|
axes |
A character string specifying how axes should be treated
across the combined layout (passed to
|
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. Default |
design |
A custom layout design for the combined plot (passed to
|
... |
Additional arguments. |
A ggplot object, a patchwork object, or a named
list of ggplot objects (when combine = FALSE), each with
height and width attributes in inches.
When split_by is provided:
The split_by column(s) are validated via
check_columns() with force_factor = TRUE and
concat_multi = TRUE (multiple columns are concatenated
with split_by_sep).
The data frame is split by split_by (preserving factor
level order). If split_by is NULL, the data is
wrapped in a single-element list with name "...".
Per-split palette, palcolor,
legend.position, and legend.direction are resolved
via check_palette(), check_palcolor(),
and check_legend().
VolcanoPlotAtomic() is called for each split. If
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)
n <- 200
n_de <- 150
## Non-DE genes
fc_null <- rnorm(n - n_de, 0, 0.35)
z_null <- rnorm(n - n_de, 0, 1)
## DE genes
fc_de <- rnorm(
n_de,
mean = sample(c(-1, 1), n_de, replace = TRUE),
sd = 0.45
)
## Make significance related to effect size,
## but with substantial variation
z_de <- fc_de * rnorm(n_de, 4.5, 0.8)
avg_log2FC <- c(fc_null, fc_de)
z <- c(z_null, z_de)
p_val <- 2 * pnorm(-abs(z))
p_val_adj <- p.adjust(p_val, method = "BH")
## Shuffle genes
i <- sample(n)
data <- data.frame(
avg_log2FC = avg_log2FC[i],
p_val_adj = p_val_adj[i],
gene = paste0("gene", seq_len(n))[i],
pct_diff = rnorm(n, 0, 1),
group = sample(LETTERS[1:2], n, replace = TRUE)
)
# --- Basic usage ---
VolcanoPlot(data, x = "avg_log2FC", y = "p_val_adj", color_by = "pct_diff",
y_cutoff_name = "-log10(0.05)")
# --- With gene labels ---
VolcanoPlot(data, x = "avg_log2FC", y = "p_val_adj", color_by = "pct_diff",
y_cutoff_name = "-log10(0.05)", label_by = "gene")
# --- Mirrored layout ---
VolcanoPlot(data, x = "avg_log2FC", y = "p_val_adj", y_cutoff_name = "none",
flip_negatives = TRUE, label_by = "gene")
# --- With faceting ---
VolcanoPlot(data, x = "avg_log2FC", y = "p_val_adj", y_cutoff_name = "none",
flip_negatives = TRUE, facet_by = "group", label_by = "gene")
# --- With splitting ---
VolcanoPlot(data, x = "avg_log2FC", y = "p_val_adj", y_cutoff_name = "none",
flip_negatives = TRUE, split_by = "group", label_by = "gene")
# --- With highlighting ---
VolcanoPlot(data, x = "avg_log2FC", y = "p_val_adj", y_cutoff_name = "none",
highlight = c("gene196", "gene151"), label_by = "gene")
# --- Per-split palettes ---
VolcanoPlot(data, x = "avg_log2FC", y = "p_val_adj", color_by = "pct_diff",
y_cutoff_name = "-log10(0.05)", split_by = "group", label_by = "gene",
palette = c(A = "Spectral", B = "PuOr"))
# Trim extreme x-values (winsorize to 40% and 50% quantiles), for demo purposes
VolcanoPlot(data, x = "avg_log2FC", y = "p_val_adj", color_by = "pct_diff",
y_cutoff_name = "-log10(0.05)", trim = c(0.4, 0.5))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.