admin管理员组文章数量:1303654
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.
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)
A d3-js related question is here (I know nothing about JS so far).
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.
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)
A d3-js related question is here (I know nothing about JS so far).
Share Improve this question edited Jan 26, 2018 at 10:23 zx8754 56.2k12 gold badges125 silver badges225 bronze badges asked Dec 27, 2015 at 13:22 Zhilong JiaZhilong Jia 2,3691 gold badge23 silver badges35 bronze badges2 Answers
Reset to default 7I think you should be able to pass a javascript function wrapped in JS
to linkColour
to get colors based on the values in MisLinks. For example, return blue links for values > 1 and red for values <= 1.
forceNetwork(Links = MisLinks, Nodes = MisNodes,
Source = "source", Target = "target",
Value = "value", NodeID = "name",
Group = "group", opacity = 0.8,
linkColour = JS('function(l) { return l.value > 1 ? "#00F" : "#F00" }'))
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"))
This solution does not depend on knowing javascript
.
Hope this helps.
本文标签:
版权声明:本文标题:javascript - Specify colors for each link in a force directed network, networkD3::forceNetwork() - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741731221a2394852.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论