I am trying to change the color of the vertexes in an igraph generated graph. To be more specific, I have a 95 nodes graph created from an adjacency matrix and I would like to color them according to their degree/betweenness/eigenvalue centrality/closeness but I'm guessing that after I know how to do it with one, I'll be able to do it with others.
So I've coded the basics of graph generation until now:
dataset <- read.csv("~/Google Drive/Cours M2/Network Economics/Data/Collabs_2013.csv", sep=";")
matrix<-as.matrix(dataset)
adj<-graph.adjacency(matrix)
plot(adj)
btw<-betweenness(adj,directed = FALSE)
I now have a vector of 95 values of betweennesses and I would like to plot a graph with a gradient of colors that follows the betweenness values (e.g. from red for the lowest value to green to the highest). I'm guessing I have to mess with vertex's attributes but I have no idea how to input the vector as a color attribute.
Seems like you already did most of the work. All you have to know is colorRamppalette
and setting the vertex.color
for the network. Assuming you have to change the colors linearly,
just do
fine = 500 # this will adjust the resolving power.
pal = colorRampPalette(c('red','green'))
#this gives you the colors you want for every point
graphCol = pal(fine)[as.numeric(cut(btw,breaks = fine))]
# now you just need to plot it with those colors
plot(adj, vertex.color=graphCol)
credits to this. I was using a much more inefficient method to assign the colors before answering this.
Just a note:
It can be problematic to define
palette = colorRampPalette(c('blue','green'))
as the 'palette' function, is also used by igraph, and so igraph later produces as error.
See problem Color pallette for vertices in igraph network in R
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With