networkD3 没有绘制预期的交互式网络

networkD3 doesn't draw the expected interactive network

我有以下数据:

links <- read.table(header = T, as.is = T, text = '
from  to      type
p13  p1 hyperlink
p13  p2 hyperlink
p13  p3 hyperlink
p13  p4 hyperlink
p13  p5 hyperlink
p13 p11 hyperlink
p12 p10 hyperlink
p12  p8 hyperlink
p12  p9 hyperlink
p14  p6 hyperlink
p14  p7 hyperlink
')

nodes <- read.table(header = T, as.is = T, text = '
id                                               name node.type
p1                                                 Pi   Protein
p2                                                PPi   Protein
p3                                                SAM   Protein
p4                                              L-Met   Protein
p5                                                H2O   Protein
p6                                                SAH   Protein
p7                                            Ade-Rib   Protein
p8                                              LHCYS   Protein
p9                   tetrahydropteroyltri-L-glutamate   Protein
p10 5-methyltetrahydropteroyltriglutamate-homocysteine  Protein
p11                                                ATP  Protein
p12                                          reaction1  Reaction
p13                                          reaction2  Reaction
p14                                          reaction3  Reaction
')

我正在使用 包,我有以下代码:

library("networkD3")

links.d3 <- data.frame(from=as.numeric(factor(links$from))-1,
                   to=as.numeric(factor(links$to))-1 )
nodes.d3 <- cbind(idn=factor(nodes$name, levels=nodes$name), nodes)

forceNetwork(Links = links.d3,
         Nodes = nodes.d3,
         Source="from",
         Target="to",
         NodeID = "name",
         Group = "node.type",
         linkWidth = 1,
         linkColour = "#afafaf",
         fontSize=12,
         zoom=T,
         colourScale = JS(ColourScale),
         legend=T,
         opacity = 0.8,
         charge=-300,
         arrows = FALSE,
         bounded = TRUE,
         opacityNoHover = 2,
         width = NULL,
         height = NULL,
         clickAction = myClick)

p12、p13 和 p14 是反应。预期结果是除了H2O、L-Met和LHCYS之外,每个蛋白质都应该连接到一个反应,这三个应该连接到两个单独的反应。

这是我得到的:

绘制我所期待的示例:

有人可以解释原因并实现我想要的结果吗?

首先,您的链接数据中缺少定义您要显示的内容的链接:

  • p12 -> p4(反应 1 -> L-Met)
  • p14 -> p5(反应 3 -> H2O)
  • p14 -> p8(反应 3 -> LHCYS)

其次,我不明白你试图创建 links.d3 data.frame 的目的,但是 fromto variables/columns 应该是 nodes data.frame 中节点的索引,您可以如下创建...

links <- read.table(header = T, as.is = T, text = '
from  to  type
p13   p1  hyperlink
p13   p2  hyperlink
p13   p3  hyperlink
p13   p4  hyperlink
p13   p5  hyperlink
p13   p11 hyperlink
p12   p10 hyperlink
p12   p8  hyperlink
p12   p9  hyperlink
p12   p4  hyperlink
p14   p6  hyperlink
p14   p7  hyperlink
p14   p5  hyperlink
p14   p8  hyperlink
')

nodes <- read.table(header = T, as.is = T, text = '
id                                               name node.type
p1                                                 Pi   Protein
p2                                                PPi   Protein
p3                                                SAM   Protein
p4                                              L-Met   Protein
p5                                                H2O   Protein
p6                                                SAH   Protein
p7                                            Ade-Rib   Protein
p8                                              LHCYS   Protein
p9                   tetrahydropteroyltri-L-glutamate   Protein
p10 5-methyltetrahydropteroyltriglutamate-homocysteine  Protein
p11                                                ATP  Protein
p12                                          reaction1  Reaction
p13                                          reaction2  Reaction
p14                                          reaction3  Reaction
')

links.d3 <- data.frame(from = match(links$from, nodes$id) - 1,
                       to = match(links$to, nodes$id) - 1,
                       value = 1)

library("networkD3")
forceNetwork(Links = links.d3, Nodes = nodes, Source = "from", Target = "to", 
             Value = "value", NodeID = "name", Group = "node.type", 
             linkColour = "#afafaf", fontSize = 12, zoom = T, legend = T, 
             opacity = 0.8, charge = -300, arrows = TRUE, bounded = TRUE, 
             opacityNoHover = 2)