简体   繁体   English

R-相关矩阵-如何绘制此数据?

[英]R - Correlation Matrix - How to plot with this data?

I have the data like below: 我有如下数据:

A B 100
A C 100
B D 80
A D 50
B C 5
B D 60

basically three columns column 1 and Column 2 are character columns and column 3 is an integer which represents percentage of match between Col1 and Col2. 基本上,第一列和第二列是三列,是字符列,第三列是代表Col1和Col2匹配百分比的整数。 Now I would like to represent this data as a correlation matrix. 现在,我想将此数据表示为相关矩阵。 How can I do the same? 我该怎么做?

Here is an example on how to reconstruct the whole correlation matrix relatively simply using igraph: 这是一个有关如何使用igraph相对简单地重建整个相关矩阵的示例:

library(igraph)
library(corrplot)

g <- graph.data.frame(df, directed = FALSE)
mat <- get.adjacency(g, attr = "V3", sparse = FALSE)
mat
#output
    A   B   C  D
A   0 100 100 50
B 100   0   5 60
C 100   5   0  0
D  50  60   0  0
 diag(mat) <- 100
 mat <- mat/100

 corrplot.mixed(mat, upper =  "shade", "number")

在此处输入图片说明

There is already one answer, but here is an idea with only base functions 已经有一个答案,但这是一个仅具有基本功能的想法

heatmap(
  xtabs(
    data = aggregate(V3 ~ V1 + V2, data, 'mean'), 
    formula = V3 ~ V1 + V2
  ), 
  Rowv = NA, 
  Colv = NA, 
  revC = T)
)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM