--- title: "Volcano plots" output: rmarkdown::html_vignette: toc: TRUE vignette: > %\VignetteIndexEntry{Volcano plots} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ```{r, include = FALSE} knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.width = 4, fig.height = 4, fig.align = "center", out.width = '60%', dpi = 120, message = FALSE ) library(ckbplotr) set.seed(941) results <- data.frame( name = sapply(1:500, \(x) paste(sample(LETTERS, 3), collapse = "")), est = c(rnorm(499, 0, 0.2), 0.6), se = c(sqrt(runif(499, 0.01, 0.05)), 0.001) ) results$p <- 2 * pnorm(abs(results$est / results$se), lower.tail = FALSE) results <- dplyr::arrange(results, desc(p)) ``` ## Introduction The `volcano_plot()` function creates a plot of estimates and (-log10) p-values using the [ggplot2](https://ggplot2.tidyverse.org/) 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). ```{r} head(results) 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. ```{r} 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`. ```{r} 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](customising_plots.html) for details. ```{r, fig.width = 8, out.width = '95%'} 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. ```{r} 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. ```{r} 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). ```{r} 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](https://ggrepel.slowkow.com/) provides geoms to avoid overlapping text labels. ```{r} 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)) ```