【问题标题】:Is there an easy way to color network nodes by degree in igraph for R?是否有一种简单的方法可以在 R 的 igraph 中按度数对网络节点进行着色?
【发布时间】:2021-03-02 14:06:57
【问题描述】:

使用 R 的 igraph 包,我想按度数为网络节点着色。颜色应该代表一个渐变,例如从蓝色到红色,或从黄色到红色,从网络中观察到的最低到最高程度。

我在这里使用函数setNamescolorRampPalette 找到了a working solution

不过,是否还有其他解决方案,使用 R 调色板,例如 heat.colorsbrewer.pal

让我举个例子,模拟我正在分析的网络类型:

g <- sample_bipartite(8, 20, type = "gnm", m = 29)

degrees <- degree(g)

colors <- heat.colors(length(unique(degrees)))

plot(g,
     vertex.color = colors)

你看到了吗?颜色渐变与度数渐变不匹配。

颜色的顺序如何与度数的顺序相匹配,从最低到最高?

非常感谢!

【问题讨论】:

    标签: r network-programming data-visualization igraph color-palette


    【解决方案1】:

    我不确定这是不是最好的方法,尤其是在你有很多值的情况下,或者如果网络被加权,则为连续中心性。在这种情况下,最好有间隔。

    但是,如果您的数据与示例中的数据相似,这可能会有所帮助:

    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")
    

    【讨论】:

    • 非常感谢!这正是我所需要的。您的解决方案是通用且优雅的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-29
    • 1970-01-01
    • 2011-06-25
    • 1970-01-01
    • 2020-10-27
    • 1970-01-01
    相关资源
    最近更新 更多