如何解决有没有一种简单的方法可以在 R 的 igraph 中按程度为网络节点着色?
使用 R 的 igraph
包,我想按度数为网络节点着色。颜色应该代表一个渐变,例如从蓝色到红色,或从黄色到红色,从网络中观察到的最低到最高程度。
我在这里使用函数 setNames
和 colorRampPalette
找到了 a working solution。
尽管如此,难道没有其他解决方案,使用 R 调色板,例如 heat.colors
或 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")
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。