在igraph R

时间:2018-06-15 10:06:36

标签: r csv graph igraph adjacency-matrix

我正在网络中实施社区检测,并且一直在尝试将我的网络绘制为R中的图形。我有两个.csv个文件(adj.csv& communities.csv),{ {1}}包含图adj.csv的邻接矩阵,其中包含应该组合在一起的顶点列表。

  

adj.csv:

     

0,1,1,2,1,1,0,1,2,2,1,1,0,0,1,0,0

     

1,0,1,1,0,0,0,0,1,0,0,0,0,0,2,0,0

     

1,1,0,1,0,1,1,1,2,2,1,1,0,1,1,0,1

     

2,1,1,0,1,1,0,1,0,1,1,1,0,0,1,0,0

     

1,0,0,1,0,0,0,1,0,1,1,1,0,0,0,0,0

     

1,0,1,1,0,0,0,0,0,0,1,1,1,1,0,0,2

     

0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0

     

1,0,1,1,1,0,0,0,0,2,1,1,0,1,0,0,0

     

2,1,2,0,0,0,1,0,0,1,0,0,0,0,1,0,0

     

2,0,2,1,1,0,0,2,1,0,1,1,0,1,0,0,0

     

1,0,1,1,1,1,0,1,0,1,0,2,1,0,0,0,1

     

1,0,1,1,1,1,0,1,0,1,2,0,0,0,0,0,2

     

0,0,0,0,0,1,0,0,0,0,1,0,0,1,0,0,0

     

0,0,1,0,0,1,0,1,0,1,0,0,1,0,0,0,0

     

1,2,1,1,0,0,0,0,1,0,0,0,0,0,0,0,0

     

0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1

     

0,0,1,0,0,2,0,0,0,0,1,2,0,0,0,1,0

     

communities.csv:

     

11,10,7,4,3

     

12,5

     

13,9

     

14,1,0,8,6,2

     

15,16

我想要的是显示由邻接矩阵(我使用communities.csv包完成)给出的图形和igraph每行中的顶点,以便在此图中一起显示并具有不同的颜色,即line1(11,10,7,4,3)的所有顶点可能都是红色,line2的所有顶点可能都是绿色等等,每行中的顶点必须在图中一起显示,基本上我是' d优先选择每行(社区)的随机颜色,因为行数可能会因communities.csv而异。

我是R的菜鸟因此对igraph也没有做太多,到目前为止我所做的是:

communities.csv

我知道它几乎没有,但这与算法实现完全不同。

编辑:

  

dput(adj.matrix)

结构(c(0L,1L,1L,2L,1L,1L,0L,1L,2L,2L,1L,1L,0L, 0L,1L,0L,0L,1L,0L,1L,1L,0L,0L,0L,0L,1L,0L,0L,0L, 0L,0L,2L,0L,0L,1L,1L,0L,1L,0L,1L,1L,1L,2L,2L,1L, 1L,0L,1L,1L,0L,1L,2L,1L,1L,0L,1L,1L,0L,1L,0L,1L, 1L,1L,0L,0L,1L,0L,0L,1L,0L,0L,1L,0L,0L,0L,1L,0L, 1L,1L,1L,0L,0L,0L,0L,0L,1L,0L,1L,1L,0L,0L,0L,0L, 0L,0L,1L,1L,1L,1L,0L,0L,2L,0L,0L,1L,0L,0L,0L,0L, 0L,1L,0L,0L,0L,0L,0L,0L,0L,0L,1L,0L,1L,1L,1L,0L, 0L,0L,0L,2L,1L,1L,0L,1L,0L,0L,0L,2L,1L,2L,0L,0L, 0L,1L,0L,0L,1L,0L,0L,0L,0L,1L,0L,0L,2L,0L,2L,1L, 1L,0L,0L,2L,1L,0L,1L,1L,0L,1L,0L,0L,0L,1L,0L,1L, 1L,1L,1L,0L,1L,0L,1L,0L,2L,1L,0L,0L,0L,1L,1L,0L, 1L,1L,1L,1L,0L,1L,0L,1L,2L,0L,0L,0L,0L,0L,2L,0L, 0L,0L,0L,0L,1L,0L,0L,0L,0L,1L,0L,0L,1L,0L,0L,0L, 0L,0L,1L,0L,0L,1L,0L,1L,0L,1L,0L,0L,1L,0L,0L,0L, 0L,1L,2L,1L,1L,0L,0L,0L,0L,1L,0L,0L,0L,0L,0L,0L, 0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L, 0L,0L,1L,0L,0L,1L,0L,0L,2L,0L,0L,0L,0L,1L,2L,0L, 0L,0L,1L,0L),. Dim = c(17L,17L),. Dimnames = list(NULL,c(“V1”, “V2”,“V3”,“V4”,“V5”,“V6”,“V7”,“V8”,“V9”,“V10”,“V11”, “V12”,“V13”,“V14”,“V15”,“V16”,“V17”)))

  

dput(社区)

结构(c(11L,12L,13L,14L,15L,10L,5L,9L,1L,16L,7L, NA,NA,0L,NA,4L,NA,NA,8L,NA,3L,NA,NA,6L,NA,NA,NA, NA,2L,NA),. Dim = 5:6,.Dimnames = list(NULL,c(“V1”,“V2”, “V3”,“V4”,“V5”,“V6”)))

要求

library(igraph) adj.matrix <- read.csv(file = "D:\\communityDetection\\adj.csv", header = FALSE, sep = ",") adj.matrix<- as.matrix(adj.matrix) communities <- read.csv(file = "D:\\communityDetection\\communities.csv", header = FALSE, sep = ",") g <- graph_from_adjacency_matrix(adj.matrix) plot(g) 结果,所以这里是

编辑:

dput()

到目前为止,我已编写此代码,但有些顶点填充了错误的颜色

like in this image

7应该像11一样是绿色的,因为它们在同一个社区但不是,是否有任何语法上的差异。节点也是分开的,有没有办法将相同颜色的节点显示在一起。

0 个答案:

没有答案