R: Keep same set of default colors in visNetwork, even when having missing groups

Viewed 633

I want to plot a network using the visNetwork package. I would like my nodes across all the different networks using the same colors for the same groups. So I want

  • group A -> blue
  • group B -> yellow
  • group C -> red.

which are the first 3 default colors. Here is a first simple example:

nodes <- data.frame(id = 1:10, label = paste("Label", 1:10), 
                    group = factor(c("A","B","C", "A","C","C","C","A","A","A"), levels=c("A","B","C"))) 
visnw <- visNetwork(nodes, edges) %>%
  visLegend(position = "right")
visnw 

This gives enter image description here

But when I omit group B the color "red" disappears even when I specify that column 'group' is a factor containing 3 levels, this fact is ignored in the visualisation. Moreover, when the first element is not "A" this are shuffled even more.

nodes <- data.frame(id = 1:10, label = paste("Label", 1:10), 
                    group = factor(c("C","A","C", "A","C","C","C","A","A","A"),c("A","B","C"))) %>%
                    dplyr::arrange(group) #needed to have A as first group but it doesn't seem to have an effect on the second visualisation
edges <- data.frame(from = c(2,5,10), to = c(1,2,10))
visnw <- visNetwork(nodes, edges) %>%
  visLegend(position = "right")
visnw 

However, the above code gives me this which is wrong because group C must have color red. Moreover the groups are switched because nodes 1,3,5,6,7 belong to group "C" and not "A". This latter problem doesn't ariwe when I omit the arrange(), but then the legend has level "C" on top and below level "A" which isn't nice.

enter image description here

I have tried to edit the list visnw, but this doesn't work.

I almost can achieve what I want by explicitly defining the colors I want for each group.

visnw <- visNetwork(nodes, edges) %>%
  visGroups(groupname = "A", color = "lightblue") %>%
  visGroups(groupname = "B", color = "yellow") %>%
  visGroups(groupname = "C", color = "red") %>%
  visLegend(position = "right")
visnw

This gives me this visualisation:

enter image description here

I prefer to use the default colors, since I like them and there is also an edge on the node in the default scheme which I should have to define manually as well. Anybody knows how to achieve this?

2 Answers

The default group color options are provided by vis.js; you can see the full list here.

Based on the parameter list in the link above, you can, for example, fully replicate the default group styling for the first group like this:

visnw <- visNetwork(nodes, edges) %>%
  ## 0: blue
  visGroups(groupname = "A", color = list(border = "#2B7CE9", background = "#97C2FC", highlight = list(border = "#2B7CE9", background = "#D2E5FF"), hover = list(background = "#2B7CE9", border = "#D2E5FF")))

Alternatively, if you want to change just the salient parameters, you can try the following:

visnw <- visNetwork(nodes, edges) %>%
  ## 0: blue
  visGroups(groupname = "A", color = list(border = "#2B7CE9", background = "#97C2FC")) %>%
  ## 1: yellow
  visGroups(groupname = "B", color = list(border = "#FFA500", background = "#FFFF00")) %>%
  ## 2: red
  visGroups(groupname = "C", color = list(border = "#FA0A10", background = "#FB7E81")) %>%
  visLegend(position = "right")
visnw

I believe that if you want "A" "B" and "C" to be the first three default colors regardless of if and how they are used, you can do this first:

visnw.groups.get("A");
visnw.groups.get("B");
visnw.groups.get("C");

The default behavior of .get() is to create each group using the next available default.

Now it won't matter if you introduce nodes in a different order or have no nodes in group "B", as the groups have been created beforehand.

Related