Coloring vertexes according to their centrality

放肆的年华 提交于 2019-12-05 02:19:09

问题


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.


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/27004167/coloring-vertexes-according-to-their-centrality

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!