Я дам вам пример того, как построить chord
диаграмму, используя пакет circlize
в R:
Генерировать случайные данные:
set.seed(1000)
mat = matrix(sample(18, 18), 3, 6)
rownames(mat) = paste0("S", 1:3)
colnames(mat) = paste0("E", 1:6)
print(mat)
Вывод
E1 E2 E3 E4 E5 E6
S1 6 11 9 10 16 17
S2 13 8 7 12 5 14
S3 2 1 3 18 4 15
Преобразование в матрицу adjacency
:
df = data.frame(from = rep(rownames(mat), times = ncol(mat)),
to = rep(colnames(mat), each = nrow(mat)),
value = as.vector(mat),
stringsAsFactors = FALSE)
print(df)
Выход
from to value
1 S1 E1 6
2 S2 E1 13
3 S3 E1 2
4 S1 E2 11
5 S2 E2 8
6 S3 E2 1
7 S1 E3 9
8 S2 E3 7
9 S3 E3 3
10 S1 E4 10
11 S2 E4 12
12 S3 E4 18
13 S1 E5 16
14 S2 E5 5
15 S3 E5 4
16 S1 E6 17
17 S2 E6 14
18 S3 E6 15
Диаграмма chord
диаграмма:
chordDiagram(df, transparency = 0.5)
Adjacency matrix
по вашим данным:
код:
spot1<-c("A","B","B","B","C")
spot2<-c("C","NA","D","D","D")
spot3<-c("D","N","E","F","G")
spot4<-c("H","I","I","NA","NA")
spot5<-c("B","I","NA","NA","NA")
data1<-as.data.frame(cbind(spot1,spot2,spot3,spot4,spot5))
mat<-data1
df = data.frame(from = rep(rownames(mat), times = ncol(mat)),
to = rep(colnames(mat), each = nrow(mat)),
value = as.vector(mat),
stringsAsFactors = FALSE)
print(df)
выход
from to value.spot1 value.spot2 value.spot3 value.spot4 value.spot5
1 1 spot1 A C D H B
2 2 spot1 B NA N I I
3 3 spot1 B D E I NA
4 4 spot1 B D F NA NA
5 5 spot1 C D G NA NA
6 1 spot2 A C D H B
7 2 spot2 B NA N I I
8 3 spot2 B D E I NA
9 4 spot2 B D F NA NA
10 5 spot2 C D G NA NA
11 1 spot3 A C D H B
12 2 spot3 B NA N I I
13 3 spot3 B D E I NA
14 4 spot3 B D F NA NA
15 5 spot3 C D G NA NA
16 1 spot4 A C D H B
17 2 spot4 B NA N I I
18 3 spot4 B D E I NA
19 4 spot4 B D F NA NA
20 5 spot4 C D G NA NA
21 1 spot5 A C D H B
22 2 spot5 B NA N I I
23 3 spot5 B D E I NA
24 4 spot5 B D F NA NA
25 5 spot5 C D G NA NA