R Igraph Rename Vertices

R igraph rename vertices

You can use

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

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

Vertex Labels in igraph R

You can specify the labels like this:

 library(igraph)
gg <- graph.data.frame(
links,directed=FALSE,
vertices = rbind(
setNames(links[,c(1,4)],c("id","label")),
setNames(links[,c(2,5)], c("id","label"))))
plot(gg, vertex.color = 'lightblue', edge.label=links$value,
vertex.size=1, edge.color="darkgreen",
vertex.label.font=1, edge.label.font =1, edge.label.cex = 1,
vertex.label.cex = 2 )

Sample Image

You could also pass

merge(rbind(
setNames(links[,c(1,4)],c("id","label")),
setNames(links[,c(2,5)], c("id","label"))),
nodes,
by.x="label", by.y="name")

to the vertices argument if you needed the other node attributes.

Data:

 links <- read.table(header=T, text="
source target value sourceID targetID
1 3 4 0.6245 1450552 1519842
2 6 8 0.5723 2607133 3051992
3 9 7 0.7150 3101536 3025831
4 0 1 0.7695 401517 425784
5 2 5 0.5535 1045501 2258363")

nodes <- read.table(header=T, text="
name group size
1 401517 1 8
2 425784 1 8
3 1045501 1 8
4 1450552 1 8
5 1519842 1 8
6 2258363 1 8
7 2607133 1 8
8 3025831 1 8
9 3051992 1 8
10 3101536 1 8")

Calling vertices in Igraph

to works only with edge sequences; e.g., E(g)[to(1)] gives you all the edges that point to vertex 1. IMHO this is quite logical since vertices do not "point" anywhere (the edges do) so it does not make sense to use from or to.

Also, the "official" way of using nei is nei(1, "out") and not nei(1, "to") although it could be the case that "to" works as well. You can use outnei(1) as well.

Disclaimer: I am one of the authors of igraph although I did not write the R interface so there might be a better reason than the one I explained above.

Alter X and Y coordinates of graph vertices in iGraph

From the igraph documentation, it says that one of the limitations of its ability to read GML is

Only node and edge attributes are used, and only if they have a simple
type: integer, real or string. So if an attribute is an array or a
record, then it is ignored. This is also true if only some values of
the attribute are complex.

Which is probably why it's having trouble with the graphics attribute. igraph should be able to read graphml format as well, but likely encounters the same hurdle.

Luckily, if you need a workaround, GML is a human-readable format that is relatively easy to parse out. If you just need the x and y coordinates of the nodes for example, you can do something like this:

library(igraph)

g <- read_graph("graph.gml", format="gml") #create graph object

z <- readLines("graph.gml") #create character vector from which to grep out attributes

id <- trimws(gsub("\t\tid\t", " ", z[grep("\t\tid\t", z)])) #retrive node ids
totid <- length(id) #retrieve number of nodes

#grep for and extract x and y attributes
xcor <- as.numeric(trimws(gsub("\t\t\tx\t", " ", z[grep("\t\t\tx\t", z)])))[1:totid]
ycor <- as.numeric(trimws(gsub("\t\t\ty\t", " ", z[grep("\t\t\ty\t", z)])))[1:totid]

#edges will also have x and y coordinates, so we need to index by the number of nodes

#add attributes back into graph object
g <- set.vertex.attribute(g, "x", value=xcor)
g <- set.vertex.attribute(g, "y", value=ycor)


Related Topics



Leave a reply



Submit