I\'ve been trying to plot a network using networkD3 package in R for a week now. The simpleNetwork
function works normally, but it doesn\'t allow much
Technically speaking, the reason your example data will not work, even if you address other possible problems (like edg$Gene1
and edg$Gene2
being non-numeric), is because you refer to a node 22
in your edg
data, which in "0-based index" terms points to the 23rd row of your vert
data frame, which does not exist.
As has been pointed out, this is probably because it is in 1-based indexing and should be converted, which could easily be done with
edg$Gene1 <- edg$Gene1 - 1
edg$Gene2 <- edg$Gene2 - 1
Alternatively, one might have intended to refer to another node which, for whatever reason did not make it into the vert
data frame, in which case that node would need to be added to the vert
data frame, which could easily be done with (for example)...
vert <- rbind(vert, c(23,1,1))
You could test whether or not you refer to a node in your edj
data that doesn't exist in your vert
data with something like...
all(unique(c(edg$Gene1, edg$Gene2)) %in% (1:nrow(vert) - 1))
# [1] FALSE
which should return TRUE
. If not, something's wrong.
You could determine which nodes are referred to in your edg
data that do not exist in your vert
data with...
unique(c(edg$Gene1, edg$Gene2))[which(!unique(c(edg$Gene1, edg$Gene2)) %in% (1:nrow(vert) - 1))]
# [1] 22
fully reproducible example adjusting the indices in edg
to be "0-based"
edg <- read.csv(header = TRUE, colClasses = 'character', text = '
Gene1,Gene2,Prob
1,22,3
2,22,6
3,22,6
4,22,9
5,22,3
6,22,4
7,22,8
8,22,4
9,22,6
10,22,8
11,22,6
12,22,10
13,22,6
14,22,3
15,22,6
16,22,6
17,22,0
18,22,4
19,22,6
20,22,4
')
vert <- read.csv(header = TRUE, colClasses = 'character', text = '
Symbol,Chr,Expr
1,21,9
2,17,10
3,17,0
4,20,0
5,6,9
6,5,11
7,12,0
8,1,20
9,17,11
10,17,7
11,17,11
12,10,0
13,17,0
14,7,7
15,17,6
16,17,0
17,2,5
18,5,10
19,17,10
20,17,9
21,12,4
22,3,2
')
# cast to numeric just to be sure
edg$Gene1 <- as.numeric(edg$Gene1)
edg$Gene2 <- as.numeric(edg$Gene2)
# adjust the indices so they're "0-based"
edg$Gene1 <- edg$Gene1 - 1
edg$Gene2 <- edg$Gene2 - 1
# Nodesize is also necessarily numeric
vert$Expr <- as.numeric(vert$Expr)
library(networkD3)
forceNetwork(Links = edg, Nodes = vert, Source = "Gene1", Target = "Gene2",
Value = "Prob", NodeID = "Symbol", Group = "Chr", opacity = 0.7,
Nodesize = "Expr", zoom = TRUE, legend = TRUE)
fully reproducible example adding a node to vert
edg <- read.csv(header = TRUE, colClasses = 'character', text = '
Gene1,Gene2,Prob
1,22,3
2,22,6
3,22,6
4,22,9
5,22,3
6,22,4
7,22,8
8,22,4
9,22,6
10,22,8
11,22,6
12,22,10
13,22,6
14,22,3
15,22,6
16,22,6
17,22,0
18,22,4
19,22,6
20,22,4
')
vert <- read.csv(header = TRUE, colClasses = 'character', text = '
Symbol,Chr,Expr
1,21,9
2,17,10
3,17,0
4,20,0
5,6,9
6,5,11
7,12,0
8,1,20
9,17,11
10,17,7
11,17,11
12,10,0
13,17,0
14,7,7
15,17,6
16,17,0
17,2,5
18,5,10
19,17,10
20,17,9
21,12,4
22,3,2
')
# cast to numeric just to be sure
edg$Gene1 <- as.numeric(edg$Gene1)
edg$Gene2 <- as.numeric(edg$Gene2)
vert$Expr <- as.numeric(vert$Expr)
# add another node to the Nodes data frame
vert <- rbind(vert, c(23,1,1))
library(networkD3)
forceNetwork(Links = edg, Nodes = vert, Source = "Gene1", Target = "Gene2",
Value = "Prob", NodeID = "Symbol", Group = "Chr", opacity = 0.7,
Nodesize = "Expr", zoom = TRUE, legend = TRUE)