【问题标题】:Coloring edges based on node label (edges connecting similar and dissimilar colored differently)基于节点标签着色边缘(连接相似和不同颜色的边缘不同)
【发布时间】:2019-06-28 08:29:15
【问题描述】:

我有一个基于二进制邻接矩阵构建的 300 个节点的无向​​网络。 50 个节点标记为minority,其余的标记为majority。我想根据它们连接的节点设置边缘颜色(和属性):within.minowithin.majobetween.minomajo

我已经看到了基于this 等节点之一为边缘着色的方法,但这不是我的问题。我也尝试过this solution,但无法适应我的问题。

这是一个最小的可重现示例:

library(igraph)

# making the binary matrix
set.seed(10)
m.non_sym <- matrix(sample(0:1, 7, replace=TRUE), 10, 10)

# making it symmetrical
m.lower <- lower.tri(m.non_sym)*m.non_sym
m <- m.lower + t(m.lower)
diag(m) <- 0

# making the graph
g <- m %>% graph_from_adjacency_matrix(mode="undirected")

# assigning labels
V(g)$partition <- c(rep("minority", 4),
                    rep("majority", 6))

# plotting the graph
g %>% plot(vertex.size=10,
           vertex.color=c("skyblue", "pink")[1+(V(g)$partition=="majority")],
           edge.width = 3)

我想根据连接到的节点类型在边缘上分配以下标签:

【问题讨论】:

    标签: r igraph network-analysis


    【解决方案1】:

    您可以使用%--% 选择器查找满足您条件的边并设置它们的color。试试看:

    # select edges and set color 
    E(g)[V(g)[partition == "minority"] %--% V(g)[partition == "minority"]]$color <- "blue" # within.mino
    E(g)[V(g)[partition == "minority"] %--% V(g)[partition == "majority"]]$color <- "red" # between.minomajo
    E(g)[V(g)[partition == "majority"] %--% V(g)[partition == "majority"]]$color <- "yellow" # within.majo
    # plot
    g %>% plot(vertex.size = 10,
               vertex.color = c("skyblue", "pink")[1 + (V(g)$partition == "majority")],
               edge.width = 3)
    

    如果需要,您也可以使用label,而不是color

    E(g)[V(g)[partition == "minority"] %--% V(g)[partition == "minority"]]$label <- "within.mino"
    E(g)[V(g)[partition == "minority"] %--% V(g)[partition == "majority"]]$label <- "between.minomajo"
    E(g)[V(g)[partition == "majority"] %--% V(g)[partition == "majority"]]$label <- "within.majo"
    
    g %>% plot(vertex.size = 10,
               vertex.color = c("skyblue", "pink")[1 + (V(g)$partition == "majority")],
               edge.width = 3)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      • 2011-03-31
      • 1970-01-01
      • 2013-07-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多