可视化重要的顶点与边
该麻疹数据集有一个问题:有 3 位个体的感染来源未知。其中一位(顶点 184)最终似乎对将疾病传播给许多其他人负有重要责任,尽管他并未直接感染太多个体。然而,由于顶点 184 在网络中没有入边,其介数看起来较低。评估该顶点重要性的一种方法,是可视化从该个体向外扩散的测地距离。在本练习中,您将绘制从这位"零号病人"出发的这些距离。
本练习是课程的一部分
R 中的网络分析
练习说明
- 使用
make_ego_graph()创建只包含与顶点 184 相连顶点的网络子集。第 1 个参数是原始图g。第 2 个参数是任一顶点到目标顶点允许的最大连接数。在本例中,使用diameter()返回网络中的最长路径长度。第 3 个参数是我们的目标顶点,应为 184。最后一个参数是模式。在这里,您可以不考虑方向,包含所有连接。 - 创建对象
dists,其包含每个顶点到顶点 184 的测地距离。使用函数distances()计算。 - 为每个顶点设置属性
color。颜色将根据其测地距离选择。调色板colors的长度等于最大测地距离加 1。这样距离相同的顶点会用相同颜色绘制,且零号病人也有其专属颜色。 - 使用
plot()可视化网络g184。顶点标签应为测地距离dists。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Make an ego graph
g184 <- ___(g, ___(g), nodes = '184', mode = c("all"))[[1]]
# Get a vector of geodesic distances of all vertices from vertex 184
dists <- ___(g184, "184")
# Create a color palette of length equal to the maximal geodesic distance plus one.
colors <- c("black", "red", "orange", "blue", "dodgerblue", "cyan")
# Set color attribute to vertices of network g184.
V(___)$color <- colors[dists+1]
# Visualize the network based on geodesic distance from vertex 184 (patient zero).
___(___,
vertex.label = ___,
vertex.label.color = "white",
vertex.label.cex = .6,
edge.color = 'black',
vertex.size = 7,
edge.arrow.size = .05,
main = "Geodesic Distances from Patient Zero"
)