R Traps

This page contains the common catches when using the igraph R package.

Dealing with node names

Given a igraph object g, one way to get the node names is to write V(g). While this will display the node names, you can't use it as a vector. To get a character vector of node names use get.vertex.attribute(g, 'name') or V(g)$name

Having gotten a vector node names, remember that many igraph functions will take a vertex id when specifying a vertex. But vertex id's start from 0. Thus if you use the index of the node name directly as a vertex id, it will be off by 1. You need to subtract 1.


> g <- graph.atlas(1000)        
> V(g)$name <- c("Alfred","Benny","Cristina","Delphine","Emily","Frank")        
> V(g)$name <- c("Alfred","Benny","Cristina","Delphine","Emily","Frank","Gus")        
> V(g)$name        
[1] "Alfred"   "Benny"    "Cristina" "Delphine" "Emily"    "Frank"    "Gus"

V(g)$name is an R object, starting with 1 :

> V(g)$name[1]        
[1] "Alfred"

but V(g) is an igraph object, starting with 0 :

> V(g)[0]        
Vertex sequence:
[1] "Alfred"


> V(g)[0]$name    
[1] "Alfred"

Dealing with node names when plotting

Node names are not automatically plotted. Name attribute can be reached with $name while what appears in the plot is the label attribute : $label. If you want to plot the names, simply type the following and then plot the graph.

V(g)$label <- V(g)$name
Unless otherwise stated, the content of this page is licensed under GNU Free Documentation License.