这可能看起来相对简单,但我正在尝试将数据从数据帧更改为矩阵,因为我正在使用新的 D3 chorddiag 包。在我使用 circlize 并依赖于使用数据框功能之前。Chorddiag 似乎完全依赖于矩阵,所以我想知道如何跳过这些类型。下面是示例:
library(circlize)
library(chorddiag)
from = c("A", "A", "A", "A", "B", "B", "C", "C", "D")
to = c("B", "C", "D", "J", "E", "F", "G", "H", "I")
value = c(5,8,2,5,5,6,7,8,11)
food = data.frame(from, to, value)
food
chordDiagram(food)
#now in chordiag how to transform into matrix like m?
m <- matrix(c(11975, 5871, 8916, 2868,
1951, 10048, 2060, 6171,
8010, 16145, 8090, 8045,
1013, 990, 940, 6907),
byrow = TRUE,
nrow = 4, ncol = 4)
groupnames <- c("black", "blonde", "brown", "red")
row.names(m) <- groupnames
colnames(m) <- groupnames
chorddiag(m)
如何将食物更改为类似于 m?我想可能像下面这样,但我不知道如何添加名称。感谢帮助
matrix(data = food$value, nrow = length(food$from), ncol = length(food$to), byrow = TRUE
#,dimnames =
)