网络图: 如何对网络图进行分组展示

导入网络数据

1
2
data(karate)
karate
1
2
3
4
5
6
7
8
9
10
11
12
## IGRAPH 4b458a1 UNW- 34 78 -- Zachary's karate club network
## + attr: name (g/c), Citation (g/c), Author (g/c), Faction (v/n), name (v/c), label (v/c), color (v/n),
## | weight (e/n)
## + edges from 4b458a1 (vertex names):
## [1] Mr Hi --Actor 2 Mr Hi --Actor 3 Mr Hi --Actor 4 Mr Hi --Actor 5 Mr Hi --Actor 6 Mr Hi --Actor 7
## [7] Mr Hi --Actor 8 Mr Hi --Actor 9 Mr Hi --Actor 11 Mr Hi --Actor 12 Mr Hi --Actor 13 Mr Hi --Actor 14
## [13] Mr Hi --Actor 18 Mr Hi --Actor 20 Mr Hi --Actor 22 Mr Hi --Actor 32 Actor 2--Actor 3 Actor 2--Actor 4
## [19] Actor 2--Actor 8 Actor 2--Actor 14 Actor 2--Actor 18 Actor 2--Actor 20 Actor 2--Actor 22 Actor 2--Actor 31
## [25] Actor 3--Actor 4 Actor 3--Actor 8 Actor 3--Actor 9 Actor 3--Actor 10 Actor 3--Actor 14 Actor 3--Actor 28
## [31] Actor 3--Actor 29 Actor 3--Actor 33 Actor 4--Actor 8 Actor 4--Actor 13 Actor 4--Actor 14 Actor 5--Actor 7
## [37] Actor 5--Actor 11 Actor 6--Actor 7 Actor 6--Actor 11 Actor 6--Actor 17 Actor 7--Actor 17 Actor 9--Actor 31
## + ... omitted several edges

可视化网络数据

1
2
3
4
5
6
7
8
9
10
11
12
13
set.seed(123)
karateLayout <- layout_with_fr(karate)

# par是调节基础画图细节的函数
par(mar = c(0,0,2,0))
plot(karate,
vertex.size = 10,
vertex.color = "steelblue4",
edge.width = 1,
vertex.label = NA,
edge.color = "darkgrey",
layout = karateLayout,
main = "Zachary's karate club network" )

以群落为模块,设置组间距离,展示网络图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 提取模块分组信息
Communitykarate <- cluster_louvain(karate)
prettyColors <- c("turquoise4", "azure4", "olivedrab","deeppink4")
# 设置模块颜色
communityColors <- prettyColors[membership(Communitykarate)]

# 重新设置组间的weight
edge.weights <- function(community, network, weight.within = 100, weight.between = 1) {
bridges <- crossing(communities = community, graph = network)
weights <- ifelse(test = bridges, yes = weight.between, no = weight.within)
return(weights)
}
E(karate)$weight <- edge.weights(Communitykarate, karate)
karateLayoutA <- layout_with_fr(karate, karateLayout)

set.seed(123)
plot(x = Communitykarate,
y = karate,
edge.width = 1,
vertex.size = 10,
mark.groups = NULL,
layout = karateLayoutA,
vertex.label = NA,
col = communityColors,
c("darkgrey","tomato2")[crossing(Communitykarate, karate) + 1],
main = "Communities in Zachary's karate club network (grouped)")

修改组间距离大小

注:不知道怎么以监督方式设置分组展示,只能根据网络属性—模块设置分组,不知道大家有没有什么好的办法

1
2
3
4
5
6
7
8
9
10
11
12
E(karate)$weight <- edge.weights(Communitykarate, karate, weight.within = 1000)
karateLayoutB <- layout_with_fr(karate, karateLayout)
plot(x = Communitykarate,
y = karate,
edge.width = 1,
vertex.size = 10,
mark.groups = NULL,
layout = karateLayoutB,
vertex.label = NA,
col = communityColors,
c("darkgrey","tomato2")[crossing(Communitykarate, karate) + 1],
main = "Communities in Zachary's karate club network (grouped)")

以按照分组顺序在circle形状上展示数据

1
2
3
4
5
6
7
karate_groups <- cluster_optimal(karate) # cluster_fast_greedy(karate)
coords <- layout_in_circle(karate, order =
order(membership(karate_groups)))
V(karate)$label <- sub("Actor ", "", V(karate)$name)
V(karate)$label.color <- membership(karate_groups)
V(karate)$shape <- "none"
plot(karate, layout = coords)

R information

1
sessionInfo()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
## R version 4.0.2 (2020-06-22)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 19042)
##
## Matrix products: default
##
## locale:
## [1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252
## [3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
## [5] LC_TIME=English_United States.1252
## system code page: 936
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] igraphdata_1.0.1 igraph_1.2.5
##
## loaded via a namespace (and not attached):
## [1] rstudioapi_0.11 knitr_1.30 magrittr_1.5 tidyselect_1.1.0 R6_2.5.0 rlang_0.4.7
## [7] highr_0.8 stringr_1.4.0 dplyr_1.0.2 tools_4.0.2 xfun_0.19 htmltools_0.5.0
## [13] ellipsis_0.3.1 yaml_2.2.1 digest_0.6.25 tibble_3.0.3 lifecycle_0.2.0 crayon_1.3.4
## [19] purrr_0.3.4 vctrs_0.3.4 glue_1.4.2 evaluate_0.14 rmarkdown_2.5 stringi_1.5.3
## [25] compiler_4.0.2 pillar_1.4.6 generics_0.1.0 jsonlite_1.7.1 pkgconfig_2.0.3

参考

  1. 如何在igraph中进行分组展示

参考文章如引起任何侵权问题,可以与我联系,谢谢。


------------- The End Thanks for reading --------