数据和代码获取:请查看主页个人信息!!!
载入R包
library('networkdata') library('ggraph') library('igraph') library('graphlayouts') library('ggforce')
分区域网络图
xy <- layout_as_multilevel(multilvl_ex, type = "all", alpha = 25, beta = 45) ggraph(multilvl_ex, "manual", x = xy[, 1], y = xy[, 2]) + geom_edge_link0( aes(filter = (node1.lvl == 1 & node2.lvl == 1)), edge_colour = "firebrick3", alpha = 0.5, edge_linewidth = 0.3 ) + geom_edge_link0( aes(filter = (node1.lvl != node2.lvl)), alpha = 0.3, edge_linewidth = 0.1, edge_colour = "black" ) + geom_edge_link0( aes(filter = (node1.lvl == 2 & node2.lvl == 2)), edge_colour = "goldenrod3", edge_linewidth = 0.3, alpha = 0.5 ) + geom_node_point(aes(shape = as.factor(lvl)), fill = "grey25", size = 3) + scale_shape_manual(values = c(21, 22)) + theme_graph() + coord_cartesian(clip = "off", expand = TRUE) + theme(legend.position = "none")
这段代码使用
ggraph
包创建了一个多级网络图。其中:
ggraph()
函数创建了基本的图形,并指定了使用手动布局。
geom_edge_link0()
函数用于绘制边,通过filter
参数指定了不同的边的过滤条件,并设置了颜色、透明度和线宽。
geom_node_point()
函数用于绘制节点,通过lvl
参数指定了节点的级别,设置了节点的形状、填充颜色和大小。
scale_shape_manual()
函数手动设置了节点形状的值。<