R networkD3 包:simpleNetwork() 中的节点着色

R networkD3 package: node coloring in simpleNetwork()

networkD3 包(参见 here and here)允许用户创建简单的交互式网络:

# Load package
library(networkD3)

# 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)

# Plot
simpleNetwork(networkData)

有没有办法指定我希望 src 向量中的所有元素都是某种颜色,同时允许 target 向量中的所有元素是不同的颜色?这将使我能够从视觉上区分网络中的 src 个节点和 target 个节点。

simpleNetwork() 目前似乎不支持此功能(但我希望有人可以用自制脚本帮助我):

一个类似但不相关的问题是

您可以执行与链接 post 中类似的操作,但这里是使用 visNetwork 包的示例。这个包与 vis.js 接口,并制作非常好的交互式图形。

library(visNetwork)
id <- unique(c(src, target))                                  # node ids
nodes <- data.frame(id, group=+(id %in% src),                 # add a grouping for src/target
  label=id, title=sprintf('<p>Node %s</p>', id))              # add some labels
g <- visNetwork(nodes=nodes, edges=networkData, width="75%")  # make graph
visExport(visHierarchicalLayout(g))                           # make it tree-like

下面是如何使用 forceNetwork 控制节点的颜色。请注意,这仍然不会告诉您链接的方向,因为某些节点是某些链接的源和其他链接的目标 - 因此您需要以某种方式重新考虑该逻辑。但是无论如何,这里是控制节点的颜色。

# 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")

# simple with default colours
forceNetwork(Links = edges, Nodes = nodes, 
             Source = "source",
             Target = "target",
             NodeID ="name",
             Group = "group",
             Value = "width",
             opacity = 0.9,
             zoom = TRUE)

# control colours with a JS ordinal scale
# edited 20 May 2017 with updated code from Renal Chesak's answer:
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))

Peter Ellis 的上述回答可能曾经有效,但似乎代码已在新版本中更新。您将不得不使用 d3.scaleOrdinal().range([])

而不是 d3.scale.ordinal().range([])

Peter Ellis 的回答完成了这项工作,但这是另一种方法,恕我直言,这种方法更简洁,更容易理解...

(另外,应该注意节点可以同时在 sourcetarget 向量中,在这种情况下,它们应该是哪种颜色,给定您的规范, 不确定。)

library(networkD3)

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)

# make a nodes data frame out of all unique nodes in networkData
nodes <- data.frame(name = unique(c(networkData$src, networkData$target)))

# make a group variable where nodes in networkData$src are identified
nodes$group <- nodes$name %in% networkData$src

# make a links data frame using the indexes (0-based) of nodes in 'nodes'
links <- data.frame(source = match(networkData$src, nodes$name) - 1,
                    target = match(networkData$target, nodes$name) - 1)

forceNetwork(Links = links, Nodes = nodes, Source = "source",
             Target = "target", NodeID ="name", Group = "group",
             opacity = 1, opacityNoHover = 1)