其他
相关性热图还能玩出什么花样?
#查看范例数据的前6行;
head(mtcars)
#计算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)
实用科研工具推荐
详实生信软件教程分享
前沿创新组学文章解读
独家生信视频教程发布