最近小編在後臺看到有的小夥伴留言諮詢曼哈頓圖(Manhattan Plot) 的繪製方法,小編一開始也是比較不了解,奈何我又是一個寵讀者的小編,這就匯總了曼哈頓圖(Manhattan Plot) R和Python的繪製方法,和大家一起進步。主要內容如下:
曼哈頓圖(Manhattan Plot) 是一種散點圖,通常用於顯示具有大量數據點,許多非零振幅和更高振幅值分布的數據。(大家知道意思就可以了哈~~),樣例如下:
曼哈頓圖樣例曼哈頓圖(Manhattan Plot) R繪製方法使用R繪製曼哈頓圖(Manhattan Plot) 的方法很多,這裡主要介紹R-CMplot包的繪製方法,如下:
library(CMplot)
data(pig60K)
data(cattle50K)
# 這邊的數據都是規整好的DF類型數據
#可視化繪製
CMplot(pig60K,type="p",plot.type="c",chr.labels=paste("Chr",c(1:18,"X","Y"),sep=""),r=0.4,cir.legend=TRUE,
outward=FALSE,cir.legend.col="black",cir.chr.h=1.3,chr.den.col="black",
file="jpg",memo="",dpi=600,file.output=TRUE,verbose=TRUE,width=10,height=10)
Example01 Of CMplot make當然,你也可以通添加圖例和修改顏色,如下:
CMplot(pig60K,type="p",plot.type="c",r=0.4,col=c("grey30","grey60"),chr.labels=paste("Chr",c(1:18,"X","Y"),sep=""),
threshold=c(1e-6,1e-4),cir.chr.h=1.5,amplify=TRUE,threshold.lty=c(1,2),threshold.col=c("red",
"blue"),signal.line=1,signal.col=c("red","green"),chr.den.col=c("darkgreen","yellow","red"),
bin.size=1e6,outward=FALSE,file="jpg",memo="",dpi=300,file.output=TRUE,verbose=TRUE,width=10,height=10)
Example02 Of CMplot make你也可以繪製常規的曼哈頓圖(Manhattan Plot),如下:
CMplot(pig60K,type="p",plot.type="m",LOG10=TRUE,threshold=NULL,file="jpg",memo="",dpi=300,
file.output=TRUE,verbose=TRUE,width=14,height=6,chr.labels.angle=45)
Example03 Of CMplot makeCMplot(pig60K, plot.type="m", LOG10=TRUE, ylim=NULL, threshold=c(1e-6,1e-4),threshold.lty=c(1,2),
threshold.lwd=c(1,1), threshold.col=c("black","grey"), amplify=TRUE,bin.size=1e6,
chr.den.col=c("darkgreen", "yellow", "red"),signal.col=c("red","green"),signal.cex=c(1.5,1.5),
signal.pch=c(19,19),file="jpg",memo="",dpi=300,file.output=TRUE,verbose=TRUE,
width=14,height=6)
Example03 Of CMplot make with chromosome density這裡還有更多的關於CMplot包繪製曼哈頓圖(Manhattan Plot)的小例子,具體就是CMplot()繪圖函數中不同參數的設置。更多詳細參數設置可參考:R-CMplot包官網[1]
曼哈頓圖(Manhattan Plot) Python繪製方法Python繪製曼哈頓圖則需要進行必要的數據處理操作,詳細內容如下:
from pandas import DataFrame
from scipy.stats import uniform
from scipy.stats import randint
import numpy as np
import matplotlib.pyplot as plt
# sample data
df = DataFrame({'gene' : ['gene-%i' % i for i in np.arange(10000)],
'pvalue' : uniform.rvs(size=10000),
'chromosome' : ['ch-%i' % i for i in randint.rvs(0,12,size=10000)]})
# -log_10(pvalue)
df['minuslog10pvalue'] = -np.log10(df.pvalue)
df.chromosome = df.chromosome.astype('category')
df.chromosome = df.chromosome.cat.set_categories(['ch-%i' % i for i in range(12)], ordered=True)
df = df.sort_values('chromosome')
# How to plot gene vs. -log10(pvalue) and colour it by chromosome?
df['ind'] = range(len(df))
df_grouped = df.groupby(('chromosome'))
# manhattan plot
fig = plt.figure(figsize=(10,4),dpi=100)
ax = fig.add_subplot(111)
colors = ["#30A9DE","#EFDC05","#E53A40","#090707"]
x_labels = []
x_labels_pos = []
for num, (name, group) in enumerate(df_grouped):
group.plot(kind='scatter', x='ind', y='minuslog10pvalue',color=colors[num % len(colors)], ax=ax)
x_labels.append(name)
x_labels_pos.append((group['ind'].iloc[-1] - (group['ind'].iloc[-1] - group['ind'].iloc[0])/2))
# add grid
ax.grid(axis="y",linestyle="--",linewidth=.5,color="gray")
ax.tick_params(direction='in',labelsize=13)
ax.set_xticks(x_labels_pos)
ax.set_xticklabels(x_labels)
ax.set_xlim([0, len(df)])
ax.set_ylim([0, 4])
# x axis label
ax.set_xlabel('Chromosome',size=14)
plt.savefig('Manhattan Plot in Python.png',dpi=900,bbox_inches='tight',facecolor='white')
plt.show()
Example Of Manhattan Plot in Python以上就是對曼哈頓圖(Manhattan Plot) 繪製R和Python小例子,希望對小夥伴們有所幫助~~。
總結今天的這篇推文算是回應了讀者的繪圖需求,簡單介紹了曼哈頓圖(Manhattan Plot) 的R和Python繪製方法,希望能幫助到需要的小夥伴們~~
再小的技能,也應該被認真對待。
參考資料[1]R-CMplot包官網: https://github.com/YinLiLin/CMplot。