网络分析 - 处理数据 - 闪亮

Network Analysis - Manipulating data - Shiny

我正在使用 networkD3 包,我正在尝试进行一些网络分析

假设我有这个数据:

src <- c("Dizzy Gillespie","Louis Armstrong","Max Roach","Charlie Parker","Charlie Parker")
target <- c("Chet Baker","Chet Baker","Peter Erskine","John Coltrane","Wayne Shorter")
group <- c("Trumpet","Trumpet","Drums","Saxophone","Saxophone")

networkData <- data.frame(src, target,group)

我已阅读此文档 https://christophergandrud.github.io/networkD3/,但他们不会告诉他们如何操作 MisLinksMisNodes 上的数据来创建其他列...我怎么能操纵我的数据框,以便它可以在 forceNetwork() 函数上工作,就像悲惨世界数据中的函数一样?

您可以像这样直接使用 simpleNetworksimpleNetwork(networkData)

如果你想使用 forceNetwork,帮助页面说 Nodes 数据框是 "a data frame containing the node id and properties of the nodes. If no ID is specified then the nodes must be in the same order as the Source variable column in the Links data frame. Currently only a grouping variable is allowed.",所以它应该看起来像这样...

#             names     group
# 1 Dizzy Gillespie   Trumpet
# 2 Louis Armstrong   Trumpet
# 3       Max Roach     Drums
# 4  Charlie Parker Saxophone
# 5      Chet Baker   Trumpet
# 6   Peter Erskine     Drums
# 7   John Coltrane Saxophone
# 8   Wayne Shorter Saxophone

您可以像这样从 networkData 数据框创建...

col_names <- c("name", "group")
nodes <- rbind(setNames(networkData[c(1, 3)], col_names), 
               setNames(networkData[c(2, 3)], col_names))
nodes <- unique(nodes)

帮助页面说 Links 数据框是 "a data frame object with the links between the nodes. It should include the Source and Target for each link. These should be numbered starting from 0. An optional Value variable can be included to specify how close the nodes are to one another.",所以它应该看起来像这样...

#   src target
# 1   0      4
# 2   1      4
# 3   2      5
# 4   3      6
# 5   3      7

然后您可以使用 networkData 数据框和上面创建的 nodes 数据框创建它...

networkData$src <- match(networkData$src, nodes$name) - 1
networkData$target <- match(networkData$target, nodes$name) - 1
networkData$group <- NULL

然后将它们传递给 forceNetwork 并告诉它数据框中列的名称,如下所示...

forceNetwork(Links = networkData, Nodes = nodes, Source = "src", 
             Target = "target", NodeID = "name", Group = "group")