Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Color pallette for vertices in igraph network in R

Tags:

r

igraph

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?

like image 879
tchaymore Avatar asked Sep 06 '25 03:09

tchaymore


2 Answers

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.

like image 198
Ernest Aigner Avatar answered Sep 07 '25 16:09

Ernest Aigner


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

like image 39
IRTFM Avatar answered Sep 07 '25 17:09

IRTFM



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!