networkD3::forceNetwork 的 Value 参数是一个字符串,用于设置 Links 数据框中的变量/列的名称,其中包含每个链接的值。该值通常是边缘/链接权重。每个链接的指定变量中的值将确定每个链接的宽度,显示边缘权重(如果这是值所指的)。
library(networkD3)
library(tibble)
nodes <-
tribble(
~name, ~group,
"a", 1,
"b", 1,
"c", 1,
"d", 1
)
links <-
tribble(
~source, ~target, ~value,
0, 1, 1,
0, 2, 1,
0, 3, 1,
)
forceNetwork(Links = links, Nodes = nodes, Source = "source",
Target = "target", Value = "value", NodeID = "name",
Group = "group", opacity = 1)
links <-
tribble(
~source, ~target, ~value,
0, 1, 1,
0, 2, 20,
0, 3, 100,
)
forceNetwork(Links = links, Nodes = nodes, Source = "source",
Target = "target", Value = "value", NodeID = "name",
Group = "group", opacity = 1)
更新 2020.04.26
这是一种向链接添加文本标签的方法,以便在将鼠标悬停在链接上时显示链接权重的值。
library(tibble)
library(networkD3)
library(htmlwidgets)
nodes <-
tribble(
~name, ~group,
"a", 1,
"b", 1,
"c", 1,
"d", 1
)
links <-
tribble(
~source, ~target, ~value,
0, 1, 1,
0, 2, 20,
0, 3, 100,
)
fn <- forceNetwork(Links = links, Nodes = nodes, Source = "source",
Target = "target", Value = "value", NodeID = "name",
Group = "group", opacity = 1)
link_value_js <- '
function(el) {
d3.select(el)
.selectAll(".link")
.append("title")
.text(d => d.value);
}
'
onRender(fn, link_value_js)