将数据帧转换为矩阵并创建和弦图

问题描述 投票:0回答:1

我正在尝试创建一个和弦图,如下图所示。

enter image description here

在这里,您可以看到哪些 TRAV 值(例如 TRAV21、TRAV1-2 等)与 TRBV 值(例如 TRBV6-4、TRBV6-1 ec)匹配,以及配对发生的频率。

使用提供的文档(https://jokergoo.github.io/circlize_book/book/the-chorddiagram-function.html),我必须首先以这种格式创建数据矩阵:

    E1 E2 E3 E4 E5 E6
S1  4 14 13 17  5  2
S2  7  1  6  8 12 15
S3  9 10  3 16 11 18

然后,将矩阵转换为以下格式的数据框:

   from to value
 1    S1 E1     4
 2    S2 E1     7
 3    S3 E1     9
 4    S1 E2    14
 5    S2 E2     1
 6    S3 E2    10
 7    S1 E3    13
 8    S2 E3     6
 9    S3 E3     3
 10   S1 E4    17

到目前为止,我有以下代码:

df<- structure(list(TRAV = c("TRAV1-2", "TRAV1-2", "TRBV20-1", 
"TRAV12-1", "TRAV1-2", "TRAV1-2", "TRAV1-2", "TRAV8-2", "TRAV1-2", 
"TRBV20-1", "TRAV1-2", "TRAV1-2", "TRAV12-2", "TRAV12-3", "TRAV12-2", 
"TRAV1-2", "TRAV1-2", "TRAV1-2", "TRAV19", "TRAV1-2", "TRAV1-2", 
"TRAV19", "TRAV16", "TRAV27", "TRAV5", "TRAV1-2", "TRAV22", "TRAV1-2", 
"TRAV27", "TRAV26-2", "TRAV1-2", "TRAV1-2", "TRAV1-2", "TRAV1-2", 
"TRAV1-2", "TRAV41", "TRAV1-2", "TRAV1-2", "TRAV1-2", "TRAV1-2", 
"TRAV1-2", "TRAV35", "TRAV1-2", "TRAV1-2", "TRBV19", "TRAV1-2", 
"TRAV12-2", "TRAV1-2", "TRAV1-2", "TRAV16", "TRAV17", "TRAV35", 
"TRAV1-2", "TRBV4-1", "TRAV1-2", "TRBV5-5", "TRAV1-2", "TRBV6-4", 
"TRAV12-2", "TRAV1-2", "TRAV1-2", "TRAV1-2", "TRAV1-2", "TRAV22", 
"TRAV1-2", "TRAV1-2", "TRAV8-4", "TRAV1-2", "TRAV1-2", "TRAV8-3", 
"TRBV5-1", "TRAV12-2", "TRAV1-2", "TRBV6-2", "TRAV19", "TRAV1-2", 
"TRAV1-2", "TRAV1-2", "TRAV1-2", "TRAV1-2", "TRAV1-2", "TRAV21", 
"TRBV15", "TRAV24", "TRBV6-1", "TRAV1-2", "TRAV1-2", "TRAV1-2", 
"TRAV1-2", "TRAV12-2", "TRAV12-2", "TRAV29/DV5", "TRAV8-2", "TRAV12-2", 
"TRAV1-2", "TRAV1-2", "TRAV12-2", "TRAV1-2", "TRBV6-1", "TRAV1-2"
), TRBV = c("TRBV6-4", "TRBV6-2", NA, "TRBV4-3", "TRBV6-4", 
"TRBV6-2", "TRAV5", "TRBV6-2", "TRBV6-4", NA, "TRBV3-1", "TRBV6-2", 
"TRBV6-6", "TRBV10-2", "TRBV6-2", "TRBV6-4", "TRBV6-4", "TRBV6-4", 
"TRBV6-6", "TRBV20-1", "TRBV4-2", "TRBV9", "TRBV6-2", "TRBV5-5", 
"TRBV4-3", NA, "TRBV11-3", "TRBV4-2", "TRBV5-5", "TRBV9", "TRBV6-4", 
"TRBV6-4", "TRBV4-2", "TRBV4-3", "TRBV6-1", "TRBV12-4", "TRBV6-4", 
"TRBV6-4", "TRBV6-4", "TRBV6-4", "TRBV6-4", "TRBV12-4", "TRBV6-4", 
"TRBV6-4", NA, "TRBV6-4", "TRBV6-2", "TRBV19", "TRBV28", "TRBV20-1", 
"TRBV5-5", "TRAV41", "TRBV6-4", NA, "TRBV6-4", NA, "TRBV6-4", 
NA, "TRAV5", "TRAV23/DV6", "TRBV28", "TRBV6-4", "TRBV4-2", "TRAV35", 
"TRBV6-4", "TRBV6-4", "TRBV6-2", NA, NA, "TRBV3-1", NA, "TRBV3-1", 
"TRBV6-4", NA, "TRBV9", "TRBV4-3", "TRBV20-1", "TRAV6", "TRBV6-4", 
"TRBV15", "TRBV20-1", "TRAV30", NA, "TRBV19", NA, "TRBV6-4", 
"TRBV25-1", "TRBV6-2", "TRBV6-1", "TRBV3-1", "TRBV6-6", "TRBV11-2", 
"TRBV27", "TRBV6-6", "TRBV6-4", "TRBV4-2", "TRBV6-6", "TRBV6-1", 
NA, "TRBV6-4")), row.names = c(NA, 100L), class = "data.frame")

df<- data.matrix(df)
xtabs( ~ TRAV+TRBV, data=df)

但是,xtabs 的输出删除了不同级别的 TRAV 和 TRBV 列,而仅提供数字。如何创建一个矩阵,然后按照文档中所述创建数据框,以便我能够创建弦图?

提前非常感谢!

r dataframe matrix rstudio
1个回答
0
投票

问题不在于

xtabs
,问题在于您将数据帧转换为
data.matrix
。相反,您可以在数据帧上应用
xtabs
并将输出传递给
chordDiagram

library(circlize)

chordDiagram(xtabs(~ TRAV + TRBV, data = df))

或者使用例如

aggregate
获取邻接列表作为数据帧并将其传递给
chordDiagram
:

result <- aggregate(
  rep(1, nrow(df)),
  by = list(TRBV = df$TRBV, TRAV = df$TRAV), FUN = length
)
names(result) <- c("from", "to", "value")

chordDiagram(result)

© www.soinside.com 2019 - 2024. All rights reserved.