Skip to content

Instantly share code, notes, and snippets.

@mcanouil
Last active August 9, 2022 11:48
Show Gist options
  • Save mcanouil/44bb2eaa551ee7c4c39e9fbb74cf0f6c to your computer and use it in GitHub Desktop.
Save mcanouil/44bb2eaa551ee7c4c39e9fbb74cf0f6c to your computer and use it in GitHub Desktop.
Manhattan Plot
# # MIT License
#
# Copyright (c) Mickaël Canouil
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# library(data.table)
# library(ggplot2)
# library(ggtext)
# library(scales)
# devtools::source_gist("d7cbc59a7fcbb6d231f432801ec7aa19") # pval_trans
# devtools::source_gist("c519e2ade2614f5a375906c3485c4eea") # stat_manhattan
draw_manhattan <- function(data, x, y, chr, label_y = "P-value", alpha = 0.05) {
data <- data.table::as.data.table(data)#[, .SD, .SDcols = c(x, y, chr)]
data.table::setnames(data, c(x, y, chr), c("pos", "pvalue", "chr"), skip_absent = TRUE)
if (is.numeric(data[["chr"]])) data[, "chr" := lapply(.SD, as.character), .SDcols = "chr"]
ggplot2::ggplot(data = data) +
ggplot2::aes(x = .data[["pos"]], y = .data[["pvalue"]], colour = .data[["chr"]]) +
ggplot2::geom_point(stat = "manhattan", size = 0.60, na.rm = TRUE) +
ggplot2::annotate(
geom = "rect",
xmin = -Inf, xmax = Inf, ymin = 1, ymax = alpha,
fill = "firebrick", alpha = 0.2, colour = NA
) +
ggplot2::geom_hline(yintercept = alpha, linetype = 2, colour = "firebrick") +
ggplot2::scale_x_continuous(
breaks = 1:24,
labels = c(1:22, "X", "Y"),
expand = ggplot2::expansion(add = 0.25)
) +
ggplot2::scale_y_continuous(
trans = pval_trans(alpha = alpha, md = TRUE, colour = "firebrick"),
expand = ggplot2::expansion(mult = c(0, 0.2)),
limits = c(1, NA)
) +
ggplot2::scale_colour_manual(values = rep(scales::viridis_pal(begin = 1/4, end = 3/4)(2), 12)) +
ggplot2::labs(colour = "Chromosome", x = "Chromosome", y = label_y) +
ggplot2::theme(panel.grid.minor.x = ggplot2::element_blank(), legend.position = "none")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment