使用R networkD3软件包中的forceNetwork函数显示边缘权重 [英] show edge weights with the forceNetwork function from R networkD3 package

查看:361
本文介绍了使用R networkD3软件包中的forceNetwork函数显示边缘权重的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法,使用的问题,在networkD3::forceNetwork?

Is there any way, using r, to display edge weights on the graph produced by networkD3::forceNetwork?

推荐答案

networkD3::forceNetworkValue参数是一个字符串,用于设置Links数据框中包含变量值的变量/列的名称.每个链接.该值通常是边缘/链接的权重.每个链接的指定变量中的值将确定每个链接的宽度,并显示边缘权重(如果这是值的话).

The Value argument of networkD3::forceNetwork is a string that sets the name of the variable/column in the Links data frame which contains the value for each link. That value is typically the edge/link weight. The value in the specified variable for each link will determine the width of each link, showing the edge weight (if that's what the value refers to).

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

UPDATE 2020.04.26

这是在链接上添加文本标签的一种方法,以便将鼠标悬停在链接上时显示链接权重的值.

Here's a way to add a text label to the links so that the value of the link weight will display when you hover the mouse over a link.

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)

这篇关于使用R networkD3软件包中的forceNetwork函数显示边缘权重的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆