Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to specify the labels of vertices in R

Tags:

graph

r

I have an matrix as below:

          jerry    peter    king
 jerry     1       0        0    
 peter     0       1        0    
 king      1       1        1              

Now I am trying to draw a graph standing for the matrix with the code below:

t <- read.table("../data/table.dat");
adjm <- data.matrix(t);
g1 <- graph.adjacency(adjm,add.colnames=NULL);
plot(g1, main="social network", vertex.color="white", edge.color="grey", vertex.size=8,
     vertex.frame.color="yellow");

The labels of the vertices is the id, so my question is how do I set the label of the vertices by the dimnames of the matrix?

I have tried to the code

vertex.label=attr(adjm,"dimnames")

but get the wrong graph.

like image 496
jerry_sjtu Avatar asked Dec 02 '11 05:12

jerry_sjtu


People also ask

What are vertex labels?

Formally, given a graph G = (V, E), a vertex labelling is a function of V to a set of labels; a graph with such a function defined is called a vertex-labeled graph. Likewise, an edge labelling is a function of E to a set of labels.

What are vertices in R?

A vertex sequence is just what the name says it is: a sequence of vertices. Vertex sequences are usually used as igraph function arguments that refer to vertices of a graph.


1 Answers

There are 2 ways to do this:

  1. When you create the graph object, assign the names to a vertex attribute called label. This is the default that plot.igraph() looks for when plotting.

    g1 <- graph.adjacency(adjm,add.colnames='label')
    
  2. Use the V iterator to extract the name vertex attribute, which is how they are stored if you use add.colnames=NULL.

    plot(g1, main="social network", vertex.color="white", edge.color="grey", vertex.size=8, vertex.frame.color="yellow", vertex.label=V(g1)$name)
    

Either way will give you your desired result. Something like:

enter image description here

like image 51
John Colby Avatar answered Sep 19 '22 22:09

John Colby