更新
如果要显示集群标签,可以使用membership
+components
添加此属性,如下所示
set.seed(1)
do.call(
graph.disjoint.union,
lapply(
rpois(30, 1) + 1,
make_full_graph
)
) %>%
set_vertex_attr(name = "names", value = seq(vcount(.))) %>%
set_vertex_attr(name = "cluster_label", value = membership(components(.))) %>%
as_tbl_graph(directed = FALSE)
这使
# A tbl_graph: 62 nodes and 49 edges
#
# An undirected simple graph with 30 components
#
# Node Data: 62 x 2 (active)
names cluster_label
<int> <dbl>
1 1 1
2 2 2
3 3 2
4 4 3
5 5 3
6 6 4
# ... with 56 more rows
#
# Edge Data: 49 x 2
from to
<int> <int>
1 2 3
2 4 5
3 6 7
# ... with 46 more rows
您可以使用disjoint.union
+make_full_graph
如下所示(假设您有 3 个完全连接的组件,分别具有 1、2 和 3 个节点)
library(tidygraph)
library(igraph)
do.call(
graph.disjoint.union,
lapply(
1:3,
make_full_graph
)
) %>%
set_vertex_attr(name = "names", value = seq(vcount(.))) %>%
as_tbl_graph(directed = FALSE)
这给了你
# A tbl_graph: 6 nodes and 4 edges
#
# An undirected simple graph with 3 components
#
# Node Data: 6 x 1 (active)
names
<int>
1 1
2 2
3 3
4 4
5 5
6 6
#
# Edge Data: 4 x 2
from to
<int> <int>
1 2 3
2 4 5
3 4 6
# ... with 1 more row
关于 的使用,如果您替换为,rpois(30,1)+1
也许这会有所帮助,例如,1:3
rpois(30,1)+1
set.seed(1)
do.call(
graph.disjoint.union,
lapply(
rpois(30,1)+1,
make_full_graph
)
) %>%
set_vertex_attr(name = "names", value = seq(vcount(.))) %>%
as_tbl_graph(directed = FALSE)
并使用以下信息生成图表
# A tbl_graph: 62 nodes and 49 edges
#
# An undirected simple graph with 30 components
#
# Node Data: 62 x 1 (active)
names
<int>
1 1
2 2
3 3
4 4
5 5
6 6
# ... with 56 more rows
#
# Edge Data: 49 x 2
from to
<int> <int>
1 2 3
2 4 5
3 6 7
# ... with 46 more rows