Rnaldinho
Rnaldinho

Reputation: 431

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!

Upvotes: 2

Views: 1907

Answers (1)

PGCD
PGCD

Reputation: 1

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)...

Upvotes: 0

Related Questions