在情节中,我需要为两个特定社区着色。采取以下数据框:
A B C D E F G
A 0 1 0 1 0 1 0
B 1 0 1 1 0 1 0
C 0 1 0 0 0 0 0
D 1 1 0 0 1 1 0
E 0 0 0 1 0 1 0
F 1 1 0 1 1 0 1
G 0 0 0 0 0 1 0
ob <- read.csv("...ties.csv",sep = ",", header = TRUE, row.names = 1)
m <- as.matrix(ob)
g <- graph.adjacency(m, mode="undirected", weighted = T, add.rownames = T)
首先,我使用edge.betweenness
检测我的图表g的社区(com):
com <- edge.betweenness.community(g)
V(g)$memb <- com$membership
此操作产生了许多社区,com [[1]],com [[2]]等。我使用以下代码绘制结果图 - 每个社区一种颜色 - :
plot(g, vertex.color=membership(com))
现在,我如何只为两个选定的社区着色,比如com [[1]]和com [[2]],保持其余节点的同质性?
答案 0 :(得分:0)
我不得不调整你的邻接矩阵,以便超过1个社区出现。
library(igraph)
ob <- read.table(text="
A B C D E F G
A 0 1 0 1 0 1 0
B 1 0 1 1 0 1 0
C 0 0 0 0 0 0 0
D 1 1 0 0 1 0 0
E 0 0 0 1 0 1 1
F 0 1 0 0 1 0 1
G 0 0 0 0 1 1 0", header=TRUE)
m <- as.matrix(ob)
g <- graph.adjacency(m, mode="undirected", weighted = T, add.rownames = T)
com <- edge.betweenness.community(g)
V(g)$memb <- com$membership
cols <- membership(com)
cols[cols!=3] <- 1
plot(g, vertex.color=cols)