1

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...

md1630
  • 841
  • 1
  • 10
  • 28

1 Answers1

2

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)
lukeA
  • 53,097
  • 5
  • 97
  • 100