简体   繁体   中英

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. 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:

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)
)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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