簡體   English   中英

如何在斷開的圖中標記連接的組件?

[英]How to label connected components in a disconnected graph?

我有一些由三個連接的組件組成的斷開連接的圖。 此圖由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")

生成的斷開連接圖繪制如下: 在此處輸入圖片說明

我想用字母標記每個斷開的組件,例如“ A”,“ B”和“ C”。 另外,我想為igraph R中的每個連接組件制作一些字幕。

使用components獲取群集ID。 要將標簽在每個ID中水平居中,請使用tapply計算'm'中x值的中點。 對於垂直的位置,使用min y值的和合適的偏移量。 使用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])

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM