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

时间:2018-09-27 19:55:14

标签: r htmlwidgets networkd3

我有以下数据:

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以外,每种蛋白质均应与反应连接,这三种蛋白应与两个独立的反应连接。

这就是我得到的:

enter image description here

我期望的示例:

enter image description here

有人可以解释为什么并达到我想要的结果吗?

1 个答案:

答案 0 :(得分:2)

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

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

第二,我不理解您试图创建links.d3 data.frame的意图,但是fromto变量/列应该是节点的索引在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)

enter image description here