r igraph-如何基于顶点ID向顶点添加标签 [英] r igraph - how to add labels to vertices based on vertex id

查看:461
本文介绍了r igraph-如何基于顶点ID向顶点添加标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个igraph,其中每个顶点都有一个顶点ID和一个名称.我希望这些顶点仍通过其顶点ID进行标识,但要通过其名称进行标记.看起来当通过V(g)$ label<-名称将标签添加到顶点时,名称必须是按顺序排列的.有没有办法放入命名矢量或数据框,以便根据其ID对顶点进行命名?

I have an igraph where each vertex has both a vertex ID and a name. I want the vertices to still be identified by their vertex ID's, but to be labeled by their names. It seems like when adding labels to vertices through V(g)$label <- names, the names have to be in order. Is there a way to maybe put in a named vector or dataframe that names the vertices based on their IDs?

names <- c('A','B','C,','D')
from <- c(113,115,112,114,113)
to <- c(112,112,115,113,114)
structure <- data.frame("from" = from, "to" = to)
g <- graph.data.frame(structure)
V(g)$label <- names

我希望能够指定哪个顶点是A,哪个顶点是B,等等-即115是A,112是B ...

I want to be able to specify which vertex is A, which is B, etc - i.e. 115 is A, 112 is B...

推荐答案

这里是这样做的一种方法:

Here's one way to do so:

names <- c("114" = "Lisa", "115" = "Joe", "113" = "Peter", "112" = "Barbara", "113" = "Bart")
V(g)$label <- names[V(g)$name]
plot(g)

这篇关于r igraph-如何基于顶点ID向顶点添加标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆