[英]Color pallette for vertices in igraph network in R
我正在使用`igraph1在R中繪制網絡圖。 該網絡有1,380個節點和大約150k邊緣。 以下是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)
我正在嘗試添加一個顏色漸變,根據它們的中心性為節點着色。 我嘗試了幾個不同版本的代碼,首先來自這個例子 :
# 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)
奇怪的是,在我運行第一段代碼之后,我似乎無法在網絡上運行繪圖而不會出現相同的錯誤 - 即使我刪除了對palette
的調用。
我在palette
做錯了什么?
我認為問題在於這一行:
# 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)]
(在沒有可重現的例子的情況下未經測試。)
我有同樣的問題,但發現了問題。 當你這樣做
palette <- colorRampPalette(c('blue','green'))
你重新定義'調色板'功能,因此igraph后來產生錯誤(我假設igraph使用'調色板'以一種或另一種方式。
這也解釋了為什么錯誤仍然存在
palette <- colorRampPalette(c('blue','green'))
做過一次。
在錯誤回溯中,您有第2行調色板(調色板)。 我想,你覆蓋變量,試試: 蒼白 < - colorRampPalette(c('blue','green'))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.