Reputation: 56
In forcenetwork, i found group and colourscale to colour the node in a network. I would like to have 2 different groups(colour is applied to the node based on user input between 2 different variables). Is it possible to have it? If yes, how?
Forcenetwork - https://www.rdocumentation.org/packages/networkD3/versions/0.4/topics/forceNetwork
Any kind of help will be useful. Thanks!
Upvotes: 2
Views: 1020
Reputation: 8848
The colourScale
parameter determines the color palette, while the group
parameter determines the name of the vector in your nodes data frame that contains values to distinguish the group of each node. networkD3
automatically chooses a unique color from the color palette for each distinct group in your data and applies that color to each node in that group.
library(networkD3)
links <- read.table(header = T, text = "
source target value
0 1 1
1 2 1
2 0 1
0 3 1
3 4 1
4 5 1
5 3 1
")
nodes <- read.table(header = T, text = "
name group
zero 1
one 1
two 1
three 2
four 2
five 2
")
forceNetwork(Links = links, Nodes = nodes,
Source = "source", Target = "target", Value = "value",
NodeID = "name", Group = "group",
colourScale = JS("d3.scaleOrdinal(d3.schemeCategory10);"))
If you have two separate variables in your data that jointly determine the group of each node, you should combine them in your data before passing them to forceNetwork()
...
nodes <- read.table(header = T, text = "
name group1 group2
zero A D
one B E
two C F
three A E
four B F
five C D
")
nodes$group <- paste(nodes$group1, nodes$group2, sep = "_")
forceNetwork(Links = links, Nodes = nodes,
Source = "source", Target = "target", Value = "value",
NodeID = "name", Group = "group",
colourScale = JS("d3.scaleOrdinal(d3.schemeCategory10);"))
Upvotes: 1