【问题标题】:Turning relationship data into hierarchical list in R将关系数据转换为R中的分层列表
【发布时间】:2016-12-21 07:28:13
【问题描述】:

这是我的第一个问题;所以,请温柔一点。

我有一些数据格式为:

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

本质上这是一个可用于制作网络地图的边列表:

net <- graph_from_data_frame(d = Relationships,
                             directed = T)
plot(net)

我想将其转换为可以在下面的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)

像这样:

当我尝试使用此循环生成列表时:

    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)

我收到此错误:

Error in FUN(X[[i]], ...) : 
  'options' must be a fully named list, or have no names (NULL)

1) 有没有更好的方法来为diagonalNetwork 创建列表?

2) 做不到这一点;如何修改循环以退出正确结构的列表?

3) 我应该使用其他完整的功能/包吗?

感谢您提供的任何帮助,我已经用头撞墙有一段时间了。我们也欢迎就 SO 问题提出更好的方法提供反馈。

澄清:

在这里可以找到类似的问题,Convert a data frame to a treeNetwork compatible list。然而,它依赖于一个数据结构,其中根始终位于第一列,其子节点位于后续列中,而不是 igraph 中常用的这个问题中的边列表。

【问题讨论】:

  • @Procrastinatus Maximus,我对其进行了编辑以澄清两个问题之间的区别。那个具有相同的最终目标但不转换关系数据,它仅在根位于第一列并且整个父子结构在后续列中时才有效,而在边列表中则不是这种情况。
  • 好的,我重新打开了这个问题

标签: r hierarchical-data networkd3


【解决方案1】:

您可以使用 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)

【讨论】:

    【解决方案2】:

    感谢@Symbolix 指出错误

    受到@MrFlick 评论的启发,建议从根开始并让子级递归地创建列表元素 :) ... 当然可以进一步改进,以提高对意外数据输入的鲁棒性

    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]))
    

    顺便说一句,如果我没记错的话,igraph 中还有一个 layout.reingold.tilford 选项

    【讨论】:

    • 这些将不同的结构返回到问题中发布的结构
    • @Symbolix 很好,结构不同,但仅在排序上。对于我的目标(图表),这不是问题。我将研究 layout.reingold.tilford 选项。
    • 这将有很大帮助,因为它允许我使用 networkd3 包的一些功能,在我现在制作 igraph 图的一些事情上,无需重复工作。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-16
    • 2015-04-14
    • 1970-01-01
    • 1970-01-01
    • 2021-09-25
    相关资源
    最近更新 更多