如何在断开的图中标注连接的组件?

时间:2018-11-21 22:51:41

标签: r igraph

我有一些由三个连接组件组成的断开连接图。此图由igraph R中的以下命令生成:

library(igraph)
x1 <- c(1:7, 2, 8:14, 10, 15:21, 18)
x2 <- c(rep(0, 7), 1, rep(0, 7), 1, rep(0, 7), 1)
m <- cbind(x1, x2)
g <- graph.formula(1-2, 2-3, 3-4, 4-5, 5-6, 6-7, 2-8,
                   9-10, 10-11, 11-12, 12-13, 13-14, 14-15, 11-16,
                   17-18, 18-19, 19-20, 20-21, 21-22, 22-23, 20-24)
plot(g, layout = m, rescale = F, xlim = c(0.5, 21.5), vertex.size = 20,
     vertex.label = NA, edge.color = "black", vertex.color = "black")

结果断开的关系图绘制如下: enter image description here

我想用字母标记每个断开的组件,例如“ A”,“ B”和“ C”。另外,我想为igraph R中的每个连接组件制作一些字幕。

1 个答案:

答案 0 :(得分:2)

使用components获取群集ID。要将标签在每个ID中水平居中,请使用tapply计算'm'中x值的中点。对于垂直位置,请使用y值的min和适当的偏移量。使用text添加标签。

m <- cbind(m, id = components(g)$membership)
xs <- tapply(m[ , "x1"], m[ , "id"], function(x) mean(range(x)))
ys <- tapply(m[ , "x2"], m[ , "id"], min)
plot(g, layout = m, rescale = F, xlim = c(0.5, 21.5), vertex.size = 20,
     vertex.label = NA, edge.color = "black", vertex.color = "black")
text(xs, ys - 0.6, LETTERS[1:3])

enter image description here