为强制定向网络中的每个链接指定颜色,networkD3 :: forceNetwork() [英] Specify colors for each link in a force directed network, networkD3::forceNetwork()

查看:405
本文介绍了为强制定向网络中的每个链接指定颜色,networkD3 :: forceNetwork()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是在R中使用 networkD3 :: forceNetwork 基于链接的值或权重指定两种不同的颜色。例如,蓝色表示链接权重大于1,黑色表示链接权重小于1。

The question is to specify two different colors based on the Value or weight of the link using networkD3::forceNetwork in R. For example, Blue for the weight of links more than 1, dark for the weight of links less than 1.

示例代码,复制自此处 forceNetwork section):

Example code, copied from here (the forceNetwork section):

library(networkD3)
# Load data
data(MisLinks)
data(MisNodes)

# Plot
forceNetwork(Links = MisLinks, Nodes = MisNodes,
        Source = "source", Target = "target",
        Value = "value", NodeID = "name",
        Group = "group", opacity = 0.8)

与d3-js相关的问题是这里(到目前为止我对JS一无所知)。

A d3-js related question is here (I know nothing about JS so far).

推荐答案

我在使用 networkD3 时遇到了同样的问题。

你可以通过提供一个向量来实现这一点,具体取决于值 MisLinks $ value 使用 ifelse 函数:

I've just had the same problem working with networkD3.
You can do that by providing a vector depending on the values of MisLinks$value using the ifelse function:

forceNetwork(Links = MisLinks, Nodes = MisNodes,
         Source = "source", Target = "target",
         Value = "value", NodeID = "name",
         Group = "group", opacity = 0.8,
         linkColour = ifelse(MisLinks$value > 1, "blue","black"))

此解决方案不依赖于知道 javascript

希望这会有所帮助。

This solution does not depend on knowing javascript.
Hope this helps.

这篇关于为强制定向网络中的每个链接指定颜色,networkD3 :: forceNetwork()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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