Coloring scheme on the network D3 vs igraph

I have a network where I characterized each of the nodes with the previous analysis and assigned colors 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 the previous analysis, so the colors will match the placement / clustering of the vertices.

enter image description here

However, when I try to implement a personalized coloring 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 its own coloring scheme ... so when I omit the argument: 'colorScale = node_colors', I get the following:

enter image description here

But, as you can see, the colors do not synchronize with the colors in the graphic.

Does anyone know how to create a personalized coloring scheme (a vector containing a number of colors) in networkD3

+1
r igraph networkd3
source share
1 answer

You can pass Javascript code using 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 bind certain colors to certain variables, you can use

  YourColors <- 'd3.scaleOrdinal() .domain(["variable1", "variable2", "variable3"]) .range(["#7FFF00", "#A52A2A", "#E6E6FA"])' 
+3
source share

All Articles