0

My question is with regards to:

R networkD3 color node stroke for radialNetwork()

Whenever I create and use a colour vector (context given in link's answer), the diagonalNetwork() distributes the node colours incorrectly.

Is there any way to know how the diagonalNetwork() rearranges the data input into it? The network labels and structure are correct. Only the colours are not.

CJ Yetman
  • 8,373
  • 2
  • 24
  • 56
Cam K
  • 127
  • 2
  • 2
  • 13

1 Answers1

0

The colors are applied per level, and then in the order the data was originally. In this example, the colors are ordered so that the root is black, the provinces are red, and the cities are blue.

library(networkD3)

CanadaPC <- list(name = "Canada", children = 
                   list(list(name = "Newfoundland",
                             children = list(list(name = "St. John's"))),
                        list(name = "PEI",
                             children = list(list(name = "Charlottetown"))),
                        list(name = "Nova Scotia",
                             children = list(list(name = "Halifax"))),
                        list(name = "New Brunswick",
                             children = list(list(name = "Fredericton"))),
                        list(name = "Quebec",
                             children = list(list(name = "Montreal"),
                                             list(name = "Quebec City"))),
                        list(name = "Ontario",
                             children = list(list(name = "Toronto"),
                                             list(name = "Ottawa"))),
                        list(name = "Manitoba",
                             children = list(list(name = "Winnipeg"))),
                        list(name = "Saskatchewan",
                             children = list(list(name = "Regina"))),
                        list(name = "Nunavuet",
                             children = list(list(name = "Iqaluit"))),
                        list(name = "NWT",
                             children = list(list(name = "Yellowknife"))),
                        list(name = "Alberta",
                             children = list(list(name = "Edmonton"))),
                        list(name = "British Columbia",
                             children = list(list(name = "Victoria"),
                                             list(name = "Vancouver"))),
                        list(name = "Yukon",
                             children = list(list(name = "Whitehorse")))
                   ))

colorVector <- c("black", rep("red", 13), rep("blue", 16))

jsarray <- paste0('["', paste(colorVector, collapse = '", "'), '"]')
nodeStrokeJS <- JS(paste0('function(d, i) { return ', jsarray, '[i]; }'))

diagonalNetwork(List = CanadaPC, nodeStroke = nodeStrokeJS)

enter image description here

CJ Yetman
  • 8,373
  • 2
  • 24
  • 56
  • Thank you for your help! To re-arrange my data by tree "depth", I did the following: asdf<-rbind(1:length(CEO_depth),CEO_depth) abcd<-split(asdf[1,1:dim(asdf)[2]],asdf[2,1:dim(asdf)[2]]) diagindex<-c() for (n5 in 1:length(abcd)){ qwer<-unlist(abcd[n5]) for (n6 in 1:length(unlist(qwer))){ diagindex<-rbind(diagindex,as.numeric(as.character(unlist(qwer)[n6]))) } } colour3<-colour2[diagindex] – Cam K Jul 08 '18 at 15:21
  • Now I just have to figure out how to format stack overflow comments. – Cam K Jul 08 '18 at 15:27