Specify colors for each link in a force directed network, networkD3::forceNetwork()

被刻印的时光 ゝ 提交于 2019-12-05 07:51:51

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.

I 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" }'))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!