本文介绍了如何在断开的图中标记连接的组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have some disconnected graph composed of three connected components. This graph is produced by the following commands in the 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")

生成的断开连接图绘制如下:

The resulting disconnected graph is plotted below:

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

I want to label every disconnected components by a letter, for instance "A", "B" and "C". Alternatively, I want to make some subtitles for every connected components in the igraph R ?

推荐答案

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

Use components to get the cluster id. To center labels horizontally within each id, use tapply to calculate the midpoint of x values in 'm'. For the vertical position, use min of the y values and a suitable offset. Use text to add labels.

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])

这篇关于如何在断开的图中标记连接的组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 05:59