Chr*_*s P 8 r hierarchical-data networkd3
这是我的第一个问题; 所以,请温柔.
我有一些数据形式:
library('networkD3')
Relationships<- data.frame(Parent=c("earth","earth","forest","forest","ocean","ocean","ocean","ocean"),
Child=c("ocean","forest","tree","sasquatch","fish","seaweed","mantis shrimp","sea monster"))
> Relationships
Parent Child
1 earth ocean
2 earth forest
3 forest tree
4 forest sasquatch
5 ocean fish
6 ocean seaweed
7 ocean mantis shrimp
8 ocean sea monster
Run Code Online (Sandbox Code Playgroud)
本质上,这是一个可用于制作网络地图的边缘列表:
net <- graph_from_data_frame(d = Relationships,
directed = T)
plot(net)
Run Code Online (Sandbox Code Playgroud)
我想将它转换为可以在diagonalNetwork下面的函数中使用的形式.
Hierarchical_list <- list(name = "earth",
children = list(list(name = "ocean",
children = list(list(name = "mantis shrimp"),
list(name = "fish"),
list(name = "sea monster"),
list(name = "seaweed")
)),
list(name = "forest",
children = list(list(name = "sasquatch"),
list(name = "tree")
))
))
diagonalNetwork(Hierarchical_list)
Run Code Online (Sandbox Code Playgroud)
像这样:
当我尝试使用此循环生成列表时:
List_attempt <- list()
levels<- levels(factor(Relationships$Parent))
for(n in 1:length(levels)){
Children <- subset(Relationships, Relationships$Parent == levels[n], select = Child)
for(c in 1:length(Children)){
sublist <- as.list(Children)
List_attempt <- list(List_attempt, name = levels[n],children = sublist)
}
}
diagonalNetwork(List_attempt)
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
Error in FUN(X[[i]], ...) :
'options' must be a fully named list, or have no names (NULL)
Run Code Online (Sandbox Code Playgroud)
1)是否有更好的方法来创建列表diagonalNetwork?
2)失败; 如何修改我的循环以找出正确结构的列表?
3)我应该使用其他功能/包吗?
谢谢你能给予的任何帮助,我已经在这墙上打了一会儿.关于更好地提出有关SO问题的方法的反馈也将受到欢迎.
澄清:
此处还可以找到类似的问题,将数据帧转换为treeNetwork兼容列表.但是它依赖于一个数据结构,其中根始终位于第一列中,并且其子项位于后续列中,而不是此问题中的边列表,这通常用于igraph中.
Chr*_*lur 11
您可以使用data.tree包,该包可以从开箱即用的分层数据进行多次转换:
library('networkD3')
Relationships<- data.frame(Parent=c("earth","earth","forest","forest","ocean","ocean","ocean","ocean"),
Child=c("ocean","forest","tree","sasquatch","fish","seaweed","mantis shrimp","sea monster"))
library('data.tree')
tree <- FromDataFrameNetwork(Relationships)
tree
lol <- ToListExplicit(tree, unname = TRUE)
diagonalNetwork(lol)
Run Code Online (Sandbox Code Playgroud)
感谢您指出错误@Symbolix
受到 @MrFlick 评论的启发,建议从 root 开始并让子进程递归地创建列表元素:) ...肯定可以进一步提高针对意外数据输入的鲁棒性
library(igraph)
library('networkD3')
Relationships<- data.frame(Parent=c("earth","earth","forest","forest","ocean","ocean","ocean","ocean"),
Child=c("ocean","forest","tree","sasquatch","fish","seaweed","mantis shrimp","sea monster"))
net <- graph_from_data_frame(d=Relationships, directed=T)
plot(net)
#net and Relationships as declared in question
#get root
root <- setdiff(Relationships$Parent, Relationships$Child)
#traverse next layer and then recurve
as.list.igraph <- function(thisNode) {
nm <- vertex_attr(net, "name", thisNode)
childNodes <- V(net)[which(shortest.paths(net, thisNode, mode="out") == 1)]
if (length(childNodes)==0) return(list(name=nm))
list(name=nm, children=unname(lapply(childNodes, as.list.igraph)))
}
#plot D3 network
diagonalNetwork(as.list.igraph(V(net)[root]))
Run Code Online (Sandbox Code Playgroud)
顺便说一句,如果我没记错的话,igraph 中还有一个layout.reingold.tilford 选项