2014-10-06 83 views
2

我有這樣一個數據幀: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 2 
2 3 
4 5 
.... 

現在,我使用的庫的igraph使用下面的代碼繪製此圖中的R代碼我得到了圖形的圖和形成最大集團的值。但是,如果我想要那個實際最大派系的陰謀,我該怎麼做? 謝謝!

回答

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) 

劇情1(原始圖):

Graph 1

劇情2(集團):

Clique


編輯:

作爲正確地指出通過@GaborCsardi,沒有必要到子集的圖形,因爲一個集團是一個完整的曲線圖。這可能是比induced.subgraph更高效:

g2 <- graph.full(length(clique1)) 
V(g2)$name <- V(g1)$name[clique1] 
+3

你實際上並不需要創建導出子,拉幫結派僅僅是一個完整的圖形,所以只需要創建一個完整的圖形,並設置其頂點名稱。這實際上也意味着,繪製派系毫無意義。您在劇情中傳達的信息是什麼? – 2014-10-06 20:02:03

+0

@GaborCsardi:正確,已編輯謝謝;) – digEmAll 2014-10-06 20:14:12