forceNetwork不显示任何边缘 [英] forceNetwork not displaying any edges

查看:136
本文介绍了forceNetwork不显示任何边缘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出对forceNetwork()的节点和链接数据帧的精确要求,因为我的网络显示的是彩色可移动点,而没有任何边缘将它们连接起来,我也不知道为什么.据我所知,要求是:

I am trying to figure out the EXACT requirements for the node and link dataframes in forceNetwork() because my network is displaying colored movable points without any edges connecting them and I dont know why. As far as I can tell the requirements are:

节点数据框必须具有:

  • 与链接源名称完全匹配的nodeID列(如果节点以与链接相同的顺序出现).如果它们的顺序相同,则可以根据我对文档的阅读来删除此参数.
  • 一个组变量,它指定节点的组.不同的组使用不同的颜色来区分它们.

    nodes dataframe must have:

  • a nodeID column (if nodes arent in the same order as the links) that exactly matches the link source name. If they are in the same order this parameter can be dropped according to my reading of the documentation.
  • a group variable which specifies the group of the node. Different groups are different colors to differentiate them.

    链接数据框必须具有:

  • 源列(将与目标连接的节点列表)
  • 目标列
  • 一个值列,它指定要在节点之间绘制的线的粗细

    links dataframe must have:

  • a source column (list of nodes that will connect with targets)
  • a target column
  • a value column which specifies the weight of the line to draw between the nodes

    > head(links)
      source target value
    1  11170      7     1
    2   2840      2     1
    3  32595      2     1
    4  45410      8     1
    5  52720     12     1
    6  61720      6     1
    > head(nodes)
      nodeID group
    1  11170     2
    2   2840     1
    3  32595     2
    4  45410     3
    5  52720     1
    6  61720     2
    > head(E(g))
    Edge sequence:
    
    [1] 7     -- 11170
    [2] 2     -- 2840 
    [3] 2     -- 32595
    [4] 8     -- 45410
    [5] 12    -- 52720
    [6] 6     -- 61720
    > head(V(g))
    Vertex sequence:
    [1] "11170" "2840"  "32595" "45410" "52720" "61720"
    
    > typeof(nodes$nodeID[1])
    [1] "integer"
    > typeof(links$source[1])
    [1] "integer"
    
    > dim(links)
    [1] 121   3
    > dim(nodes)
    [1] 135   2
    
    > forceNetwork(Links = links, Nodes = nodes,Source = "source", Target = "target", NodeID = "nodeID",Group = "group", opacity = 0.8, colourScale = "d3.scale.category10()")
    

    推荐答案

    Links数据框中的source target向量必须为数字,并且其值引用它们表示的Nodes数据框中的节点的索引(零索引,与R不同,因为它由JavaScript代码使用).

    The source and target vectors in the Links data frame must be numeric, and their values refer to the index of the node in the Nodes data frame which they represent (zero-indexed, unlike R, because it's used by the JavaScript code).

    Nodes数据框的nodeID矢量给出每个节点的名称(字符或数字),当您将鼠标悬停在结果可视化中的某个节点上时会显示该名称.

    The nodeID vector of the Nodes data frame gives the name (character or numeric) of each node, which is displayed when you hover over a node in the resulting visualization.

    考虑到您的示例数据,我怀疑您在links$source中拥有的数据将成为节点的ID,而在links$target中具有的数据就是其他东西.虽然它们都是数字矢量,但它们的值并不代表它们在Nodes数据框中引用的节点的索引.

    Given your example data, I suspect that the data you have in links$source is meant to be the ID of the nodes, and the data you have in links$target is something else. While they're both numeric vectors, the values do not represent the index of the node they refer to in the Nodes data frame.

    如果在links$sourcelinks$target向量中具有节点名称/ID,而不是在Nodes数据框中的节点索引,则可以像这样修复它...

    If you have node names/IDs in your links$source and links$target vectors instead of the index of the nodes in your Nodes data frame, you can fix it like so...

    links <- read.table(header = T, text = "
    source target value
    11170      7     1
    2840       2     1
    32595      2     1
    45410      8     1
    52720     12     1
    61720      6     1
    ")
    
    nodes <- read.table(header = T, text = "
    nodeID group
    11170     2
    2840      1
    32595     2
    45410     3
    52720     1
    61720     2
    ")
    
    # build a new Nodes data frame that includes every 
    # unique node found in links$source and links$target
    nodes_complete <- data.frame(nodeID = unique(c(links$source, links$target)))
    
    # add groups already identified in your original Nodes data frame
    nodes_complete$group <- nodes$group[match(nodes_complete$nodeID, nodes$nodeID)]
    
    # convert your links$source and links$target nodes to their index
    # in the new Nodes data frame (zero-indexed)
    links$source <- match(links$source, nodes_complete$nodeID) - 1
    links$target <- match(links$target, nodes_complete$nodeID) - 1
    
    # now the forceNetwork function will run as expected
    library(networkD3)
    forceNetwork(Links = links, Nodes = nodes_complete, Source = "source", 
                 Target = "target", NodeID = "nodeID", Group = "group", 
                 opacity = 0.8, 
                 colourScale = "d3.scaleOrdinal(d3.schemeCategory10);")
    

    这篇关于forceNetwork不显示任何边缘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆