【这图怎么画】相关性热图,这样画才好看!
点击蓝字
关注小图
热图是科研论文中一种常见的可视化手段,而在转录组研究领域,我们常常需要分析一些基因与基因之间的相关性,来判断生物样本中是否存在共表达情况,以及共表达基因模块。除了基因集之间,其他方向,比如免疫细胞群体之间相关性,样本的相关性,也常常用相关性热图的形式进行展示。总而言之,往大了说,任何表征相关性的数值都可以用相关性热图来进行绘制。
小图先带大家来看看下面这张图,这是一篇发表在 PLoS Medicine (注意不是 Plos One, 该杂志 IF = 11.048) 上的文章图,来看 22 种免疫细胞群体之间的相关性,其中红色的颜色代表正相关,蓝色代表负相关。每一格的数字代表相关系数。这是一种经常会用到的图形,不同于常规热图(参考:热图绘制)。常规热图中的每行代表一个观察值,每列代表一个样本,而小图在本次教程中,将为大家带来更高级,也更美观的相关性热图。

话不多说,直接开始~
先看一下数据结构:
data1=read.table("score-WLD_cor.txt",sep='t',header = T)

data2=read.table("score-WLD_P.txt",sep='t',header = T)

data11=data1[1:19,2:20]
data11=as.matrix(data11)
rownames(data11)=data1[,1]
data22=data[,2:17]
data22=as.matrix(data22)
rownames(data22)=data[,1]
corr=data22
p.mat=data22
data1=data[,2:17]
rownames(data1)=data[,1]
data1=as.matrix(data22)
计算相关性和P值
library(Hmisc)
res2 <- rcorr(as.matrix(data1))
result=res2
corr=result$r
p=result$P
write.table(result$r, "score-WLP_cor.txt", quote = F, sep = "t", row.names = F)

write.table(result$P, "score-WLP_P.txt", quote = F, sep = "t", row.names = F)

绘制相关性热图
install.packages("ggcorrplot")
library(ggplot2)
library(ggcorrplot)
ggcorrplot(cor(data22))
ggcorrplot(cor(data22),outline.color = "red",tl.srt = TRUE)
ggcorrplot(s,show.diag = TRUE,type = "lower",lab = TRUE)
ggcorrplot(s,hc.order = TRUE,p.mat=p)
corr <- cor(data22)#计算相关性矩阵
p.mat<- cor_pmat(data22)#计算相关性矩阵的P值
ggcorrplot(corr,hc.order = TRUE,p.mat=p.mat)
library(corrplot)
#设置图中字体形式为Tiime New Roman
?par
## 方法1:使用par设置参数,此参数有设备依赖,可能会报“Windows字体数据库里没有这样的字体系列”的警告信息。
m = par(no.readonly = TRUE) # 查看可修改图形参数,返回的是一个列表,family是修改字体的参数。
par(family= "Times New Roman") # 设置全局使用Times New Roman字体,英文字体分为Serif和Sans-Serif字体。Times New Roman是一种Serif字体,Arial是一种Sans-Serif字体。
par("family") # 参看par的family设置
pdf(file = "lncRNA_COR.pdf",width = 6.5,height = 6.5)
cor.plot<-corrplot(corr =corr,p.mat = p.mat,type="upper",
tl.pos="lt",tl.col="black",
insig = "label_sig", sig.level = c(.01, .05),
pch.cex=1,pch.col = "black",order = "original") # ,order参数是对数据进行排序,后面再细讲。
cor.plot<-corrplot(corr = corr,type="lower",add=TRUE,method="number",
tl.pos="n",tl.col="black",tl.cex=1.2,
col="black",diag=FALSE, cl.pos="n",pch.col = "black",
number.cex = 0.8,order = "original")
dev.off()
par(m) # 还原初始图形参数设置
##
#font_import() # 导入字体,花的时间有点长。
install.packages("corrplot")
library(corrplot)
## 方法2:pdf()保存图片时设置为Times New Roman
pdf(file = "lncRNA_sample_cor.pdf",width = 8,height = 8,family="Times")
cor.plot<-corrplot(corr =corr,p.mat = p.mat,type="upper",
tl.pos="lt",tl.col="black",
insig = "label_sig", sig.level = c(.01, .05),
pch.cex=1,pch.col = "black",order = "original") #,order参数是对数据进行排序,后面再细讲。
cor.plot<-corrplot(corr = corr,type="lower",add=TRUE,method="number",
tl.pos="n",tl.col="black",tl.cex=1.2,
col="black",diag=FALSE, cl.pos="n",pch.col = "black",
number.cex = 0.8,order = "original")
dev.off()

这样的热图,既能完美征服大小老板,也能瞬间让编辑和同行评议专家们的好感度蹭蹭蹭的上升几个层次。
小图鸡汤
每一张科研制图都需要精雕细琢,勤写代码,多看帮助文档,可以让我们更好更快的掌握科研制图的精要,也能让我们的工作更完美的展现出来。加油吧!
不想写代码?可以用我们的工具云生信平台在线绘制堆积柱形图。谢谢你的阅读,请期待小图的下一期分享吧!
欢迎使用:云生信平台 ( http://www.biocloudservice.com/home.html)

往期推荐 |
|
|
|
👇点击阅读原文进入网址