【发布时间】:2017-11-23 10:54:50
【问题描述】:
我有一个代码,它创建了几个项目之间的相关矩阵,我想以最好的方式将其可视化,我尝试使用 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")
【问题讨论】:
-
您是否加载了
corrplot包?或者你的问题到底是什么? -
@Alex 是的,我做到了
-
然后将其添加到您的问题中,以使您的代码可重现。并说明您的问题是什么以及您想要实现的目标。
-
完成了,我想用最简单的方式表示结果,你知道另一种有效的表示方式吗?
-
附带说明,由于您使用的参数数量(参见您的最后一个问题),您应该考虑使用
plotly中的heatmap,因为您可以通过滚动放大或获取名称剧情。plot_ly(x = rownames(Correlation_Tab), y = colnames(Correlation_Tab), z = Correlation_Tab, type = "heatmap")之类的内容应该可以帮助您进行分析。
标签: r data-visualization correlation r-corrplot