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

2

6

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).

Unpromising answered 27/12, 2015 at 13:22 Comment(0)
P
6

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.

Parsley answered 30/9, 2016 at 1:23 Comment(1)
can we add a legend based on the link color ? Not replacing an existing legend. Something like. 2 legends one for the nodes and another for the link color ?Florenceflorencia
H
7

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" }'))
Haggard answered 27/12, 2015 at 17:14 Comment(4)
in this example is it possible to use a different attribute for color? so, use value for width of the edge and colocated for color? I tried passing an attribute called colocated in addition to source, target and value and changed the code to linkColour = JS('function(l) { return l.colocated > 0 ? "#00F" : "#F00" }'). I get the following error: cannot coerce class ""JS_EVAL"" to a data.frame.Flin
possibly a related question whereby one must colour the nodes with pre designated colours: #38794447Bertiebertila
@Flin it seems linkColour parameter no longer supports JS, while it worked well before. However, Joseah's answer works.Unpromising
Is it possible to add a legend based on linkColour argument?Tubb
P
6

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.

Parsley answered 30/9, 2016 at 1:23 Comment(1)
can we add a legend based on the link color ? Not replacing an existing legend. Something like. 2 legends one for the nodes and another for the link color ?Florenceflorencia

© 2022 - 2024 — McMap. All rights reserved.