我们如何在包 networkD3 的 forceNetwork 函数中找到组?

how can we find group in forceNetwork function of package networkD3?

我正在尝试在 using 包中使用函数 forceNetwork 创建 D3 网络,即

forceNetwork(Links = MisLinks, Nodes = MisNodes,
             Source = "source", Target = "target",
             Value = "value", NodeID = "name",
             Group = "group", opacity = 0.8)

我想知道,他们是根据什么来组群的

R中的示例数据给出了创建网络的两个表,

1)   MisLink: which gives information about the links
header:   |source |target |value
     1    |  1    |  0    | 1
     2    |  2    |  0    | 8
     3    |  3    |  0    |10
     4    |  3    |  2    | 6
     5    |  4    |  0    | 1

2) MisNode: which consists of information regarding nodes.
header:  |              name| group| size
       1 |          Myriel  |   1  | 15
       2 |        Napoleon  |   1  | 20
       3 | Mlle.Baptistine  |   1  | 23
       4 |    Mme.Magloire  |   1  | 30
       5 |    CountessdeLo  |   1  | 11

我的节点数据没有任何组列。如何创建 GROUP 列?

我有两个表如下:

Link Table:

Source |    Target     |  EdgeWt
5-HT1A |    depression |    4
5-HT2A |    depression |    5
5-HT2C |    Anxiety    |    3
5-HT2C |    depression |    4

Node Table:

Id     | NodeType
5-HT1A | GeneSymbol
5-HT2A | GeneSymbol
5-HT2C | GeneSymbol
ACNE   | Disease
ACTA   | GeneSymbol
ACTH   | GeneSymbol

我可以将 Source 和 Target 更改为数字,但最后我想要基于 NodeType 的节点颜色。

如果您的节点没有分组,那么您可以简单地向您的节点数据添加一个组列,其中每个节点的值都相同,例如...

myLinks$group <- 1

如果您的数据已分组,则将该数据添加到您的节点数据框中并在 Group 参数中引用该列名称。


给定您添加的数据,这是如何实现的 (Group = "NodeType")...

links <-
  data.frame(
    stringsAsFactors = FALSE,
    Source = c("5-HT1A", "5-HT2A", "5-HT2C", "5-HT2C"),
    Target = c("depression", "depression", "Anxiety", "depression"),
    EdgeWt = c(4, 5, 3, 4)
  )

nodes <- data.frame(
  stringsAsFactors = FALSE,
  Id = c("5-HT1A", "5-HT2A", "5-HT2C", "ACNE", "ACTA", "ACTH"),
  NodeType = c(
    "GeneSymbol",
    "GeneSymbol",
    "GeneSymbol",
    "Disease",
    "GeneSymbol",
    "GeneSymbol"
  )
)

nodes <- rbind(nodes, c("depression", "Disease"), c("Anxiety", "Disease"))

links$Source <- match(links$Source, nodes$Id) - 1
links$Target <- match(links$Target, nodes$Id) - 1

library(networkD3)

forceNetwork(Links = links, Nodes = nodes,
             Source = "Source", Target = "Target",
             Value = "EdgeWt", NodeID = "Id",
             Group = "NodeType", opacityNoHover = 1, 
             opacity = 1, fontSize = 10)