Color pallette for vertices in igraph network in R

二次信任 提交于 2019-12-14 01:16:18

问题


I'm plotting network graph in R using `igraph1. The network has 1,380 nodes and about 150k edges. Here's the summary from igraph:

IGRAPH UN-- 1380 159718 -- 
+ attr: name (v/c), rels (v/n), label (v/n), degree (v/n), btw (v/n), color (v/c), rels (e/n)

I'm trying to add a color gradient that colors the nodes based on their centrality. I've tried a couple of different versions code, first from this example:

# calculate betweenness
V(g_yearly)$btw <- betweenness(g_yearly)

# construct color palette
fine <- 100
palette <- colorRampPalette(c('blue','green'))

# assign palette to nodes
V(g_yearly)$color <- palette(fine) [as.numeric(cut(V(g_yearly)$btw,breaks=fine))]

# plot network 
plot.igraph(g_yearly,vertex.shape="circle",vertex.size=1,vertex.label.cex=0.6,layout=lgl(g_yearly),edge.arrow.size=0,edge.width=E(g_yearly)$rels/50)

I get the following error and traceback:

Error in seq.int(0, 1, length.out = n) : 
  'length.out' must be a non-negative number 
  9 .approxfun(x, y, v, method, yleft, yright, f) 
  8 palette[[1L]](x) 
  7 cbind(palette[[1L]](x), palette[[2L]](x), palette[[3L]](x), if (alpha) palette[[4L]](x)) 
  6 pmin(rgb, 1) 
  5 pmax(pmin(rgb, 1), 0) 
  4 roundcolor(cbind(palette[[1L]](x), palette[[2L]](x), palette[[3L]](x), 
if (alpha) palette[[4L]](x))) 
  3 ramp(seq.int(0, 1, length.out = n)) 
  2 palette(palette) 
  1 plot.igraph(g_yearly, vertex.shape = "circle", vertex.size = 1, 
vertex.label.cex = 0.6, layout = layout.lgl(g_yearly), edge.arrow.size = 0, 
edge.width = E(g_yearly)$rels/50) 

In addition: Warning message:
In .approxfun(x, y, v, method, yleft, yright, f) :
NAs introduced by coercion

If I use the rainbow function instead, this works just fine:

V(g_yearly)$color <- rainbow(V(g_yearly)$btw,start=3/6,end=4/6)

Oddly, after I've run the first bit of code once, I can't seem to run plot on the network without getting the same error -- even if I remove the calls to palette.

What am I doing wrong with palette?


回答1:


I think the problems is in this line:

# assign palette to nodes
V(g_yearly)$color <- palette(fine [as.numeric(cut(V(g_yearly)$btw,breaks=fine))]

The argument to palette should be single integer and fine is a vector of length 1 so attempting to index it with anything other than 1 will fail. Try:

V(g_yearly)$color <- palette(fine)[ as.numeric( cut(V(g_yearly)$btw, breaks=fine)]   

(Untested in the absence of a reproducible example.)




回答2:


I had the same issues but found the problem. When you do

palette <- colorRampPalette(c('blue','green'))

you redefine the 'palette' function, and so igraph later produces as error (I assume igraph uses 'palette' in one or the other way.

This also explains why the error remains when

palette <- colorRampPalette(c('blue','green'))

has been done once.




回答3:


In error traceback you have line: 2 palette(palette). I think, that you overwrite variable, try: pale <- colorRampPalette(c('blue','green'))



来源:https://stackoverflow.com/questions/36852821/color-pallette-for-vertices-in-igraph-network-in-r

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