前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >全了!!曼哈顿图样样式、方法大汇总(Python+R)~

全了!!曼哈顿图样样式、方法大汇总(Python+R)~

作者头像
DataCharm
发布2021-11-04 15:27:06
3K0
发布2021-11-04 15:27:06
举报

最近小编在后台看到有的小伙伴留言咨询曼哈顿图(Manhattan Plot) 的绘制方法,小编一开始也是比较不了解,奈何我又是一个宠读者的小编,这就汇总了曼哈顿图(Manhattan Plot) R和Python的绘制方法,和大家一起进步。主要内容如下:

  • 曼哈顿图(Manhattan Plot)简介
  • 曼哈顿图(Manhattan Plot) R绘制方法
  • 曼哈顿图(Manhattan Plot) Python绘制方法

曼哈顿图(Manhattan Plot)简介

曼哈顿图(Manhattan Plot) 是一种散点图,通常用于显示具有大量数据点,许多非零振幅和更高振幅值分布的数据。(大家知道意思就可以了哈~~),样例如下:

曼哈顿图样例

曼哈顿图(Manhattan Plot) R绘制方法

使用R绘制曼哈顿图(Manhattan Plot) 的方法很多,这里主要介绍R-CMplot包的绘制方法,如下:

代码语言:javascript
复制
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

当然,你也可以通添加图例和修改颜色,如下:

代码语言:javascript
复制
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),如下:

代码语言:javascript
复制
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 make

  • 在曼哈顿图的底部附加染色体密度
代码语言:javascript
复制
CMplot(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绘制曼哈顿图则需要进行必要的数据处理操作,详细内容如下:

代码语言:javascript
复制
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。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-11-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 DataCharm 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 曼哈顿图(Manhattan Plot)简介
  • 曼哈顿图(Manhattan Plot) R绘制方法
  • 曼哈顿图(Manhattan Plot) Python绘制方法
  • 总结
    • 参考资料
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档