基于节点标签的着色边缘(连接相似和不同颜色的边缘不同)
Coloring edges based on node label (edges connecting similar and dissimilar colored differently)
我有一个基于二进制邻接矩阵构建的 300 个节点的无向网络。 50 个节点标记为 minority
,其余节点标记为 majority
。我想根据它们连接的节点设置边颜色(和属性):within.mino
、within.majo
和 between.minomajo
.
我已经看到基于节点之一着色边的方法,例如 this but this is not my problem. I also have tried ,但无法使其适应我的问题。
这是一个最小的可重现示例:
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)
我想根据它们连接到的节点类型在边上分配以下标签:
您可以使用 %--%
选择器查找满足您条件的边并设置它们 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)
我有一个基于二进制邻接矩阵构建的 300 个节点的无向网络。 50 个节点标记为 minority
,其余节点标记为 majority
。我想根据它们连接的节点设置边颜色(和属性):within.mino
、within.majo
和 between.minomajo
.
我已经看到基于节点之一着色边的方法,例如 this but this is not my problem. I also have tried
这是一个最小的可重现示例:
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)
我想根据它们连接到的节点类型在边上分配以下标签:
您可以使用 %--%
选择器查找满足您条件的边并设置它们 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)