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.
There are 2 ways to do this:
When you create the graph object, assign the names to a vertex attribute called
label. This is the default thatplot.igraph()looks for when plotting.Use the
Viterator to extract thenamevertex attribute, which is how they are stored if you useadd.colnames=NULL.Either way will give you your desired result. Something like: