gpt4 book ai didi

r - R中igraph网络中顶点的调色板

转载 作者:行者123 更新时间:2023-12-04 11:23:40 26 4
gpt4 key购买 nike

我正在使用 `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)

我正在尝试添加一个颜色渐变,根据节点的中心性为节点着色。我尝试了几个不同版本的代码,首先来自这个 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)

奇怪的是,在我运行了第一段代码之后,我似乎无法在网络上运行 plot 而不会出现相同的错误——即使我删除了对 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)]   

(在没有可重现示例的情况下未经测试。)

关于r - R中igraph网络中顶点的调色板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36852821/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com