2016-04-25 179 views
3

我正在使用`igraph1在R中繪製網絡圖。該網絡有1380個節點和約150k條邊緣。這是從igraph總結:R在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) 

我想添加一個顏色漸變,根據他們的中心顏色的節點。我已經試過了幾個不同版本的代碼,首先從這個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) 

我碰到下面的錯誤和回溯:

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 

如果我使用rainbow函數來代替,這工作得很好:

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

奇怪的是,我已經運行的代碼的第一位後一次,我似乎不能沒有得到同樣的錯誤運行網絡上的情節 - 即使I R請撥打電話palette

我在做什麼錯誤palette

+0

你是怎樣得到一個回溯?!? –

回答

1

我想的問題是在這條線:

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

palette的參數應該是一個整數,fine是一個長度爲1的向量因此試圖指數也比1任何其他將會失敗。嘗試:

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

(在不存在可再現的示例的未測試。)

+0

嗯,這是一個不幸的錯字,但我認爲這是在我的崗位而已。無論如何,我雙精度檢查和相同的結果。 – tchaymore

+0

您正在調用一個「示例」的鏈接上有代碼但沒有數據。所以這不是真的......一個例子。 –

+0

我會看看我是否可以共享一部分數據。這是一個聯合研究項目的一部分,數據是我的,但我不能完整分享。 – tchaymore

-1

在錯誤回溯你有行:2調色板(調色板)。 我認爲,您覆蓋變量,嘗試:蒼白 < - colorRampPalette(C( '藍', '綠色'))

1

我有同樣的問題,但發現了這個問題。當你做

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

您重新定義「調色板」的功能,所以IGRAPH後產生的錯誤(我假設的igraph使用「調色板」中的一種或另一種方式。

這也解釋了爲什麼錯誤當

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

已經做了一次依然存在。

+0

相關:https://github.com/igraph/rigraph/issues/252 – OganM