7
votes

Is there a possibility to rename the vertices in an igraph. I want to plot a certain graph multiple times with different notation on the vertices. Given the following igraph az:

> az
IGRAPH DN-- 24 23 -- 
+ attr: name (v/c), label (v/c), color (v/c), fill (v/c), width (e/n)

with

> V(az)
Vertex sequence:
 [1] "x1"  "x2"  "x3"  "x4"  "x5"  "x7"  "x8"  "x9"  "x10" "x11" "x12" "x13"
[13] "x14" "x15" "x16" "x19" "x20" "x21" "x22" "x23" "x24" "x25" "x26" "x27"

I want to change the vertices into, lets say to y1-y27 However,

V(az)$name <- paste("y",1:27,sep="")

is not working. How can I achieve this? Thanks in advance.

Cheers

EDIT: For the record.

V(az)$name <- paste("y",1:27,sep="")

works in that way, so that it returns:

 > V(az)
    Vertex sequence:
     [1] "y1"  "y2"  "y3"  "y4"  "y5"  "y7"  "y8"  "y9"  "y10" "y11" "y12" "y13"
    [13] "y14" "y15" "y16" "y19" "y20" "y21" "y22" "y23" "y24" "y25" "y26" "y27"

However, plot(az) stills return the graph with the x nodes

2
You wrote V(as) instead of V(az) is it normal ?dickoa
oh thanks for the hint. Thats a typo. It should be V(az)$name <- paste("y",1:27,sep="")Richard A. Schäfer
How do you know that it doesn't work ? there's an error message ?dickoa
Just edited the questionRichard A. Schäfer
I see but I'm sure that plot will work too. Check again. There's no reasondickoa

2 Answers

14
votes

You can use

ay <- set.vertex.attribute(az, "name", value=paste("y",1:27,sep=""))

Also works with "label" instead of "name".

10
votes

If V(az) has both a set of 'name' attributes and a set of 'label' attributes, it is the 'label' attributes that get plotted.

> gt <- graph.tree(24, children = 4, mode=c("out", "in", "undirected"))
> V(gt)$name <- letters[1:24]
> plot(gt)   # So 'name's get displayed if no label is present
> V(gt)$label <- LETTERS[1:24]
> plot(gt)    # Labels get displayed
> V(gt)$name <- letters[1:24]  # see if then get overwritten 
> plot(gt)    # Still plots with 'label's