用 R 可视化复杂的相关矩阵

Visualizing Complex correlation matrix with R

我有一个代码可以在几个项目之间创建一个相关矩阵,我想以最好的方式将其可视化,我尝试使用 corrplot 函数,但我遇到了问题,无法理解来自它 , 这是我的代码和我的数据样本:

library(corrplot)
Orders<- structure(list(WHWorkOrderHeaderId = c(137413L, 137413L, 137413L, 
137413L, 137413L, 137413L, 137413L, 137413L, 137413L, 137413L, 
137413L, 137413L, 137429L, 137429L, 137429L, 137429L, 137429L, 
137429L, 137429L, 137429L, 137429L, 137260L, 137260L, 137260L, 
137260L, 137260L, 137260L, 137260L, 137260L, 137260L, 137260L, 
136729L, 136729L, 136729L, 136729L, 136729L, 136729L, 136729L, 
136729L, 136729L, 136729L, 136729L, 136729L, 136729L, 137902L, 
137902L, 137902L, 137902L, 137902L, 137902L, 137902L, 137974L, 
137974L, 138837L, 138837L, 138837L, 138837L, 138837L, 138837L, 
139424L, 139424L, 139424L, 139424L, 139424L, 139424L, 139424L, 
139424L, 139424L, 139642L, 139642L, 139642L, 139642L, 139642L, 
139642L, 139642L, 140676L, 140676L, 140676L, 140676L, 140676L, 
140676L, 140938L, 140938L, 140938L, 140938L, 140938L, 140938L, 
140938L, 140938L, 140938L, 140938L, 141302L, 141302L, 141302L, 
141302L, 141302L, 141302L, 138297L, 138297L, 138297L), OtherLangDescription = structure(c(17L, 
16L, 34L, 19L, 25L, 32L, 18L, 35L, 15L, 27L, 13L, 22L, 16L, 26L, 
5L, 20L, 19L, 14L, 29L, 35L, 27L, 17L, 16L, 30L, 26L, 5L, 19L, 
14L, 31L, 29L, 27L, 23L, 24L, 16L, 30L, 8L, 19L, 14L, 32L, 9L, 
31L, 35L, 27L, 21L, 24L, 30L, 26L, 5L, 14L, 32L, 31L, 26L, 5L, 
11L, 24L, 31L, 15L, 27L, 13L, 11L, 17L, 24L, 10L, 19L, 32L, 6L, 
31L, 33L, 23L, 11L, 17L, 4L, 10L, 19L, 31L, 23L, 11L, 4L, 10L, 
19L, 31L, 11L, 17L, 16L, 14L, 25L, 12L, 31L, 7L, 1L, 2L, 23L, 
3L, 35L, 15L, 27L, 28L, 17L, 24L, 16L), .Label = c(" Green Beans", 
"Baladi Cabbage", "Baladi Garlic", "Banati Grape", "Barshomi Figs", 
"Black Eggplant", "Cantaloupe", "Capsicum", "Carrot", "Chili Pepper", 
"Classic Eggplant", "Cooking Potato", "Coriander", "Cucumber", 
"Dill", "Flame Grape", "frying Potato", "Golden Onion", "Green pepper", 
"Hot Pepper", "Local Celery ", "Local Eggplant", "Local Lemon", 
"Local Pear", "Molokhia", "Momtaza Owais Mango", "Parsley", "Red Globe Grape", 
"Red Onion", "Superior Grape", "Tomato", "White Eggplant ", "Zaghlol Dates", 
"Zebdaya Mango", "Zucchini"), class = "factor")), .Names = c("WHWorkOrderHeaderId", 
"OtherLangDescription"), row.names = c(NA, -100L), class = "data.frame")

Orders$OtherLangDescription <- as.factor(Orders$OtherLangDescription)
orderList <- unique(Orders$OtherLangDescription) 
ListId <- lapply(orderList, function(x) subset(Orders, OtherLangDescription == x)$WHWorkOrderHeaderId) 
Initial_Tab <- lapply(ListId, function(x) subset(Orders, WHWorkOrderHeaderId %in% x)$OtherLangDescription) 
Correlation_Tab <- mapply(function(Product, ID) table(Product)/length(ID),
                          Initial_Tab, ListId) 
colnames(Correlation_Tab) <- orderList
cor_per<- round(Correlation_Tab*100,2)
#View(cor_per)
#plot cor matrix
corrplot(Correlation_Tab, tl.pos="lt", type="upper",        
         tl.col="black", tl.cex=0.6, tl.srt=45, is.corr = FALSE,
         addCoef.col="black", addCoefasPercent = TRUE,
         sig.level=0.50, insig = "blank")

您可以尝试使用网络分析。这是来自“http://www.r-graph-gallery.com”的示例。

这是示例代码:

# library
library(igraph)

# data
head(mtcars)

# Make a correlation matrix:
mat=cor(t(mtcars[,c(1,3:6)]))
# Keep only high correlations
mat[mat<0.995]=0

# Make an Igraph object from this matrix:
network=graph_from_adjacency_matrix( mat, weighted=T, mode="undirected", diag=F)

plot(network)

这将是结果:

您可以根据自己的情况调整此示例,以便更好地了解相关性。