How to specify vertex labels in R

I have a matrix as shown below:

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

Now I'm 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"); 

Vertex labels are id, so my question is how to set vertex labels by dimnames matrix?

I tried the code

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

but get the wrong schedule.

+6
source share
1 answer

There are two ways to do this:

  • When creating a chart object, name the attribute of the label vertex. This is the default value that plot.igraph() performs when plotting.

     g1 <- graph.adjacency(adjm,add.colnames='label') 
  • Use the V iterator to retrieve the vertex name attribute, which is 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) 

In any case, you will get the desired result. Sort of:

enter image description here

+10
source

Source: https://habr.com/ru/post/902903/


All Articles