R语言中绘制曼哈顿图--线段图
001 完整代码
dat <- read.table("result.fst", header = T) head(dat) chrlen <- vector() for (i in unique(dat$CHR)) { temp <- dat[dat$CHR == i, ] temp <- temp[order(temp[,3], decreasing = T),] chrlen <- c(chrlen, temp[,3][1]) } gap <- 0.005 * sum(chrlen) chrpos <- cumsum(chrlen[-length(chrlen)] + gap) chrpos <- c(0, chrpos) names(chrpos) <- unique(dat$CHR) dat$POS <- dat$POS + chrpos[dat$CHR] colsource <- c("red", "black", "blue", "green", "cyan", "purple") colchr <- rep(colsource, 10)[1:length(chrlen)] chr <- unique(dat$CHR) idx <- match(dat$CHR, chr) dat$COL <- colchr[idx] chrloc <- vector() for (i in unique(dat$CHR)) { chrloc <- c(chrloc, chrpos[i] + round(1/2 * chrlen[i])) } par(mai = c(1, 1, 0.6, 0.6), mgp = c(3, 1, 0)) plot(dat$POS, dat$FST, ylim = c(0, 0.8),type = "n",ann = F, axes = F, yaxs = "i", xaxs = "i",bty = "l") for (i in 1:nrow(dat)) { segments(dat$POS[i],0, dat$POS[i], dat$FST[i], col = dat$COL[i]) } axis(2,at = c(0,0.2,0.4,0.6, 0.8),labels = c(0,0.2,0.4,0.6, 0.8), font=2,tck=-0.015,lwd=3,las = 1,lwd.ticks =3,cex.axis= 1,line = 0) axis(1,at = chrloc,labels = unique(dat$CHR), font=2,tck=-0.015,lwd=3,las = 1,lwd.ticks =3,cex.axis= 1,line = 0) axis(1,at = c(0, dat$POS[nrow(dat)]),labels = NA, font=2,tck=0,lwd=3,las = 1,lwd.ticks =3,cex.axis= 1,line = 0) threshold <- quantile(dat$FST, 0.99) abline(h = threshold, lwd = 3, col = "grey") mtext(side = 2,expression(Fst),adj = 0.5,line = 2.8,font = 2,cex=1.8) mtext(side = 1,"Chromosome",adj = 0.5,line = 2.8,font = 2,cex=1.7)
002 运行过程:
> dat <- read.table("result.fst", header = T) > head(dat) ## 数据结构 CHR SNP POS NMISS FST 1 1 snp00001 55910 100 0.256630 2 1 snp00002 85204 100 0.000000 3 1 snp00003 122948 100 0.101524 4 1 snp00004 203750 100 0.181254 5 1 snp00005 312707 100 0.052965 6 1 snp00006 400518 100 0.000000 > chrlen <- vector() > for (i in unique(dat$CHR)) { + temp <- dat[dat$CHR == i, ] + temp <- temp[order(temp[,3], decreasing = T),] + chrlen <- c(chrlen, temp[,3][1]) + } > gap <- 0.005 * sum(chrlen) > chrpos <- cumsum(chrlen[-length(chrlen)] + gap) > chrpos <- c(0, chrpos) > names(chrpos) <- unique(dat$CHR) > dat$POS <- dat$POS + chrpos[dat$CHR] > colsource <- c("red", "black", "blue", "green", "cyan", "purple") > colchr <- rep(colsource, 10)[1:length(chrlen)] > chr <- unique(dat$CHR) > idx <- match(dat$CHR, chr) > dat$COL <- colchr[idx] > chrloc <- vector() > for (i in unique(dat$CHR)) { + chrloc <- c(chrloc, chrpos[i] + round(1/2 * chrlen[i])) + } > par(mai = c(1, 1, 0.6, 0.6), mgp = c(3, 1, 0)) > plot(dat$POS, dat$FST, ylim = c(0, 0.8),type = "n",ann = F, axes = F, yaxs = "i", xaxs = "i",bty = "l") > for (i in 1:nrow(dat)) { + segments(dat$POS[i],0, dat$POS[i], dat$FST[i], col = dat$COL[i]) + } > axis(2,at = c(0,0.2,0.4,0.6, 0.8),labels = c(0,0.2,0.4,0.6, 0.8), + font=2,tck=-0.015,lwd=3,las = 1,lwd.ticks =3,cex.axis= 1,line = 0) > axis(1,at = chrloc,labels = unique(dat$CHR), + font=2,tck=-0.015,lwd=3,las = 1,lwd.ticks =3,cex.axis= 1,line = 0) > axis(1,at = c(0, dat$POS[nrow(dat)]),labels = NA, + font=2,tck=0,lwd=3,las = 1,lwd.ticks =3,cex.axis= 1,line = 0) > threshold <- quantile(dat$FST, 0.99) > abline(h = threshold, lwd = 3, col = "grey") > mtext(side = 2,expression(Fst),adj = 0.5,line = 2.8,font = 2,cex=1.8) > mtext(side = 1,"Chromosome",adj = 0.5,line = 2.8,font = 2,cex=1.7)
003 绘图结果
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET10 - 预览版1新功能体验(一)
2021-05-17 c语言 ??????
2020-05-17 linux中vim如何快速跳转至行首或者行尾