[英]Is there an easy way to color network nodes by degree in igraph for R?
使用用於 R 的igraph
package,我想按其度數為網絡的節點着色。 colors 應該表示從網絡中觀察到的最低程度到最高程度的梯度,例如從藍色到紅色,或從黃色到紅色。
我在這里使用函數setNames
和colorRampPalette
找到了一個可行的解決方案。
盡管如此,是否還有其他解決方案,使用 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)
你看? 顏色漸變與度數漸變不匹配。
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)
如果您想檢查哪些 colors 已分配給哪些節點:
k <- as_data_frame(g, what = "vertices")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.