【问题标题】:show edge weights with the forceNetwork function from R networkD3 package使用 R networkD3 包中的 forceNetwork 函数显示边缘权重
【发布时间】:2020-04-25 09:34:11
【问题描述】:

有没有什么办法,使用,在networkD3::forceNetwork生成的图上显示边权重?

【问题讨论】:

  • 欢迎来到 SO 和 R。为了帮助我们帮助您,请将您的代码和数据放入问题中。按照此链接minimal reproducible example 中的指导,您可能会发现编写一个出色的问题很有帮助。请,请以数据框格式添加一些数据,例如df

标签: r r htmlwidgets networkd3


【解决方案1】:

networkD3::forceNetworkValue 参数是一个字符串,用于设置 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)

【讨论】:

  • 我问的是,当将鼠标光标移动到 html 网络中的边缘时,是否有一种方法可以显示边缘权重
  • 编辑您的问题以正确详细说明您想要什么,以便有人能够回答它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-20
  • 2017-10-11
  • 2022-08-15
相关资源
最近更新 更多