Introduction
The volcano_plot() function creates a plot of estimates
and (-log10) p-values using the ggplot2 graphics package. The
function returns both a plot and the ggplot2 code used to create the
plot. In RStudio, the code used to create the plot will be shown in the
Viewer pane.
Basic usage
Supply a data frame of estimates and p-values to the
volcano_plot() function. Use col.estimate and
col.p to specify the names of columns containing estimates
and p-values (some common names are found automatically).
head(results)
#> name est se p
#> 1 CPK -0.001193170 0.1947208 0.9951109
#> 2 WXS -0.001141980 0.1223696 0.9925541
#> 3 CDJ 0.001972373 0.1369434 0.9885086
#> 4 UPN 0.003614462 0.2077709 0.9861204
#> 5 MCS -0.004488801 0.2126560 0.9831593
#> 6 SYX -0.004415751 0.1979863 0.9822060
volcano_plot(results)
Points with a p-value of “zero” are shown as triangles at the top limit of the plot.
If your estimates are on the log scale (e.g. log hazard ratios), then
set exponentiate to TRUE. This will plot
exp(estimates) and use a log scale for the axis.
volcano_plot(results,
exponentiate = TRUE)
Arguments
Some arguments are available for customising the appearance of the
plot. Use xlims and ylims to set axis scale
limits, xbreaks and ybreaks to set axis break
points, and xlab and ylab to set axis titles.
Colours for points can be set using a named vector for
point_colours. The p-value threshold (for statistical
significance) to colour points and show a line segment can be set using
p_threshold.
volcano_plot(results,
exponentiate = TRUE,
xlims = c(0.5, 2 + 1e-12),
ylims = c(0, 7),
xbreaks = c(0.5, 1, 2),
ybreaks = c(0:7),
p_threshold = 0.05 / length(results$p),
point_colours = c(pos = "green4",
neg = "purple4",
null = "grey50"),
xlab = "Hazard Ratio",
title = "Volcano plot")
Customisation
Use the addaes, addarg and add
arguments to further customise the plot. See Customising plots for details.
volcano_plot(results,
exponentiate = TRUE,
xlims = c(0.5, 2 + 1e-12),
ylims = c(0, 7),
xbreaks = c(0.5, 1, 2),
ybreaks = 0:7,
xlab = "Hazard Ratio",
title = "Volcano plot",
addarg = list(
points = "size = 1.5",
scale.colour = c("name = NULL",
"labels = c(pos = 'Significant positive', neg = 'Significant inverse')"),
line = c("colour = 'grey70'",
"linetype = 'dotted'"),
theme = c("legend.position = 'right'",
"legend.direction = 'vertical'",
"plot.title = element_text(size = 14, margin = margin(0, 0, 2, 0, 'lines'))")
))
Labelling points
Using the point_labels argument
The point_labels argument can be used to add labels for
selected points (by adding a geom_text_move() layer). The
argument must be data frame with columns:
-
id: The id of the point to be labelled. This is “pos”/“neg” depending on if the sign of the estimate, followed by the p-value rank. -
position: The clock position of the label compared to the point. -
r: The distance of the label from the point. -
line: Logical value to indicate if a line should connect the point to the label.
Use the col.label argument to set the column to use for
the text labels.
point_labels <- tibble::tribble(
~id, ~position, ~r, ~line,
"pos1", 3, 0.5, FALSE,
"pos2", 3, 0.5, FALSE,
"pos3", 1.5, 1, TRUE,
"neg1", 12, 0.5, FALSE,
"neg2", 6, 0.5, FALSE,
"neg3", 4.5, 1, TRUE
)
volcano_plot(results,
exponentiate = TRUE,
col.label = "name",
point_labels = point_labels,
xlims = c(0.5, 2 + 1e-12),
ylims = c(0, 7),
xbreaks = c(0.5, 1, 2),
ybreaks = 0:7,
xlab = "Hazard Ratio",
addarg = list(
points = "size = 1.5",
line = c("colour = 'grey70'",
"linetype = 'dotted'")))
Using geom_text_move
The add argument can also be used to add a custom
geom_text_move() layer to the plot.
label_positions <- tibble::tribble(
~name, ~position, ~distance, ~line,
"NTP", 6, 0.5, FALSE,
"VHO", 12, 0.5, FALSE
)
## create a geom_text_move layer to add the text labels
volcano_labels <- geom_text_move(
aes(label = name,
offset_clock = position,
offset_r = distance,
line = line),
data = \(x) dplyr::inner_join(x, label_positions, by = "name")
)
## use the add argument to add the labels layer
volcano_plot(results,
exponentiate = TRUE,
xlims = c(0.5, 2 + 1e-12),
ylims = c(0, 7),
xbreaks = c(0.5, 1, 2),
ybreaks = 0:7,
xlab = "Hazard Ratio",
addarg = list(
points = "size = 1.5",
line = c("colour = 'grey70'",
"linetype = 'dotted'")),
add = list(start = volcano_labels))
Using geom_text
ggplot2::geom_text() and
ggplot2::geom_label() can be used to add text labels,
though this requires careful positioning of the text using the data
coordinates (e.g. using nudge_x to move labels to the right
of points).
volcano_plot(results,
exponentiate = TRUE,
xlims = c(0.5, 2 + 1e-12),
ylims = c(0, 7),
xbreaks = c(0.5, 1, 2),
ybreaks = 0:7,
xlab = "Hazard Ratio",
addarg = list(
points = "size = 1.5",
line = c("colour = 'grey70'",
"linetype = 'dotted'")),
add = list(end = geom_text(aes(label = name),
hjust = 0,
nudge_x = 0.03,
data = \(x) dplyr::slice_min(x, n = 3, order_by = p)))
)
Using ggrepel
The ggrepel package provides geoms to avoid overlapping text labels.
volcano_labels <- ggrepel::geom_text_repel(
aes(label = name),
min.segment.length = 0.1,
size = 3,
data = \(x) dplyr::mutate(x, name = dplyr::if_else(-log10(p) > 3.2, name, "")))
volcano_plot(results,
exponentiate = TRUE,
xlims = c(0.5, 2 + 1e-12),
ylims = c(0, 7),
xbreaks = c(0.5, 1, 2),
ybreaks = 0:7,
xlab = "Hazard Ratio",
addarg = list(points = "size = 1.5",
line = c("colour = 'grey70'",
"linetype = 'dotted'")),
add = list(end = volcano_labels))
