具有NodeSize的R闪亮D3网络图

时间:2019-02-04 16:28:52

标签: r d3.js shiny

我正在使用D3Network库在Web上绘制图形(使用光泽)。

wc <- cluster_walktrap(finalGraph)
members <- membership(wc)
karate_d3 <- igraph_to_networkD3(finalGraph, group = members)

forceNetwork(Links = karate_d3$links, Nodes = karate_d3$nodes, 
                     Source = 'source', Target = 'target', opacity = 0.85, zoom = TRUE,
                     NodeID = 'name', Group = 'group', fontSize = 20, Nodesize = "weight",
                     colourScale = JS('force.alpha(1); force.restart(); d3.scaleOrdinal(d3.schemeCategory20);'))

finalGraph这是一个加权igraph,我将其转换为d3

V(finalGraph)$weight <- #logic to assign weight

我希望D3的Nodesize表示权重值,但是做Nodesize = "weight"无效,因为未定义列。如何将其添加为d3的属性/列,以便可以绘制权重?

1 个答案:

答案 0 :(得分:1)

您必须将大小存储在节点矩阵中。这是一个包含图形的完整示例。请注意,节点大小不是直接获取的,而是由另一个函数(radiusCalculation)以某种方式重新计算的。

library(networkD3)
library(igraph)

# build graph
finalGraph <- erdos.renyi.game(10,0.3)
wc <- cluster_walktrap(finalGraph)
members <- membership(wc)
karate_d3 <- igraph_to_networkD3(finalGraph, group = members)

# here is the important line: set size!
karate_d3$nodes$size = abs(rnorm(10,mean=1,sd=10))^2

forceNetwork(Links = karate_d3$links, Nodes = karate_d3$nodes, 
             Source = 'source', Target = 'target', opacity = 0.85, zoom = TRUE,
             NodeID = 'name', Group = 'group', fontSize = 20, Nodesize = 'size',
             colourScale = JS('force.alpha(1); force.restart(); d3.scaleOrdinal(d3.schemeCategory20);'))