r语言 - networkD3 中的原力网络不产生图形,也不产生错误



我正在尝试使用 R 中的 networkD3 生成一个简单的力网络图,但使用以下代码没有得到任何输出或错误。 我已经确认我的节点 ID 是 0 索引,数值和名称/组字段是字符。 任何帮助,不胜感激。

# 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可以看出:

节点

大小字符串,指定节点数据中的列 具有一些值的框架来改变节点半径。参见 半径计算。

所以你是说您希望第 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)

相关内容

  • 没有找到相关文章