我有一个非常特殊的data.frame结构,其中包含细胞谱系数据,我需要将其转换为树状图结构的嵌套列表,以便使用dendextend程序包绘制生成的树和色叶。
我的数据具有以下结构:
Mother Daughter MotherLifeTime Var
A Aa 10 8
A Ab 10 8
Aa Aaa 11 2
Aa Aab 11 2
Ab Aba 12 4
Ab Abb 12 4
Aba NA 15 6
Abb NA 14 7
Aaa NA 16 7
Aab NA 15 9
每个母亲通常都有两个女儿,末级分支则没有。分支的长度由MotherLifetime指示。每个单元在变量Var中都有一些与之相关的度量,我想使用这些度量为分支着色或标记。
df <- data.frame(Mother=c("A","A","Aa","Aa","Ab","Ab","Aba","Abb","Aaa","Aab"),
Daugther=c("Aa","Ab","Aaa","Aab","Aba","Abb",NA,NA,NA,NA),
MotherLifeTime=c(10,10,11,11,12,12,15,14,16,15),
Var=c(8,8,2,2,4,4,6,7,7,9))
我想将此data.frame转换为树状图类对象-即具有属性的列表列表。结构类似于此:
library(dendextend)
dend <- c(1:4) %>% dist %>% hclust %>% as.dendrogram
labels(dend) <- c("Aaa","Aab","Aba","Abb")
dend %>% unclass %>% str
结构看起来有点像这样(这里的A根和分支没有正确的长度)
List of 2
$ :List of 2
..$ : int 1
.. ..- attr(*, "label")= chr "Aaa"
.. ..- attr(*, "members")= int 1
.. ..- attr(*, "height")= num 0
.. ..- attr(*, "leaf")= logi TRUE
..$ : int 2
.. ..- attr(*, "label")= chr "Aab"
.. ..- attr(*, "members")= int 1
.. ..- attr(*, "height")= num 0
.. ..- attr(*, "leaf")= logi TRUE
..- attr(*, "members")= int 2
..- attr(*, "midpoint")= num 0.5
..- attr(*, "height")= num 1
$ :List of 2
..$ : int 3
.. ..- attr(*, "label")= chr "Aba"
.. ..- attr(*, "members")= int 1
.. ..- attr(*, "height")= num 0
.. ..- attr(*, "leaf")= logi TRUE
..$ : int 4
.. ..- attr(*, "label")= chr "Abb"
.. ..- attr(*, "members")= int 1
.. ..- attr(*, "height")= num 0
.. ..- attr(*, "leaf")= logi TRUE
..- attr(*, "members")= int 2
..- attr(*, "midpoint")= num 0.5
..- attr(*, "height")= num 1
- attr(*, "members")= int 4
- attr(*, "midpoint")= num 1.5
- attr(*, "height")= num 3
谢谢您的建议。
拉德克