Skip to content

Instantly share code, notes, and snippets.

@Zepeng-Mu
Created June 1, 2025 18:22
Show Gist options
  • Save Zepeng-Mu/e2152f984ae95db3d683cc3b8648a114 to your computer and use it in GitHub Desktop.
Save Zepeng-Mu/e2152f984ae95db3d683cc3b8648a114 to your computer and use it in GitHub Desktop.
Create a single Manhattan plot using the tidyplots package
tpManhattanSingle <- function(
summ.1, chr = "CHR", bp = "BP", p = "P", snp = "SNP",
summ.1.name = "$-log[10](P)$", plot.label = NULL,
logp = TRUE, size = 2, lead.snp = NULL, r2 = NULL,
plot.width = 100, plot.height = 50, ...
) {
if (!is.null(summ.1[[snp]])) {
d1 <- data.frame(CHR = summ.1[[chr]], BP = summ.1[[bp]], P = summ.1[[p]], SNP = summ.1[[snp]])
} else {
d1 <- data.frame(CHR = summ.1[[chr]], BP = summ.1[[bp]], P = summ.1[[p]])
}
# Calculate -log(P) for summ.1 and summ.2
if (logp) {
d1$logp <- -log10(d1$P)
} else {
d1$logp <- d1$P
}
# Map SNP r2 to discrete colors
# This is inspired by locuscomparer package
if (is.null(r2)) {
g1 <- d1 %>%
mutate(x = BP / 1e6) %>%
tidyplot(x = x, y = logp) %>%
adjust_size(width = plot.width, height = plot.height) %>%
adjust_font(fontsize = 10) %>%
add_data_points(color = "steelblue3", size = 0.5, rasterize = T, rasterize_dpi = 960)
} else {
d1$r2 <- r2
g1 <- d1 %>%
mutate(x = BP / 1e6) %>%
tidyplot(x = x, y = logp, col = r2) %>%
adjust_size(width = plot.width, height = plot.height) %>%
adjust_font(fontsize = 10) %>%
add_data_points(size = 0.5, rasterize = T, rasterize_dpi = 960) %>%
adjust_colors(new_colors = colors_continuous_turbo, limits = c(0, 1))
}
g1 <- g1 %>%
adjust_title(title = plot.label) %>%
add_annotation_line(x = lead.snp / 1e6, xend = lead.snp / 1e6, y = 0, yend = max(d1$logp),
color = "grey50", lwd = 0.4, lty = 2) %>%
add_data_points(data = filter_rows(BP == lead.snp), shape = 18, size = 3, color = "purple2") %>%
adjust_y_axis(title = summ.1.name, limits = c(0, max(d1$logp) * 1.05)) %>%
adjust_x_axis(title = "", padding = c(0, 0))
return(g1)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment