0

我有 100 多个观察点(点),在那里我查看了特定项目(例如,A、B、C..等)。数据的基本格式如下:

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))
print(data1)

现在我想开发一个项目来使用包adjacency matrix在 R 中查看他们的网络。circlize建议将不胜感激。

4

1 回答 1

0

我会给你一个例子,如何使用R 中的包绘制chord图表:circlize

生成随机数据:

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
于 2019-10-27T07:58:46.163 回答