我有一个这样的数据框:
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)
现在,在运行这段代码时,我得到了图形的绘图和形成最大集团的值。但是,如果我想要那个真正最大的集团的阴谋,我该怎么做呢? 谢谢!
最佳答案
这是一个例子:
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(原始图):
情节 2(集团):
编辑:
正如@GaborCsardi 正确指出的那样,没有必要对图进行子集化,因为团是一个完整的图。这可能比 induced.subgraph
更有效:
g2 <- graph.full(length(clique1))
V(g2)$name <- V(g1)$name[clique1]
关于r - 在 R 中识别派系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26222659/