networkD3 中的 forcenetwork 不产生任何图形和错误

forcenetwork in networkD3 Produces No Graph and No Errors

我正在尝试在 R 中使用 networkD3 生成一个简单的 forcenetwork 图,但我使用下面的代码没有得到任何输出或错误。我已经确认我的节点 ID 是 0 索引的,数值和 name/group 字段是字符。感谢任何帮助。

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

试试这个:

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)

?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.

所以你是说你希望第 1 列是你的节点的大小,但它需要一个字符串来引用该列,即使接受了一个位置,在节点中 data.frame 第 1 列不是大小但名字。

因此,如果您想确定一个 Nodesize,请在节点中创建一个新列并分配您想要的值。然后,将该列的名称传递给 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)