Reputation: 164
I am trying to produce a simple forcenetwork graph using networkD3 in R, but I do not get any output or errors using the code below. I have confirmed my node ID's are 0 indexed, numeric values and the name/group fields are characters. Any help is appreciated.
# Load Packages
library(dplyr)
library(networkD3)
zz <- "name id group source target value
A 0 A 0 1 2199
B 1 A 1 0 784
C 2 B 2 4 394
D 3 B 3 2 382
E 4 B 4 8 340
F 5 B 5 9 286
G 6 B 6 5 279
H 7 A 0 7 279
I 8 B 7 0 240
J 9 B 8 0 196
K 10 A 9 0 174"
df<- read.table(text=zz, header = TRUE)
nodes <- df %>%
select(name,id,group) %>%
mutate(name = as.character(name),id = as.numeric(id), group=as.character(group))
links <- df %>%
select(source,target,value) %>%
mutate(source = as.numeric(source),target=as.numeric(target),value=as.numeric(value))
# simple with default colours
forceNetwork(Links = links, Nodes = nodes,
Source = "source",
Target = "target",
NodeID ="name",
Group = "group",
Value = "value",
Nodesize = 1,
width = "1000px",
height = "600px",
opacity = 0.9,
zoom = TRUE)
sapply(nodes,class)
sapply(links, class)
Upvotes: 2
Views: 241
Reputation: 4480
Try this:
forceNetwork(Links = links, Nodes = nodes,
Source = "source",
Target = "target",
NodeID ="name",
Group = "group",
Value = "value",
# Nodesize = 1,
width = "1000px",
height = "600px",
opacity = 0.9,
zoom = TRUE)
As you can see from ?forceNetwork
:
Nodesize character string specifying the a column in the Nodes data frame with some value to vary the node radius's with. See also radiusCalculation.
So you are saying that you want column 1 to be the size of your nodes but it requires a string to reference the column and even if a position were accepted, in nodes data.frame
column 1 are not sizes but names.
So if you want to determine a Nodesize
create a new column in nodes and assign your desired value. Then, pass the name of that column to Nodesize
:
library(dplyr)
library(networkD3)
zz <- "name id group source target value
A 0 A 0 1 2199
B 1 A 1 0 784
C 2 B 2 4 394
D 3 B 3 2 382
E 4 B 4 8 340
F 5 B 5 9 286
G 6 B 6 5 279
H 7 A 0 7 279
I 8 B 7 0 240
J 9 B 8 0 196
K 10 A 9 0 174"
df<- read.table(text=zz, header = TRUE)
nodes <- df %>%
select(name,id,group) %>%
mutate(name = as.character(name),id = as.numeric(id), group=as.character(group))
links <- df %>%
select(source,target,value) %>%
mutate(source = as.numeric(source),target=as.numeric(target),value=as.numeric(value))
nodes<-nodes %>% mutate(size=100)
forceNetwork(Links = links, Nodes = nodes,
Source = "source",
Target = "target",
NodeID ="name",
Group = "group",
Value = "value",
Nodesize = "size",
width = "1000px",
height = "600px",
opacity = 0.9,
zoom = TRUE)
Upvotes: 2