当前位置:网站首页>Example analysis of corrplot related heat map beautification in R language

Example analysis of corrplot related heat map beautification in R language

2022-06-24 19:21:00 Yisu cloud

R Language corrplot Example analysis of heat map beautification

This article mainly introduces “R Language corrplot Example analysis of heat map beautification ” Knowledge about , Xiaobian shows you the operation process through practical cases , The operation method is simple and fast , Practical , Hope this article “R Language corrplot Example analysis of heat map beautification ” The article can help you solve problems .

Introduce

R corrplot package Provides a visual exploration tool on the correlation matrix , The tool supports automatic variable reordering , To help detect hidden patterns between variables .

corrplot Very easy to use , And in the visual method 、 Graphic layout 、 Color 、 legend 、 Text labels and other aspects provide a wealth of drawing options . It also provides p Values and confidence intervals , To help users determine the statistical significance of the correlation .

corrplot() There are about 50 Parameters , But the most common parameters are only a few . In most scenes , We can get a correlation matrix with only one line of code .

1. Load package

library(corrplot)

2. Load data

mtcars

3. mapping

corrplot(M, method = 'number')

R Language corrplot Example analysis of heat map beautification

#order Sorting method original( Default ), Eigenvector angle sorting AOE, The first principal component order FPC, Hierarchical clustering sorting hclust, In alphabetical order alphabetcorrplot(M, method = 'color', order = 'hclust')

R Language corrplot Example analysis of heat map beautification

# Shape default circle, In addition to that square,ellipse,number,pie,shade,colorcorrplot(M,method="circle")

R Language corrplot Example analysis of heat map beautification

corrplot(M,method="square")

R Language corrplot Example analysis of heat map beautification

corrplot(M,method="ellipse")

R Language corrplot Example analysis of heat map beautification

corrplot(M,method="pie")

R Language corrplot Example analysis of heat map beautification

#diag = FALSE, Do not show... In the middle 1 Lattice of corrplot(M,method="square",diag = FALSE)

R Language corrplot Example analysis of heat map beautification

#type Show only the lower part of the correlation , In addition, there are parameters full,uppercorrplot(M, method = 'square', order = 'FPC', type = 'lower', diag = FALSE)

R Language corrplot Example analysis of heat map beautification

corrplot(M, method = 'ellipse', order = 'FPC', type = 'upper', diag = FALSE)

R Language corrplot Example analysis of heat map beautification

# Mix numbers and graphs corrplot.mixed(M, order = 'AOE')

R Language corrplot Example analysis of heat map beautification

# Mix the upper pie chart , The lower shadow corrplot.mixed(M, lower = 'shade', upper = 'pie', order = 'hclust')

R Language corrplot Example analysis of heat map beautification

# Hierarchical clustering , Mark out 2 individual clustercorrplot(M, order = 'hclust', addrect = 2)

R Language corrplot Example analysis of heat map beautification

# Define the circled cluster, And the color and line of the loop line corrplot(M, method = 'square', diag = FALSE, order = 'hclust',         addrect = 3,          rect.col = 'blue',          rect.lwd = 3,          tl.pos = 'd')

R Language corrplot Example analysis of heat map beautification

4. Personalize the clustering method

install.packages("seriation")library(seriation)list_seriation_methods('matrix')list_seriation_methods('dist')data(Zoo)Z = cor(Zoo[, -c(15, 17)])dist2order = function(corr, method, ...) {  d_corr = as.dist(1 - corr)  s = seriate(d_corr, method = method, ...)  i = get_order(s)  return(i)}# Fast Optimal Leaf Ordering for Hierarchical Clusteringi = dist2order(Z, 'OLO')corrplot(Z[i, i], cl.pos = 'n')

R Language corrplot Example analysis of heat map beautification

# Quadratic Assignment Problemi = dist2order(Z, 'QAP_2SUM')corrplot(Z[i, i], cl.pos = 'n')

R Language corrplot Example analysis of heat map beautification

# Multidimensional Scalingi = dist2order(Z, 'MDS_nonmetric')corrplot(Z[i, i], cl.pos = 'n')

R Language corrplot Example analysis of heat map beautification

5. Add a personalized matrix

library(magrittr)# Method 1i = dist2order(Z, 'R2E')corrplot(Z[i, i], cl.pos = 'n') %>% corrRect(c(1, 9, 15))

R Language corrplot Example analysis of heat map beautification

# Method 2corrplot(Z, order = 'AOE') %>%  corrRect(name = c('tail', 'airborne', 'venomous', 'predator'))

R Language corrplot Example analysis of heat map beautification

# Method 3 Direct designation r = rbind(c('eggs', 'catsize', 'airborne', 'milk'),          c('catsize', 'eggs', 'milk', 'airborne'))corrplot(Z, order = 'hclust') %>% corrRect(namesMat = r)

R Language corrplot Example analysis of heat map beautification

6. color setting

COL1(sequential = c("Oranges", "Purples", "Reds", "Blues", "Greens",                     "Greys", "OrRd", "YlOrRd", "YlOrBr", "YlGn"), n = 200)COL2(diverging = c("RdBu", "BrBG", "PiYG", "PRGn", "PuOr", "RdYlBu"), n = 200)#cl.* Parameters are often used in color legends :cl.pos Location of color labels ('r'type='upper''full''b'type='lower''n'),cl.ratio Width recommendations for color legend 0.1~0.2#tl.* Parameters are often used in text legends :tl.pos Location for text labels ,tl.cex Text size ,tl.srt Rotation of text 
corrplot(M, order = 'AOE', col = COL2('RdBu', 10))

R Language corrplot Example analysis of heat map beautification

corrplot(M, order = 'AOE', addCoef.col = 'black', tl.pos = 'd',            cl.pos = 'r', col = COL2('PiYG'))

R Language corrplot Example analysis of heat map beautification

corrplot(M, method = 'square', order = 'AOE', addCoef.col = 'black', tl.pos = 'd',            cl.pos = 'r', col = COL2('BrBG'))

R Language corrplot Example analysis of heat map beautification

corrplot(M, order = 'AOE', cl.pos = 'b', tl.pos = 'd',col = COL2('PRGn'), diag = FALSE)

R Language corrplot Example analysis of heat map beautification

corrplot(M, type = 'lower', order = 'hclust', tl.col = 'black', cl.ratio = 0.2, tl.srt = 45, col = COL2('PuOr', 10))

R Language corrplot Example analysis of heat map beautification

corrplot(M, order = 'AOE', cl.pos = 'n', tl.pos = 'n',         col = c('white', 'black'), bg = 'gold2')

R Language corrplot Example analysis of heat map beautification

About “R Language corrplot Example analysis of heat map beautification ” That's all for , Thanks for reading . If you want to know more about the industry , You can pay attention to the Yisu cloud industry information channel , Xiaobian will update different knowledge points for you every day .

原网站

版权声明
本文为[Yisu cloud]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206241708084560.html