问题
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