Zettelkasten

# https://danielroelfs.com/blog/how-i-create-manhattan-plots-using-ggplot/

library(norment)
library(tidyverse)
library(ggprism)

set.seed(2404)
gwas.dat <- simulateGWAS(nSNPs = 1e6, nSigCols = 3)

sig.dat <- gwas.dat %>% 
  subset(P < 0.05)
notsig.dat <- gwas.dat %>% 
  subset(P >= 0.05) %>%
  slice(sample(nrow(.), nrow(.) / 5))
gwas.dat <- rbind(sig.dat,notsig.dat)

nCHR <- length(unique(gwas.dat$CHR))
gwas.dat$BPcum <- NA
s <- 0
nbp <- c()
for (i in unique(gwas.dat$CHR)){
  nbp[i] <- max(gwas.dat[gwas.dat$CHR == i,]$BP)
  gwas.dat[gwas.dat$CHR == i,"BPcum"] <- gwas.dat[gwas.dat$CHR == i,"BP"] + s
  s <- s + nbp[i]
}

axis.set <- gwas.dat %>% 
  group_by(CHR) %>% 
  summarize(center = (max(BPcum) + min(BPcum)) / 2)
ylim <- abs(floor(log10(min(gwas.dat$P)))) + 2 
sig <- 5e-8

manhplot <- gwas.dat %>%
  ggplot(
	aes(
	  x = BPcum,
	  y = -log10(P), 
      color = as.factor(CHR),
      size = -log10(P)
    )
  ) +
  geom_point(alpha = 0.75) +
  geom_hline(
  	yintercept = -log10(sig),
  	color = "grey40",
  	linetype = "dashed"
  ) + 
  scale_x_continuous(
  	label = axis.set$CHR,
  	breaks = axis.set$center
  ) +
  scale_y_continuous(
  	expand = c(0,0),
  	limits = c(0, ylim)
  ) +
  scale_color_manual(values = rep(c("#276FBF", "#183059"), nCHR)) +
  scale_size_continuous(range = c(0.5,3)) +
  labs(
  	x = NULL, 
    y = "-log10(p)"
  ) + 
  theme_minimal() +
  ggprism::theme_prism() + 
  #ggprism::scale_colour_prism() + 
  ggprism::scale_shape_prism() +
  theme( 
    legend.position = "none",
    axis.text.x = element_text(
      angle = 90,
      size = 8,
      vjust = 0.5
    )
  )

print(manhplot)