[英]Color pallette for vertices in igraph network in R
I'm plotting network graph in R using `igraph1. 我正在使用`igraph1在R中绘制网络图。 The network has 1,380 nodes and about 150k edges.
该网络有1,380个节点和大约150k边缘。 Here's the summary from igraph:
以下是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: 如果我使用
rainbow
功能,这很好用:
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
. 奇怪的是,在我运行第一段代码之后,我似乎无法在网络上运行绘图而不会出现相同的错误 - 即使我删除了对
palette
的调用。
What am I doing wrong with palette
? 我在
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. palette
的参数应该是单个整数, fine
是长度为1的向量,因此尝试使用除1
之外的任何内容对其进行索引将失败。 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. 你重新定义'调色板'功能,因此igraph后来产生错误(我假设igraph使用'调色板'以一种或另一种方式。
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). 在错误回溯中,您有第2行调色板(调色板)。 I think, that you overwrite variable, try: pale <- colorRampPalette(c('blue','green'))
我想,你覆盖变量,试试: 苍白 < - colorRampPalette(c('blue','green'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.