识别R中的派系

时间:2014-10-06 18:40:49

标签: r graph igraph clique

我有一个这样的数据框:

1 2
2 3
4 5
....

现在,我使用以下代码使用库igraph在R中绘制此图:

wt=read.table("NP7.txt")
wt1=matrix(nrow=nrow(wt), ncol=2)     
wt1=data.frame(wt1)
wt1[,1:2]=wt[,1:2]      
write.table(wt1,"test.txt")
library(igraph)
wt=read.table("test.txt")
wg7 <- graph.edgelist(cbind(as.character(wt$X1), as.character(wt$X2)),
                 directed=F)
sum(clusters(wg7)$csize>2)        
plot(wg7)
a <- largest.clique(wg7)

现在,在运行此代码时,我得到了图形图和构成最大集团的值。但是,如果我想要那个真正最大的集团的情节,我该怎么做? 谢谢!

1 个答案:

答案 0 :(得分:4)

以下是一个例子:

library(igraph)

# for reproducibility of graphs plots (plot.igraph uses random numbers)
set.seed(123)

# create an example graph
D <- read.table(header=T,text=
'from to
A B
A C
C D
C F
C E
D E
D F
E F')

g1 <- graph.data.frame(D,directed=F)

# plot the original graph
plot(g1)

# find all the largest cliques (returns a list of vector of vertiex ids)
a <- largest.cliques(g1)

# let's just take the first of the largest cliques
# (in this case there's just one clique)
clique1 <- a[[1]]

# subset the original graph by passing the clique vertices
g2 <- induced.subgraph(graph=g1,vids=clique1)

# plot the clique
plot(g2)

Plot 1(原始图表):

Graph 1

Plot 2(clique):

Clique


编辑:

正如@GaborCsardi正确指出的那样,由于clique是一个完整的图,因此没有必要对图进行子集化。这可能比induced.subgraph

更有效
g2 <- graph.full(length(clique1))
V(g2)$name <- V(g1)$name[clique1]