Окрашивание краев на основе метки узла (ребра, соединяющие одинаковые и разные цвета, по-разному) - PullRequest
1 голос
/ 28 июня 2019

У меня есть ненаправленная сеть из 300 узлов, построенная на основе двоичной матрицы смежности. 50 узлов помечены как minority, а остальные как majority. Я хочу установить цвета ребер (и атрибуты) на основе узлов, которые они соединяют: within.mino, within.majo и between.minomajo.

Я видел способы окрашивания краев на основе одного из узлов, таких как this , но это не моя проблема. Я также попробовал это решение , но не смог приспособить его к моей проблеме.

Вот минимальный воспроизводимый пример:

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)

Я хочу назначить следующие метки по краям в зависимости от того, к какому типу узлов они подключены:

enter image description here

1 Ответ

1 голос
/ 28 июня 2019

Вы можете использовать селектор %--%, чтобы найти ребра, которые соответствуют вашим условиям, и установить их 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)

enter image description here

Вместо color, вы также можете использовать label, если хотите:

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)

enter image description here

...