r - 在 R 中识别派系

标签 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)

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

最佳答案

这是一个例子:

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]

关于r - 在 R 中识别派系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26222659/

相关文章:

r - Barabási模型参数如何设置

javascript - 在 R 中使 visnetwork 图的边缘变直

r - R 中是否有一种有效的方法将矩阵 M2 的每一行 "paste"到矩阵 M1 的每一行以获得所有可能的组合?

在数据框中的组内运行计数

c++ - 根据用户输入使用 C++ 创建图形

graph - 如何使用 gremlin 查找加权边范围内的所有节点?

R(igraph): orientation of edge beginning and ending from same vertex

r - 使用 grid.arrange 底部的通用 x 轴标签和图例

R:何时使用 stringsAsFactors

c# - 在城市使用公共(public)汽车的公共(public)交通