简体   繁体   中英

Color pallette for vertices in igraph network in R

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 ?

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

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.

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

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