R Corrplot软件包简介,绘制相关热图

Corrplot软件包简介

介绍

所述corrplot包是相关矩阵,置信区间的图形显示。它还包含一些进行矩阵重新排序的算法。另外,corrplot擅长细节,包括选择颜色,文本标签,颜色标签,布局等。

可视化方法

有七个可视化方法(参数method中)corrplot包,命名"circle""square""ellipse""number""shade""color""pie"

正相关以蓝色显示,负相关以红色显示。颜色强度和圆圈的大小与相关系数成正比。

library(corrplot)
## corrplot 0.84 loaded
M <- cor(mtcars)
corrplot(M, method = "circle")

R Corrplot软件包简介,绘制相关热图_第1张图片

corrplot(M, method = "square")

R Corrplot软件包简介,绘制相关热图_第2张图片

corrplot(M, method = "ellipse")

R Corrplot软件包简介,绘制相关热图_第3张图片

corrplot(M, method = "number") # Display the correlation coefficient

R Corrplot软件包简介,绘制相关热图_第4张图片

corrplot(M, method = "shade")

R Corrplot软件包简介,绘制相关热图_第5张图片

corrplot(M, method = "color")

R Corrplot软件包简介,绘制相关热图_第6张图片

corrplot(M, method = "pie")

R Corrplot软件包简介,绘制相关热图_第7张图片

布局

共有三种布局类型(参数type):

  • "full"(默认):显示完整的相关矩阵
  • "upper":显示相关矩阵的上三角
  • "lower":显示相关矩阵的下三角
corrplot(M, type = "upper")
corrplot(M, type = "upper")

R Corrplot软件包简介,绘制相关热图_第8张图片

corrplot.mixed() 是混合可视化样式的包装函数。

corrplot.mixed(M)

R Corrplot软件包简介,绘制相关热图_第9张图片

corrplot.mixed(M, lower.col = "black", number.cex = .7)

R Corrplot软件包简介,绘制相关热图_第10张图片

corrplot.mixed(M, lower = "ellipse", upper = "circle")

R Corrplot软件包简介,绘制相关热图_第11张图片

corrplot.mixed(M, lower = "square", upper = "circle", tl.col = "black")

R Corrplot软件包简介,绘制相关热图_第12张图片

重新排序相关矩阵

相关矩阵可以根据相关系数重新排序。这对于确定矩阵中隐藏的结构和图案很重要。有在corrplot(参数四种方法order)的名字命名 "AOE""FPC""hclust""alphabet"。在序列化包中可以找到更多算法 。

您还可以通过function手动“重新排序”矩阵corrMatOrder()

  • "AOE"一个我ai

    a i = { 棕褐色(e i 2 / e i 1),如果  ë 我1 > 0 ;棕褐色(e i 2 / e i 1)+ π ,除此以外。ai={tan⁡(ei2/ei1),if ei1>0;tan⁡(ei2/ei1)+π,otherwise.

    Ë 1e1Ë 2e2

  • "FPC" 对于第一个主成分订单。

  • "hclust"层次聚类顺序,以及"hclust.method"要使用的聚集方法。"hclust.method"应该是一个 "ward""single""complete""average""mcquitty""median"或 "centroid"

  • "alphabet" 按字母顺序排列。

corrplot(M, order = "AOE")

R Corrplot软件包简介,绘制相关热图_第13张图片

corrplot(M, order = "hclust")

R Corrplot软件包简介,绘制相关热图_第14张图片

corrplot(M, order = "FPC")

R Corrplot软件包简介,绘制相关热图_第15张图片

corrplot(M, order = "alphabet")

R Corrplot软件包简介,绘制相关热图_第16张图片

如果使用"hclust",则corrplot()可以基于层次聚类的结果在相关矩阵图的周围绘制矩形。

corrplot(M, order = "hclust", addrect = 2)

R Corrplot软件包简介,绘制相关热图_第17张图片

corrplot(M, order = "hclust", addrect = 3)

R Corrplot软件包简介,绘制相关热图_第18张图片

# Change background color to lightblue
corrplot(M, type = "upper", order = "hclust",
         col = c("black", "white"), bg = "lightblue")

R Corrplot软件包简介,绘制相关热图_第19张图片

使用不同的色谱

如上节所示,可以自定义相关图的颜色。该功能colorRampPalette()非常便于生成色谱。

col1 <- colorRampPalette(c("#7F0000", "red", "#FF7F00", "yellow", "white",
                           "cyan", "#007FFF", "blue", "#00007F"))
col2 <- colorRampPalette(c("#67001F", "#B2182B", "#D6604D", "#F4A582",
                           "#FDDBC7", "#FFFFFF", "#D1E5F0", "#92C5DE",
                           "#4393C3", "#2166AC", "#053061"))
col3 <- colorRampPalette(c("red", "white", "blue")) 
col4 <- colorRampPalette(c("#7F0000", "red", "#FF7F00", "yellow", "#7FFF7F",
                           "cyan", "#007FFF", "blue", "#00007F"))
whiteblack <- c("white", "black")

## using these color spectra
corrplot(M, order = "hclust", addrect = 2, col = col1(100))

R Corrplot软件包简介,绘制相关热图_第20张图片

corrplot(M, order = "hclust", addrect = 2, col = col2(50))

R Corrplot软件包简介,绘制相关热图_第21张图片

corrplot(M, order = "hclust", addrect = 2, col = col3(20))

R Corrplot软件包简介,绘制相关热图_第22张图片

corrplot(M, order = "hclust", addrect = 2, col = col4(10))

R Corrplot软件包简介,绘制相关热图_第23张图片

corrplot(M, order = "hclust", addrect = 2, col = whiteblack, bg = "gold2")

R Corrplot软件包简介,绘制相关热图_第24张图片

您还可以使用标准调色板(包grDevices

corrplot(M, order = "hclust", addrect = 2, col = heat.colors(100))

R Corrplot软件包简介,绘制相关热图_第25张图片

corrplot(M, order = "hclust", addrect = 2, col = terrain.colors(100))

R Corrplot软件包简介,绘制相关热图_第26张图片

corrplot(M, order = "hclust", addrect = 2, col = cm.colors(100))

R Corrplot软件包简介,绘制相关热图_第27张图片

corrplot(M, order = "hclust", addrect = 2, col = gray.colors(100))

R Corrplot软件包简介,绘制相关热图_第28张图片

其他选择是使用RcolorBrewer包。

library(RColorBrewer)

corrplot(M, type = "upper", order = "hclust",
         col = brewer.pal(n = 8, name = "RdBu"))

R Corrplot软件包简介,绘制相关热图_第29张图片

corrplot(M, type = "upper", order = "hclust",
         col = brewer.pal(n = 8, name = "RdYlBu"))

R Corrplot软件包简介,绘制相关热图_第30张图片

corrplot(M, type = "upper", order = "hclust",
         col = brewer.pal(n = 8, name = "PuOr"))

R Corrplot软件包简介,绘制相关热图_第31张图片

更改文本标签和图例的颜色和旋转

参数cl.*用于颜色图例,tl.*如果用于文本图例。对于文本标签,tl.col(文本标签颜色)和tl.srt(文本标签字符串旋转)用于更改文本颜色和旋转。

这里有些例子。

## remove color legend and text legend 
corrplot(M, order = "AOE", cl.pos = "n", tl.pos = "n")  

R Corrplot软件包简介,绘制相关热图_第32张图片

## bottom  color legend, diagonal text legend, rotate text label
corrplot(M, order = "AOE", cl.pos = "b", tl.pos = "d", tl.srt = 60)

R Corrplot软件包简介,绘制相关热图_第33张图片

## a wider color legend with numbers right aligned
corrplot(M, order = "AOE", cl.ratio = 0.2, cl.align = "r")

R Corrplot软件包简介,绘制相关热图_第34张图片

## text labels rotated 45 degrees
corrplot(M, type = "lower", order = "hclust", tl.col = "black", tl.srt = 45)

R Corrplot软件包简介,绘制相关热图_第35张图片

处理非相关矩阵

corrplot(abs(M),order = "AOE", col = col3(200), cl.lim = c(0, 1))

R Corrplot软件包简介,绘制相关热图_第36张图片

## visualize a  matrix in [-100, 100]
ran <- round(matrix(runif(225, -100,100), 15))
corrplot(ran, is.corr = FALSE, method = "square")

R Corrplot软件包简介,绘制相关热图_第37张图片

## a beautiful color legend 
corrplot(ran, is.corr = FALSE, method = "ellipse", cl.lim = c(-100, 100))

R Corrplot软件包简介,绘制相关热图_第38张图片

如果矩阵是矩形,则可以使用win.asp参数调整纵横比, 以使矩阵呈现为正方形。

ran <- matrix(rnorm(70), ncol = 7)
corrplot(ran, is.corr = FALSE, win.asp = .7, method = "circle")

R Corrplot软件包简介,绘制相关热图_第39张图片

处理缺失(NA)值

默认情况下,corrplot将NA值呈现为"?"字符。使用na.label 参数,可以使用不同的值(最多支持两个字符)。

M2 <- M
diag(M2) = NA
corrplot(M2)

R Corrplot软件包简介,绘制相关热图_第40张图片

corrplot(M2, na.label = "o")

R Corrplot软件包简介,绘制相关热图_第41张图片

corrplot(M2, na.label = "NA")

R Corrplot软件包简介,绘制相关热图_第42张图片

在标签中使用“ plotmath”表达式

从version开始0.78,可以 在变量名称中使用 plotmath表达式。要激活plotmath渲染,前缀的人物之一的标签":""=""$"

M2 <- M[1:5,1:5]
colnames(M2) <- c("alpha", "beta", ":alpha+beta", ":a[0]", "=a[beta]")
rownames(M2) <- c("alpha", "beta", NA, "$a[0]", "$ a[beta]")
corrplot(M2)

R Corrplot软件包简介,绘制相关热图_第43张图片

将相关图与显着性检验相结合

res1 <- cor.mtest(mtcars, conf.level = .95)
res2 <- cor.mtest(mtcars, conf.level = .99)

## specialized the insignificant value according to the significant level
corrplot(M, p.mat = res1$p, sig.level = .2)

R Corrplot软件包简介,绘制相关热图_第44张图片

corrplot(M, p.mat = res1$p, sig.level = .05)

R Corrplot软件包简介,绘制相关热图_第45张图片

corrplot(M, p.mat = res1$p, sig.level = .01)

R Corrplot软件包简介,绘制相关热图_第46张图片

## leave blank on no significant coefficient
corrplot(M, p.mat = res1$p, insig = "blank")

R Corrplot软件包简介,绘制相关热图_第47张图片

## add p-values on no significant coefficient
corrplot(M, p.mat = res1$p, insig = "p-value")

R Corrplot软件包简介,绘制相关热图_第48张图片

## add all p-values
corrplot(M, p.mat = res1$p, insig = "p-value", sig.level = -1)

R Corrplot软件包简介,绘制相关热图_第49张图片

## add cross on no significant coefficient 
corrplot(M, p.mat = res1$p, order = "hclust", insig = "pch", addrect = 3)

R Corrplot软件包简介,绘制相关热图_第50张图片

可视化置信区间

corrplot(M, low = res1$lowCI, upp = res1$uppCI, order = "hclust",
         rect.col = "navy", plotC = "rect", cl.pos = "n")

R Corrplot软件包简介,绘制相关热图_第51张图片

corrplot(M, p.mat = res1$p, low = res1$lowCI, upp = res1$uppCI,
         order = "hclust", pch.col = "red", sig.level = 0.01,
         addrect = 3, rect.col = "navy", plotC = "rect", cl.pos = "n")

R Corrplot软件包简介,绘制相关热图_第52张图片

res1 <- cor.mtest(mtcars, conf.level = .95)

corrplot(M, p.mat = res1$p, insig = "label_sig",
         sig.level = c(.001, .01, .05), pch.cex = .9, pch.col = "white")

R Corrplot软件包简介,绘制相关热图_第53张图片

corrplot(M, p.mat = res1$p, method = "color",
         insig = "label_sig", pch.col = "white")

R Corrplot软件包简介,绘制相关热图_第54张图片

corrplot(M, p.mat = res1$p, method = "color", type = "upper",
         sig.level = c(.001, .01, .05), pch.cex = .9,
         insig = "label_sig", pch.col = "white", order = "AOE")

R Corrplot软件包简介,绘制相关热图_第55张图片

corrplot(M, p.mat = res1$p, insig = "label_sig", pch.col = "white",
         pch = "p<.05", pch.cex = .5, order = "AOE")

R Corrplot软件包简介,绘制相关热图_第56张图片

自定义相关图

# matrix of the p-value of the correlation
p.mat <- cor.mtest(mtcars)$p
head(p.mat[, 1:5])
##              [,1]         [,2]         [,3]         [,4]         [,5]
## [1,] 0.000000e+00 6.112687e-10 9.380327e-10 1.787835e-07 1.776240e-05
## [2,] 6.112687e-10 0.000000e+00 1.802838e-12 3.477861e-09 8.244636e-06
## [3,] 9.380327e-10 1.802838e-12 0.000000e+00 7.142679e-08 5.282022e-06
## [4,] 1.787835e-07 3.477861e-09 7.142679e-08 0.000000e+00 9.988772e-03
## [5,] 1.776240e-05 8.244636e-06 5.282022e-06 9.988772e-03 0.000000e+00
## [6,] 1.293959e-10 1.217567e-07 1.222320e-11 4.145827e-05 4.784260e-06
# Specialized the insignificant value according to the significant level
corrplot(M, type = "upper", order = "hclust", 
         p.mat = p.mat, sig.level = 0.01)

R Corrplot软件包简介,绘制相关热图_第57张图片

# Leave blank on no significant coefficient
corrplot(M, type = "upper", order = "hclust", 
         p.mat = p.mat, sig.level = 0.01, insig = "blank")

R Corrplot软件包简介,绘制相关热图_第58张图片在上图中,p值> 0.01的相关被认为是无关紧要的。在这种情况下,相关系数值留为空白或添加叉号。

col <- colorRampPalette(c("#BB4444", "#EE9988", "#FFFFFF", "#77AADD", "#4477AA"))
corrplot(M, method = "color", col = col(200),
         type = "upper", order = "hclust", number.cex = .7,
         addCoef.col = "black", # Add coefficient of correlation
         tl.col = "black", tl.srt = 90, # Text label color and rotation
         # Combine with significance
         p.mat = p.mat, sig.level = 0.01, insig = "blank", 
         # hide correlation coefficient on the principal diagonal
         diag = FALSE)

R Corrplot软件包简介,绘制相关热图_第59张图片

注意:其中一些情节摘自该博客。

探索大型功能矩阵

# generating large feature matrix (cols=features, rows=samples)
num_features <- 60 # how many features
num_samples <- 300 # how many samples
DATASET <- matrix(runif(num_features * num_samples),
               nrow = num_samples, ncol = num_features)

# setting some dummy names for the features e.g. f23
colnames(DATASET) <- paste0("f", 1:ncol(DATASET))

# let's make 30% of all features to be correlated with feature "f1"
num_feat_corr <- num_features * .3
idx_correlated_features <- as.integer(seq(from = 1,
                                          to = num_features,
                                          length.out = num_feat_corr))[-1]
for (i in idx_correlated_features) {
  DATASET[,i] <- DATASET[,1] + runif(num_samples) # adding some noise
}

corrplot(cor(DATASET), diag = FALSE, order = "FPC",
         tl.pos = "td", tl.cex = 0.5, method = "color", type = "upper")

R Corrplot软件包简介,绘制相关热图_第60张图片

H

你可能感兴趣的:(作图结果解释,R语言)