【问题标题】:Individual coloring of single communities单个社区的单独着色
【发布时间】:2017-06-05 23:59:17
【问题描述】:

我有这段代码

library(igraph)
library(igraphdata)

data("karate")

g <- karate

# for reproducibility
set.seed(23548723)

network_layout <- layout_with_fr(g)
trimmed_network <- delete.edges(g, which(E(g)$weight < 4))
communities <- cluster_louvain(trimmed_network)

plot(communities, trimmed_network, layout=network_layout)

它会生成

我想禁用单顶点社区 (length 1) 中顶点的着色(color="white"mark.groups=NULL),我知道您可以操纵“正常”图的颜色通过使用$color,但我在igraph 文档中没有找到任何提示如何按社区处理它。

还有不使用社区绘图的选项

plot(trimmed_network, ...)

因此使用图表的颜色,但我会松开组标记。

如何根据length(communities[1]) == 1更改每个社区的颜色和组标记

【问题讨论】:

    标签: r colors cluster-analysis igraph


    【解决方案1】:

    识别每个组中 > 1 的顶点并将它们作为列表传递给mark.groups。这有点繁琐,但确实有效。

    r <- rle(sort(communities$membership))
    x <- r$values[which(r$lengths>1)]
    y <- r$values[which(r$lengths==1)]
    cols <- communities$membership
    cols[which(cols %in% y)] <- "white"
    grps <- lapply(x, function(x) communities[[x]])
    grps <- lapply(1:length(grps), function(x) which(V(g)$name %in% grps[[x]]))
    plot(communities, trimmed_network, layout=network_layout, 
     col = cols, mark.groups = grps)
    

    【讨论】:

    • 感谢您的回复。使用rle 的一个非常有趣的方法。 +1 用于解决组标记,但不能接受这一点,因为它缺少单个顶点颜色禁用。
    • 我完全忽略了那部分。我的错。好吧,我将编辑响应并添加代码以将这些隔离顶点着色为白色。为了完整起见:)。
    【解决方案2】:

    我们需要找到只有一个成员的社区的数字标识符,并将这些单身社区的成员的颜色设置为“白色”。

    # Get community membership
    memb = membership(communities)
    
    # Find number of members in each community
    tab = table(memb)
    
    # Set colors for each member. (Adjust these as desired)
    col = colors()[2:(length(memb)+1)]
    
    # But for members of communities of one, set the color to white
    singles = which(memb %in% as.numeric(names(tab)[tab==1]))
    col[singles] = "white"
    
    plot(communities, trimmed_network, layout=network_layout, col=col, mark.groups=NULL)
    

    【讨论】:

    • 感谢您的回复。拥有length &gt; 1 的社区中的mark.group 呢?
    • 对不起,我错过了那部分。看起来@paqmo 已经覆盖了它。
    • 遗憾的是这样我不能接受你的任何一个答案,他没有涵盖你解决的部分:D,但采取+1
    猜你喜欢
    • 1970-01-01
    • 2017-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-28
    • 2020-11-23
    相关资源
    最近更新 更多