我使用ggplot2将GWAS汇总统计数据绘制为曼哈顿图。事情就像预期的一样,但是颜色没有正确地重复。无论我是重复2种颜色还是4种颜色,在早期都会有一次重复,导致两条染色体相邻成相同的颜色。即使我手动定义了所有的染色体颜色,我仍然在相邻的两个染色体之间有重复。我不确定为什么或者我做错了什么?
如有任何帮助,我们不胜感激!我的代码如下:
# read in data
data <- read.table(args[1], header = TRUE)
pval = args[2]
data = as.data.frame(data)
data[pval] <- lapply(data[pval], as.numeric)
# load libraries
library(ggplot2)
library(dplyr)
data$BPcum <- NA
s <- 0
nbp <- c()
for (i in unique(data$CHR)){
nbp[i] <- max(data[data$CHR == i,]$BP)
data[data$CHR == i,"BPcum"] <- data[data$CHR == i,"BP"] + s
s <- s + nbp[i]
}
axisdf = data %>% group_by(CHR) %>% summarize(center=( max(BPcum) + min(BPcum) ) / 2 )
# -log10(pval) and bonferonni cut off
data$logp <- -log10(data[pval])
bofferroni<- -log10(0.05/dim(data)[1])
# filter data so it can graph a little more easily
data<-data[!(data[pval]==0),]
data <- filter(data, data[pval] < 1e-1)
data$logp = unlist(data$logp)
# plot
plot <- ggplot(data, aes(x=BPcum, y=logp)) +
# add bofferroni cut off
geom_hline(yintercept=bofferroni, color = "red", size=.65) +
# show all points
geom_point( aes(color=as.factor(CHR)), alpha=0.4, size=1.3) +
scale_color_manual(values = rep(c("#D2A18C", "#785C50"), 12)) +
# custom X axis:
scale_x_continuous( label = axisdf$CHR, breaks= axisdf$center ) +
# remove space between plot area and x axis
scale_y_continuous(expand = c(0, 0) ) +
# add title and lables
ggtitle(paste(args[2],"Manhattan Plot")) +
xlab("BP") + ylab("-log10(PVALUE)") +
# custom the theme:
theme_bw() +
theme(
legend.position="none",
panel.border = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank()
)
pdf(paste("gwasplot", args[2], args[1], ".pdf", sep="_"), height=6, width=12)
plot
dev.off()发布于 2020-12-30 05:13:28
好的,我从一个修复了这个问题的同事那里得到了帮助,我们需要重新排序CHR的水平,因为它们的顺序是“1,10,11,12…2,21,22“等,而不是”1,2,3,4…“。为此,我添加了下面这一行
# reorder factor levels for plot
data$CHR = factor(data$CHR, levels = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"11", "12", "13", "14", "15", "16", "17", "18", "19", "20",
"21", "22", "23", "24", "X"))我还改变了这个
geom_point( aes(color=as.factor(CHR)), alpha=0.4, size=1.3) +至
geom_point( aes(color=CHR), alpha=0.4, size=1.3) +希望这对其他人有帮助!
https://stackoverflow.com/questions/65487169
复制相似问题