Skip to content

Instantly share code, notes, and snippets.

@andreagrioni
Last active November 25, 2021 17:59
Show Gist options
  • Save andreagrioni/60147546d9ef5d8a7af73e971bc73c90 to your computer and use it in GitHub Desktop.
Save andreagrioni/60147546d9ef5d8a7af73e971bc73c90 to your computer and use it in GitHub Desktop.
#' generate volcano plots with EnanchedVolcano
#' @table a tibble in the format limma::topTable
#' @coef the coeffincient to be filtered
#' @img_dir save image to directory
#' @fdr_threshold the max FDR allowed to color targets
#' @save bool (def. TRUE)
make_volcano <- function(table, coef, img_dir, fdr_threshold=0.1, save=TRUE) {
# filter for coefficient
table %>%
dplyr::filter(coeff == coef) -> tmp
# set title
title = glue::glue("{coef} | candidate biomarkers")
# set filename
img_filename = glue::glue("{coef}.volcano.png")
# set imgpath
imgpath = fs::path(img_dir, img_filename)
# set lables to show
show_lab = tmp %>%
# filter by FRD
dplyr::filter(adj.P.Val <= fdr_threshold) %>%
# pull labels
dplyr::pull("EntrezGeneSymbol")
# sett total names
names_lab = tmp %>%
dplyr::pull(EntrezGeneSymbol)
# do volcano with EnhancedVolcano
EnhancedVolcano::EnhancedVolcano(
toptable = tmp,
title=title,
subtitle="",
lab = names_lab,
x = 'logFC',
y = 'P.Value',
selectLab = show_lab,
pCutoffCol = "adj.P.Val",
pCutoff = fdr_threshold,
FCcutoff = 0,
legendLabels = c("NS", "NS", "NS", "sign"),
col = c("grey", "grey", "grey", "red")
) -> figure_
if (save) {
# set filename
img_filename = glue::glue("{coef}.volcano.png")
# set imgpath
imgpath = fs::path(img_dir, img_filename)
ggplot2::ggsave(imgpath, plot=figure_)
}
return(figure_)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment