C.J
C.J

Reputation: 169

networkD3 doesn't draw the expected interactive network

I have the following data:

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

I am using package, I have the following code:

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 and p14 are reactions. Expected result is that each protein should connect to a reaction except H2O, L-Met and LHCYS, these three should to connect to two seperate reactions.

This is what I get:

enter image description here

Plot example of what I am expecting:

enter image description here

Can someone explain why and achieve the result I want?

Upvotes: 2

Views: 100

Answers (1)

CJ Yetman
CJ Yetman

Reputation: 8848

First, you're missing links in your links data that define what you want to show:

  • p12 -> p4 (reaction1 -> L-Met)
  • p14 -> p5 (reaction3 -> H2O)
  • p14 -> p8 (reaction3 -> LHCYS)

Second, I don't understand what you were trying to do creating the links.d3 data.frame, but the from and to variables/columns should be the index of the node in the nodes data.frame, which you could create as below...

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

Upvotes: 2

Related Questions