Colouring scheme in networkD3 vs igraph
Asked Answered
G

1

1

I have a network where I have characterised each of the nodes with previous analysis- and have assigned the colours as follows:

plot.igraph(g, layout=layout.fruchterman.reingold(g), vertex.color=node_colors, vertex.label = node_names)

The variable 'node_colors' is a vector that was made from previous analysis so that the colours would coincide with the vertex placement/ clustering.

enter image description here

However when I try to implement a personalised colouring scheme in networkD3, I get an 'unused argument' error:

data<-igraph_to_networkD3(g, group = members)
forceNetwork(Links = data$links, Nodes = data$nodes, Source = 'source', Target = 'target', NodeID = 'value', Nodesize = 'size', Group = "group", colourScale=node_colors, zoom = T, legend = T, opacityNoHover = TRUE)

Error: Warning: Error in forceNetwork: unused argument (colorScale = node_colors)

NetworkD3 seems to just create it's own colouring scheme... so when I omit the argument: 'colorScale=node_colors', I get the following:

enter image description here

But as you can see the colours are not synced with that of the igraph plot.

Does anyone know how to create a personalised colouring scheme (vector containing a series of colours) in networkD3

Giantism answered 5/8, 2016 at 16:31 Comment(1)
Possible duplicate of this questionPirandello
B
4

You can pass Javascript code through with the JS() function that comes with the networkD3 package. In Javascript, you can use hex code to specify colors. Here is an example:

YourColors <- 'd3.scaleOrdinal().range([ "#FFA500", "#4169E1",  "#32CD32",
                                         "#FFC0CB", "#8A2BE2", "#FF6347"]);'

forceNetwork(Links = data$links, Nodes = data$nodes, 
             Source = 'source', Target = 'target', 
             NodeID = 'value', Nodesize = 'size', Group = "group",
             ######################### 
             colourScale = JS(YourColors), 
             #########################
             zoom = T, legend = T, opacityNoHover = TRUE)

If you want to tie specific colors to specific variables, you can use

    YourColors <- 'd3.scaleOrdinal()
                  .domain(["variable1", "variable2", "variable3"])
                  .range(["#7FFF00", "#A52A2A", "#E6E6FA"])'
Bravin answered 14/4, 2017 at 15:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.