1. ggplot2包ggplot函数绘制相关性热图
### 1. ggplot2包ggplot函数绘制相关性热图
rm(list = ls())
head(mtcars[,1:6]) #查看前六行六列
cormtcars <- round(cor(mtcars), 3) #round()函数自定义小数点后位数
head(cormtcars[,1:6])
library(ggplot2)
library(tidyverse)
library(reshape2)
m_data <- as.data.frame(cormtcars) %>% #将矩阵转换成数据框
mutate(x=rownames(cormtcars)) %>% #新建一列x,是11种属性变量
melt(id='x') %>% #将宽数据转换成长数据,更适合ggplot2绘图
dplyr::rename('y'='variable','Corr'='value') #将variable列名改成y
#先确定好绘图时x轴、y轴的因子顺序:
list <- rownames(cormtcars)
list <- factor(list,levels = list)
ggplot(data=m_data,aes(factor(x,levels = list),
factor(y,levels = list), #定义x,y轴顺序,防止被默认改变
fill=Corr))+ #根据相关性值填充颜色
geom_tile()+ #色块函数
scale_fill_gradient2(low = 'blue',mid = 'white',high ='red',
limits=c(-1,1),breaks=c(-1,-0.5,0,0.5,1))+ # 颜色范围
labs(x=NULL,y=NULL)+ # 去掉x,y轴label
theme_bw(base_size = 15) #边框颜色,坐标轴文字大小
2. ggcorrplot包ggcorrplot 函数绘制相关性热图
ggcorrplot(corr, method = c("square", "circle"), type = c("full",
"lower", "upper"), ggtheme = ggplot2::theme_minimal, title = "",
show.legend = TRUE, legend.title = "Corr", show.diag = FALSE,
colors = c("blue", "white", "red"), outline.color = "gray",
hc.order = FALSE, hc.method = "complete", lab = FALSE,
lab_col = "black", lab_size = 4, p.mat = NULL, sig.level = 0.05,
insig = c("pch", "blank"), pch = 4, pch.col = "black",
pch.cex = 5, tl.cex = 12, tl.col = "black", tl.srt = 45,
digits = 2)
### 2. ggcorrplot包ggcorrplot 函数绘制相关性热图
#install.packages('ggcorrplot')
library(ggcorrplot)
ggcorrplot(cormtcars) # 一个函数搞定,不需要转化为长数据格式
#默认是方形,修改为圆形显示,并标上相关系数
ggcorrplot(cormtcars,method = "circle",lab=T)
#使用ggcorrplot包的cor_pmat()函数计算p值:
pmtcars <- cor_pmat(mtcars)
head(pmtcars[,1:3])
ggcorrplot(cormtcars,hc.order = T, #分等级聚类重排矩阵
ggtheme = ggplot2::theme_void(base_size = 15), #主题修改
colors = c("CornflowerBlue","white","Salmon"), #自定义颜色
lab = T,lab_size = 5,#相关系数文本字体大小
tl.cex = 15, #坐标轴字体大小
p.mat = pmtcars, #添加显著性信息
sig.level = 0.01, #显著性水平
pch = 4, #不够显著的色块标记形状,4:X
pch.cex = 10) #不显著标记的大小,使用insig = "blank"将不显著的空白处理
3.corrplot包corrplot函数绘制相关性热图
Usage
corrplot(
corr,
method = c("circle", "square", "ellipse", "number", "shade", "color", "pie"),
type = c("full", "lower", "upper"),
col = NULL,
col.lim = NULL,
bg = "white",
title = "",
is.corr = TRUE,
add = FALSE,
diag = TRUE,
outline = FALSE,
mar = c(0, 0, 0, 0),
addgrid.col = NULL,
addCoef.col = NULL,
addCoefasPercent = FALSE,
order = c("original", "AOE", "FPC", "hclust", "alphabet"),
hclust.method = c("complete", "ward", "ward.D", "ward.D2", "single", "average",
"mcquitty", "median", "centroid"),
addrect = NULL,
rect.col = "black",
rect.lwd = 2,
tl.pos = NULL,
tl.cex = 1,
tl.col = "red",
tl.offset = 0.4,
tl.srt = 90,
cl.pos = NULL,
cl.length = NULL,
cl.cex = 0.8,
cl.ratio = 0.15,
cl.align.text = "c",
cl.offset = 0.5,
number.cex = 1,
number.font = 2,
number.digits = NULL,
addshade = c("negative", "positive", "all"),
shade.lwd = 1,
shade.col = "white",
p.mat = NULL,
sig.level = 0.05,
insig = c("pch", "p-value", "blank", "n", "label_sig"),
pch = 4,
pch.col = "black",
pch.cex = 3,
plotCI = c("n", "square", "circle", "rect"),
lowCI.mat = NULL,
uppCI.mat = NULL,
na.label = "?",
na.label.col = "black",
win.asp = 1,
...
)
#### 3.corrplot包corrplot函数绘制相关性热图
library(corrplot)
my_color <- colorRampPalette(c("red", "orange", "blue"),space = "rgb")(10)
M = cor(mtcars)
testRes = cor.mtest(mtcars, conf.level = 0.95)
corrplot(M, order = 'AOE', method = "circle",
type = 'upper', tl.pos = 'td',
p.mat = testRes$p, insig = "label_sig",
pch.cex = .9, tl.col="black",
#col = my_color
col = c("CornflowerBlue","white","Salmon")
)
# tl.pos: Character or logical, position of text labels.
# If character, it must be one of 'lt', 'ld', 'td', 'd' or 'n'.
# 'lt'(default if type=='full') means left and top,
# 'ld'(default if type=='lower') means left and diagonal,
# 'td'(default if type=='upper') means top and diagonal(near),
# 'l' means left, 'd' means diagonal, 'n' means don't add text-label.
# method = c("circle", "square", "ellipse", "number", "shade", "color", "pie")
# type:'full' (default), 'upper' or 'lower',
# display full matrix, lower triangular or upper triangular matrix.
# p.mat:Matrix of p-value, if NULL,
# parameter sig.level, insig, pch, pch.col, pch.cex are invalid.
corrplot(M, col = my_color, add = TRUE, type = 'lower',
method = 'number', order = 'AOE', diag = FALSE,
tl.pos = 'n', cl.pos = 'n')