使用igraph
R 包,我想按度数对网络节点进行着色。颜色应代表渐变,例如从蓝色到红色,或从黄色到红色,从网络中观察到的最低程度到最高程度。
我找到了一个可行的解决方案 https://stackoverflow.com/questions/40891576/r-igraph-color-nodes-by-degree这里使用函数setNames
and colorRampPalette
.
尽管如此,是否还有其他解决方案,使用 R 调色板,例如heat.colors
or brewer.pal
?
让我举个例子,模拟我正在分析的网络类型:
g <- sample_bipartite(8, 20, type = "gnm", m = 29)
degrees <- degree(g)
colors <- heat.colors(length(unique(degrees)))
plot(g,
vertex.color = colors)
你看?颜色渐变与程度渐变不匹配。
颜色的顺序如何与从最低到最高的程度顺序相匹配?
非常感谢!
我不确定这是最好的方法,特别是当你有很多值或连续中心性(如果网络被加权)的情况。这种情况下,最好有间隔。
但是,如果您的数据与示例中的数据相似,这可能会有所帮助:
library(igraph)
g <- sample_bipartite(8, 20, type = "gnm", m = 29)
V(g)$degree <- degree(g)
#' maybe it s better to have a full sequence, instead of the unique values,
#' in case you have large gaps in between the unique degree values?
#' If not, can use unique values, as in your example
uni_all <- seq( min(V(g)$degree), max(V(g)$degree))
colors <- data.frame( color = heat.colors(length(uni_all), rev = T),
levels = uni_all)
# Use match to get the index of right color, matching on levels
V(g)$color <- colors$color[match(V(g)$degree, colors$levels)]
# use degree as labels
V(g)$label <- V(g)$degree
plot(g)
如果您想检查哪些颜色已分配给哪些节点:
k <- as_data_frame(g, what = "vertices")
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)