用R networkD3包中的forceNetwork函数显示边缘权重。

问题描述 投票:0回答:2

有没有什么办法,用 的图形上显示边缘权重。networkD3::forceNetwork?

r htmlwidgets networkd3
2个回答
0
投票

有什么方法可以在networkD3::forceNetwork生成的图上显示边缘权重?Value 的论点 networkD3::forceNetwork 是一个字符串,设置了变量column的名称。Links 数据帧,其中包含每个链路的值。该值通常是 edgelink 权重。每个链路的指定变量中的值将决定每个链路的宽度,显示边缘权重(如果该值指的是这个)。

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)

enter image description here

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)

enter image description here


更新 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)

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.