Лично мне нравится решение tidygraph
/ ggraph
, но если вам интересно, вот как это сделать в igraph
.
library(igraph)
G = make_undirected_graph(c(1,2,
2,3,
3,4))
V(G)$attribute = c(T,T,T,F)
# Get the index for nodes with the attribute
idx <- which(V(G)$attribute)
# Assign the "non-homophilous" color
E(G)$color <- "tomato"
# Assign the "homophilous" color using the index and the `%--%` operator
E(G)[idx %--% idx]$color <- "steelblue"
plot(G)
Создано в 2020-03-23 пакетом представ. (v0.3.0)
Это может быть обобщено для атрибутов с произвольным числом признаков:
G <- igraph::sample_gnp(150, 0.05)
V(G)$gender <- sample(c("M", "F", "NB"), 150, replace = TRUE)
m_idx <- which(V(G)$gender == "M")
f_idx <- which(V(G)$gender == "F")
nb_idx <- which(V(G)$gender == "NB")
E(G)$color <- "tomato"
E(G)[m_idx %--% m_idx]$color <- "steelblue"
E(G)[f_idx %--% f_idx]$color <- "steelblue"
E(G)[nb_idx %--% nb_idx]$color <- "steelblue"
plot(G, vertex.size = 5, vertex.label = NA)
Создано в 2020-03-23 с помощью представительного пакета (v0.3.0)
library(dplyr)
library(igraph)
G <- sample_gnp(150, 0.05)
V(G)$quant <- runif(150)
epsilon <- 0.5
G <- G %>%
igraph::as_data_frame() %>%
mutate(diff = abs(V(G)$quant[.[,1]] - V(G)$quant[.[,2]]) > epsilon) %>%
graph_from_data_frame(directed = FALSE)
E(G)$color <- ifelse(E(G)$diff, "steelblue", "tomato")
plot(G, vertex.size = 5, vertex.label = NA, color = diff)
Создано в 2020-03-24 с помощью пакета представить (v0.3.0)