R/draw_manhattan.R

Defines functions draw_manhattan

# 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")
}
mcanouil/mctools documentation built on Oct. 12, 2020, 10:21 p.m.