我真的很喜欢这个网络的样子:
来源:https ://christophergandrud.github.io/networkD3/
每个扇区有多种颜色,其中每种颜色代表网络中与其他节点相距足够远的节点的某些不同部分。
但是,在任何一个特定部门中,我不喜欢src
节点与节点的颜色相同target
,所以我问了一个问题(https://stackoverflow.com/a/35371131/3878253)如何区分两种颜色。
现在有没有一种方法可以扩展此代码以恢复多种配色方案(以区分各个部门),同时保留颜色的漂亮src
/target
差异?这是代码:
# Load package
library(networkD3)
library(dplyr) # to make the joins easier
# Create fake data
src <- c("A", "A", "A", "A",
"B", "B", "C", "C", "D")
target <- c("B", "C", "D", "J",
"E", "F", "G", "H", "I")
networkData <- data.frame(src, target, stringsAsFactors = FALSE)
nodes <- data.frame(name = unique(c(src, target)), stringsAsFactors = FALSE)
nodes$id <- 0:(nrow(nodes) - 1)
# create a data frame of the edges that uses id 0:9 instead of their names
edges <- networkData %>%
left_join(nodes, by = c("src" = "name")) %>%
select(-src) %>%
rename(source = id) %>%
left_join(nodes, by = c("target" = "name")) %>%
select(-target) %>%
rename(target = id)
edges$width <- 1
# make a grouping variable that will match to colours
nodes$group <- ifelse(nodes$name %in% src, "lions", "tigers")
ColourScale <- 'd3.scaleOrdinal()
.domain(["lions", "tigers"])
.range(["#FF6900", "#694489"]);'
forceNetwork(Links = edges, Nodes = nodes,
Source = "source",
Target = "target",
NodeID ="name",
Group = "group",
Value = "width",
opacity = 0.9,
zoom = TRUE,
colourScale = JS(ColourScale))