简体   繁体   中英

R igraph: Color nodes by degree?

# Erdos
par(mfrow=c(1,2))
g <- erdos.renyi.game(100, 1/100)
V(g)$size<-seq(0.05,5,0.05)
betweenness(g)

# Draw nodes and save positions
locs <- layout.fruchterman.reingold(g)
plot(g, 
     layout=locs, 
     vertex.label=NA, 
     main="Original",
     vertex.color=degree(g))
g

vertex.color=degree(g)

did not work. Could anyone tell me how to color the vertices by "degree"? Red (high value) to blue (low value) would be perfect.

Thanks!

A solution I found is to create a new color vector with the grey color R provides us with colors()[] . If you check colors()[] in your terminal, you can see the full list of colors that are readable by the plot.igraph() function.

You first charge your data (graph, etc.) :

edgelist <- read.csv(...)
graph <- make_graph_from_data(edgelist)

Then you create a vector of colors that corresponds to the length of your vertices list :

length(V(g)) # with a length of X vertices :

colors <- c(paste0(rep("grey",X),seq(X,1)))

Finally, you plot it with the attribute vertex.color :

  plot(g,vertex.color=colors[degree(graph)])

However, one can only use this little trick for graph with less than 100 values in degree(graph) ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM