Network Analysis - Manipulating data - Shiny

不问归期 提交于 2019-12-24 07:11:33

问题


I'm using the networkD3 package and I'm trying to do some network analysis

Let's say I have this data:

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)

I've read this documentation https://christophergandrud.github.io/networkD3/ but they won't tell how they manipulated the data on MisLinks and MisNodes to create the other columns... how could I manipulate my data frame so it could work on the forceNetwork() function like the one on Les miserables data?


回答1:


You could use simpleNetwork directly like this: simpleNetwork(networkData)

If you want to use forceNetwork, the help page says that the Nodes data frame is "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.", so it should look something like this...

#             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

which you could create from your networkData data frame like this...

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

The help page says that the Links data frame is "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.", so it should look something like this...

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

which you could then create using your networkData data frame and the nodes data frame created above like this...

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

then you pass them to forceNetwork and tell it the names of the columns in your data frames like this...

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


来源:https://stackoverflow.com/questions/50804146/network-analysis-manipulating-data-shiny

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!