christophergandrud / networkD3

D3 JavaScript Network Graphs from R
http://christophergandrud.github.io/networkD3
652 stars 269 forks source link

Some colors ignored in sankeyNetwork(colourScale = ...) #242

Open wlandau opened 6 years ago

wlandau commented 6 years ago

The following graph should have a green node, but it appears blue. Tested with both the CRAN release and 9c0a9c9ff32c53212d2d43e0ae3cc664137315e6.

nodes <- data.frame(
  id = letters[1:3],
  label = c("blue", "green", "black"),
  status = c("imported", "up to date", "outdated"),
  stringsAsFactors = FALSE 
)
edges <- data.frame(
  src = c(0, 0, 1),
  target = c(2, 1, 2),
  value = 1
)
color <- "d3.scaleOrdinal() .domain(['imported', 'up to date', 'outdated']) .range(['#1874CD', '#228B22', '#000000'])"
networkD3::sankeyNetwork(
  Links = edges,
  Nodes = nodes,
  NodeID = "label",
  Source = "src",
  Target = "target",
  NodeGroup = "status",
  Value = "value",
  colourScale = color,
  fontSize = 16
)

color

cjyetman commented 6 years ago

This happens because there is a space in "up to date". If you change the group value and the value in your color domain to ""up-to-date", it should work as expected.

pretty sure this is caused by this line: return color(d.group.replace(/ .*/, ""));

not sure if that line has some other important purpose

wlandau commented 6 years ago

Thanks CJ, I would not have expected that. Removing spaces works. For my own application, this is an acceptable workaround.

nodes <- data.frame(
  id = letters[1:3],
  label = c("blue", "green", "black"),
  status = c("imported", "up-to-date", "outdated"),
  stringsAsFactors = FALSE 
)
edges <- data.frame(
  src = c(0, 0, 1),
  target = c(2, 1, 2),
  value = 1
)
color <- "d3.scaleOrdinal() .domain(['imported', 'up-to-date', 'outdated']) .range(['#1874CD', '#228B22', '#000000'])"
networkD3::sankeyNetwork(
  Links = edges,
  Nodes = nodes,
  NodeID = "label",
  Source = "src",
  Target = "target",
  NodeGroup = "status",
  Value = "value",
  colourScale = color,
  fontSize = 16
)

color2

cjyetman commented 5 years ago

We should at least at a note to the documentation that group values must not have a space. Allowing for spaces in the group name would break backwards compatibility, so I'm not sure we should do that.