相關(guān)性熱圖属拾,顧名思義就是根據(jù)數(shù)據(jù)之間的相關(guān)性系數(shù)來繪制熱圖升敲,可分為2類:組內(nèi)相關(guān)性熱圖
與組間相關(guān)性熱圖
組內(nèi)相關(guān)性熱圖炸茧,即由一組數(shù)據(jù)內(nèi)部的相關(guān)性系數(shù)繪制而成,廢話不多說直接看代碼
相關(guān)性矩陣可視化
library(tidyverse)
library(corrplot)
cor(mtcars) %>% corrplot(method = "circle",order = "hclust",
type = "lower",tl.srt = 45,tl.col = "black")
正相關(guān)以藍色顯示,負相關(guān)以紅色顯示基协。顏色強度和圓圈的大小與相關(guān)系數(shù)成正比
full
:顯示完整的相關(guān)矩陣
upper
:顯示相關(guān)矩陣的上三角
lower
:顯示相關(guān)矩陣的下三角
將相關(guān)圖與顯著性檢驗相結(jié)合
cor.mtest( )函數(shù)計算P值
res1 <- cor.mtest(mtcars)
sig.level = -1
顯示所有p值
cor(mtcars)%>% corrplot(type="lower", order="hclust",tl.srt = 45,
tl.col = "black",
p.mat = res1$p,insig = "p-value",sig.level = -1)
顯示不顯著點的p值
cor(mtcars)%>% corrplot(type="lower", order="hclust",
p.mat = res1$p,insig = "p-value")
將不顯著的點用空表示
cor(mtcars)%>% corrplot(type="lower",order="hclust",
p.mat = res1$p,insig = "blank")
在p值> 0.05的點上打X
cor(mtcars)%>% corrplot(type="lower",order="hclust",
p.mat = res1$p, sig.level = .05)
在p值> 0.01的點上打X
cor(mtcars)%>% corrplot(type="lower",order="hclust",
p.mat = res1$p, sig.level = .01)
將p值轉(zhuǎn)化為*
添加于圖上
cor(mtcars) %>% corrplot(type="lower", order="hclust",
p.mat = res1$p,insig = "label_sig",
sig.level = c(.001, .01, .05),
pch.cex = .9, pch.col = "white",
tl.srt = 45,tl.col = "black")
ggplot2繪制組內(nèi)相關(guān)性熱圖
corr.test( )
會同時計算p值與相關(guān)性系數(shù),通過for循環(huán)將p值轉(zhuǎn)化為*
library("reshape")
library("psych")
p <- corr.test(mtcars,method="pearson",adjust = "fdr")
cor <- p$r
pvalue <- p$p
display <- pvalue
l1 <- nrow(display)
l2 <- ncol(display)
for(i in 1:l1){
for(k in 1:l2){
a <- as.numeric(display[i,k])
if(a <= 0.001){
a <- "***"
}
if( 0.001 < a && a <= 0.01){
a <- "**"
}
if(0.01 < a && a < 0.05){
a <- "*"
}
if(a >= 0.05){
a <- ""
}
display[i,k] <- a
}
}
構(gòu)建ggplot2繪圖文件并導(dǎo)出
heatmap <- melt(cor) %>% rename(replace=c("X1"="sample1","X2"="sample2",
"value"="cor")) %>%
mutate(pvalue=melt(pvalue)[,3]) %>%
mutate(display=melt(display)[,3])
write.table(heatmap,file ="heatmap.xls",sep ="\t",row.names = F)
ggplot2可視化
ggplot(heatmap,aes(sample1,sample2,fill=cor))+
geom_tile()+
theme_minimal()+
scale_fill_distiller(palette = "Spectral")+
geom_text(aes(label=display),size=5,color="white")+
scale_y_discrete(position="left")+xlab(NULL) + ylab(NULL)+
labs(fill ="expr")