#計算mtcars數據框的相關性係數;
cor<- cor(mtcars)
class(cor)
#查看得到相關性係數矩陣的前5行,前5列;
cor[1:5,1:5]
#安裝R包;
install.packages("corrplot")
#載入相關R包;
library(corrplot)
#默認的繪製方法是 "circle",圖形的樣式還可以是"square"、"ellipse"、"pie"和"color"等,其中method = "number"時,只顯示相關性係數;
corrplot(cor, method = "square")
#可繪製lower、lower、full三種布局方式的熱圖,當然也可以混合顯示;
corrplot.mixed(cor, lower = "square", upper = "circle", tl.col = "black")
#常見的排序方式有 "AOE", "FPC", "hclust", "alphabet";
#"AOE" 是指按特徵向量的角度排序(angular order of the eigenvectors);
#"FPC" 是按照 first principal component 的順序排序;
#"hclust"是按照分層聚類的結果排序;
#"alphabet"按照名稱字母順序排序。
corrplot(cor, order = "hclust")
#如果是選"hclust",還可以根據聚類結果添加矩形框;
corrplot(cor, order = "hclust", addrect = 2)
#自定義漸變顏色;
col2 <- colorRampPalette(c("#77C034","white" ,"#C388FE"),alpha = TRUE)
#使用這些漸變顏色;
#addgrid.col調整網格顏色;
#outoutline指定圖形描邊;
corrplot(cor, order = "hclust", addgrid.col = "grey70",type = "upper",
outline = "orange",col = col2(100),method = "square",diag = F)
#Tips:這裡的col2的對象類型是函數!
col2(100)
class(col2)
#自定義文本標籤顏色:
#tl.* 系列參數用於調整文本標籤;
#tl.col (text label color) 調整文字標籤顏色;
#tl.srt (text label string rotation) 調標籤角度(橫軸方向);
#tl.cex調字體大小;
#使用r自帶顏色集cm.colors的效果如下;
corrplot(cor, order = "hclust",col = cm.colors(100),method = "square",
tl.col="black",tl.cex = 0.8)
# cl.* 系列參數用於調整圖例;
#cl.ratio 調整顏色條的寬度;
#cl.align調整顏色條刻度標籤的對齊方式;
#cl.length指定顏色條標籤個數;
corrplot(cor, order = "hclust",col = col2(100),method = "circle",
cl.length=5,addgrid.col = NA,outline = "grey60",
tl.col="black",tl.cex = 0.8,cl.pos = "r",cl.ratio = 0.2)
#繪製上三角熱圖;
corrplot(cor, order = "hclust",col = col2(100),method = "square",
cl.length=5, type = "upper",diag = F,
tl.col="black",tl.cex = 0.8,cl.pos = "r",cl.ratio = 0.2)
#去掉圖例和標籤;
corrplot(cor, order = "hclust",col = col2(100),method = "square",
tl.col="black",tl.cex = 0.8,cl.pos = "n",tl.pos = "n")
#添加顯著性標記:
#使用cor.mtest做顯著性檢驗;
res1 <- cor.mtest(mtcars, conf.level = .95)
res2 <- cor.mtest(mtcars, conf.level = .99)
#提取p值矩陣;
p.mat = res1$p
p.mat[1:5,1:5]
#指定顯著性水平,不顯著的為空白;
corrplot(cor, order = "hclust",col = col2(100),method = "color",
tl.col="black",tl.cex = 0.8,cl.pos = "r",cl.ratio = 0.2,
p.mat = res1$p, sig.level = .05,insig = "blank",addgrid.col="white")
#顯示P值;
corrplot(cor, order = "hclust",col = col2(100),method = "color",
tl.col="black",tl.cex = 0.8,cl.pos = "r",cl.ratio = 0.2,
p.mat = res1$p, sig.level = -1,pch.cex=1,
insig = "p-value", pch.col = "white")
#顯示相關係數;
#par(lty=2)可指定線的粗細,遺憾的是會作用到圖例的線條;
corrplot(cor, order = "hclust",col = col2(100),method = "color",
cl.length=5,addgrid.col="white",cl.pos = "r",
addCoef.col="black",number.cex=0.6,number.digits=1,number.font=1,
tl.col="black",tl.cex = 0.8,cl.ratio = 0.2)
#缺失值的展示,默認是用「?」,也可以自定義;
#corrplot(cor, na.label = "NA")
#insig為"p-value",不顯著的格子顯示p值;
#insig為"label_sig",可用於顯示不同顯著水平的星標;
#insig為"pch" (default),不顯著的格子顯示pch值對應圖形;
corrplot(cor, order = "hclust",col = col2(100),method = "color",
tl.col="black",tl.cex = 0.8,cl.pos = "r",cl.ratio = 0.2,
p.mat = res1$p, sig.level = c(.001, .01, .05),outline="white",
insig = "label_sig",pch.cex = 1.2, pch.col = "white")
#安裝方法:
# install.packages("devtools")
devtools::install_github("houyunhuang/ggcor")
#載入R包;
library(ggcor)
library(ggplot2)
#直接快速繪製整個相關性熱圖;
quickcor(mtcars, cluster = TRUE,cor.test = TRUE) +
geom_colour() +
geom_mark(size=3,color="white",fontface=1)+
scale_fill_gradientn(colours = c("#77C034","white" ,"#C388FE"))+
geom_panel_grid(colour = "white",size = 1)
#直接繪製上三角熱圖;
quickcor(mtcars, cluster = TRUE,type = "upper",cor.test = TRUE) +
geom_colour(data = get_data(type = "upper")) +
geom_mark(data = get_data( type = "upper"),size=3,color="white",fontface=1)+
scale_fill_gradientn(colours = c("#77C034","white" ,"#C388FE"))+
geom_panel_grid(colour = "white",size = 0.6)
#直接繪製上三角「方塊」熱圖;
quickcor(mtcars, cluster = TRUE,type = "upper",cor.test = TRUE) +
geom_square(data = get_data(type = "upper")) +
scale_fill_gradientn(colours = c("#77C034","white" ,"#C388FE"))+
geom_panel_grid(size = 0.6)
#直接繪製上三角「氣泡」熱圖;
quickcor(mtcars, cluster = TRUE,type = "upper",cor.test = TRUE) +
geom_circle2(data = get_data(type = "upper"),colour="white") +
scale_fill_gradientn(colours = c("#77C034","white" ,"#C388FE"))+
geom_panel_grid(colour = "white",size = 0.6)
實用科研工具推薦
詳實生信軟體教程分享
前沿創新組學文章解讀
獨家生信視頻教程發布