Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

generating a manhattan plot with ggplot

Tags:

r

ggplot2

I've been trying to generate a Manhattan plot using ggplot, which I finally got to work. However, I cannot get the points to be colored by chromosome, despite having tried several different examples I've seen online. I'm attaching my code and the resulting plot below. Can anyone see why the code is failing to color points by chromosome?

library(tidyverse)
library(vroom)

# threshold to drop really small -log10 p values so I don't have to plot millions of uninformative points. Just setting to 0 since I'm running for a small subset
min_p <- 0.0

# reading in data to brassica_df2, converting to data frame, removing characters from AvsDD p value column, converting to numeric, filtering by AvsDD (p value)
brassica_df2 <- vroom("manhattan_practice_data.txt", col_names = c("chromosome", "position", "num_SNPs", "prop_SNPs_coverage", "min_coverage", "AvsDD", "AvsWD", "DDvsWD"))
brassica_df2 <- as.data.frame(brassica_df2)
brassica_df2$AvsDD <- gsub("1:2=","",as.character(brassica_df2$AvsDD))
brassica_df2$AvsDD <- as.numeric(brassica_df2$AvsDD)
brassica_df2 <- filter(brassica_df2, AvsDD > min_p)

# setting significance threshhold
sig_cut <- -log10(1)

# settin ylim for graph
ylim <- (max(brassica_df2$AvsDD) + 2)

# setting up labels for x axis
axisdf <- as.data.frame(brassica_df2 %>% group_by(chromosome) %>% summarize(center=( max(position) + min(position) ) / 2 ))

# making manhattan plot of statistically significant SNP shifts 
manhplot <- ggplot(data = filter(brassica_df2, AvsDD > sig_cut), aes(x=position, y=AvsDD), color=as.factor(chromosome)) +
  geom_point(alpha = 0.8) +
  scale_x_continuous(label = axisdf$chromosome, breaks= axisdf$center) +
  scale_color_manual(values = rep(c("#276FBF", "#183059"), unique(length(axisdf$chromosome)))) +
  geom_hline(yintercept = sig_cut, lty = 2) +
  ylab("-log10 p value") +
  ylim(c(0,ylim)) +
  theme_classic() +
  theme(legend.position = "n")
print(manhplot)

enter image description here

like image 203
steve Avatar asked Aug 11 '26 14:08

steve


1 Answers

I think you just need to move your color=... argument inside the call to aes():

ggplot(
    data = filter(brassica_df2, AvsDD > sig_cut),
    aes(x=position, y=AvsDD),
    color=as.factor(chromosome))

becomes...

ggplot(
    data = filter(brassica_df2, AvsDD > sig_cut),
    aes(x=position, y=AvsDD, color=as.factor(chromosome)))
like image 153
Zachary Cross Avatar answered Aug 14 '26 10:08

Zachary Cross



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!