r igraph - how to add labels to vertices based on vertex id

◇◆丶佛笑我妖孽 提交于 2019-12-14 04:01:22

问题


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

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


回答1:


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)


来源:https://stackoverflow.com/questions/28909240/r-igraph-how-to-add-labels-to-vertices-based-on-vertex-id

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!