如何创建将同一组中的节点绘制在一起的布局?

How to create layout that will plot nodes in the same group close together?

使用 igraph,我的目标是使用社区检测方法,因为我想绘制一个网络布局,使不同的社区及其联系可见。

到目前为止,这是我的代码:

library(igraph)

dat=read.csv(file.choose(),header=TRUE) # choose an edgelist in .csv file format

我有一个包含以下参数的数据框;

  > 
    > Var1 = node 1  Var2 = node 2 Value = edges (markov chain probabilities)
    > 
    >
    > head (dat, 100)
    >         Var1 Var2       value
    >     1      4    4 0.833333333
    >     2     10    4 0.000000000
    >     3     11    4 0.000000000
    >     4     12    4 0.000000000
    >     5     13    4 0.000000000
    >     6     21    4 0.000000000
    >     7     23    4 0.000000000
    >     8     31    4 0.000000000
    >     9     41    4 0.000000000
    >     10    42    4 0.000000000
    >     11    43    4 0.000000000
    >     12    44    4 0.000000000
    >     13    45    4 0.000000000
    >     14    46    4 0.000000000
    >     15    47    4 0.000000000
    >     16    48    4 0.000000000
    >     17    52    4 0.000000000
    >     18    53    4 0.000000000
    >     19    61    4 0.000000000
    >     20    62    4 0.000000000
    >     21    63    4 0.000000000
    >     22    71    4 0.000000000
    >     23    81    4 0.000000000
    >     24    82    4 0.000000000
    >     25    83    4 0.000000000
    >     26    91    4 0.000000000
    >     27    92    4 0.000000000
    >     28    93    4 0.000000000
    >     29   100    4 0.000000000
    >     30   111    4 0.000000000
    >     31     4   10 0.000000000
    >     32    10   10 0.000000000
    >     33    11   10 0.000000000
    >     34    12   10 0.010695187
    >     35    13   10 0.000000000
    >     36    21   10 0.000000000
    >     37    23   10 0.000000000
    >     38    31   10 0.000000000
    >     39    41   10 0.010869565
    >     40    42   10 0.000000000
    >     41    43   10 0.000000000
    >     42    44   10 0.000000000
    >     43    45   10 0.000000000
    >     44    46   10 0.000000000
    >     45    47   10 0.000000000
    >     46    48   10 0.000000000
    >     47    52   10 0.000000000
    >     48    53   10 0.000000000
    >     49    61   10 0.000000000
    >     50    62   10 0.074074074
    >     51    63   10 0.000000000
    >     52    71   10 0.000000000
    >     53    81   10 0.000000000
    >     54    82   10 0.000000000
    >     55    83   10 0.000000000
    >     56    91   10 0.000000000
    >     57    92   10 0.000000000
    >     58    93   10 0.000000000
    >     59   100   10 0.010526316
    >     60   111   10 0.018867925
    >     61     4   11 0.166666667
    >     62    10   11 0.000000000
    >     63    11   11 0.973409307
    >     64    12   11 0.010695187
    >     65    13   11 0.126126126
    >     66    21   11 0.000000000
    >     67    23   11 0.000000000
    >     68    31   11 0.000000000
    >     69    41   11 0.000000000
    >     70    42   11 0.008928571
    >     71    43   11 0.000000000
    >     72    44   11 0.038461538
    >     73    45   11 0.000000000
    >     74    46   11 0.000000000
    >     75    47   11 0.000000000
    >     76    48   11 0.000000000
    >     77    52   11 0.000000000
    >     78    53   11 0.000000000
    >     79    61   11 0.000000000
    >     80    62   11 0.000000000
    >     81    63   11 0.333333333
    >     82    71   11 0.000000000
    >     83    81   11 0.000000000
    >     84    82   11 0.000000000
    >     85    83   11 0.000000000
    >     86    91   11 0.071428571
    >     87    92   11 0.006622517
    >     88    93   11 0.000000000
    >     89   100   11 0.005263158
    >     90   111   11 0.018867925
    >     91     4   12 0.000000000
    >     92    10   12 0.000000000
    >     93    11   12 0.003798670
    >     94    12   12 0.673796791
    >     95    13   12 0.099099099
    >     96    21   12 0.000000000
    >     97    23   12 0.000000000
    >     98    31   12 0.029702970
    >     99    41   12 0.141304348
    >     100   42   12 0.017857143
    > 
    >     dim (dat)
    >     [1] 900   3








g = graph.data.frame(dat[,c('Var1','Var2')], directed = F)  # coerces the data into a two-column matrix format that igraph likes

cluster=cluster_walktrap(g)

list=groups (cluster)

g$value<-cluster$membership[as.character(g$Var1)]

g <- simplify(g) # remove loops and multiple edges



plot( cluster,g,  vertex.size = 5, edge.width = .1)

输出对我来说没有任何意义。请问你能帮帮我吗?谢谢

通过对您的代码进行非常小的更改,我得到了一个可以理解的结果(尽管我只使用了您提供的 100 分)。你的代码 运行 simplify after 你 运行 cluster_walktrap。我先运行simplify。我还设置了 运行dom 种子,以便结果可以重现。 我明白了。

library(igraph)
g = graph.data.frame(dat[,c('Var1','Var2')], directed = F)  # coerces the data into a two-column matrix format that igraph likes
g <- simplify(g) # remove loops and multiple edges
cluster=cluster_walktrap(g)
list=groups (cluster)
g$value<-cluster$membership[as.character(g$Var1)]
set.seed(123)
plot(cluster,g,  vertex.size = 5, edge.width = .1)

这很明显有两组;一组中的蓝色节点,另一组中的 o运行ge 节点。 连接同一组中的节点的链接是黑色的。那些连接节点的链接 不同的组是红色的。