根据属性着色的 igraph 边缘消失

igraph edges colored according to attribute disappear

我想根据另一个边缘属性在 igraph 中为网络的边缘着色。

这是我所做的:

vector <- c("a","b","b","d","b","c","c","a")
field <- c("friend","friend","good friend","archenemy")

g <- graph(vector, directed = FALSE)
E(g)$status <- field

library(RColorBrewer)
color = grDevices::colors()[grep('gr(a|e)y', grDevices::colors(), invert = 
T)]
n <- length(unique(field))

col=sample(color, n)
sec=unique(field)
coloring <- as.list(setNames(col,sec))

E(g)$color <- coloring[E(g)$status]

这里我得到一个错误,或者根本没有边。

plot(g,edge.color=E(g)$color)

这对我有用。您的原始代码列出了 g 中的颜色。检查 E(g)$color.

library(igraph)
library(RColorBrewer)
color = grDevices::colors()[grep('gr(a|e)y', grDevices::colors(), invert = T)]

vector <- c("a","b","b","d","b","c","c","a")
field <- c("friend","friend","good friend","archenemy")
####
g <- graph(vector, directed = FALSE)
E(g)$status <- field

n <- length(unique(field))
col=sample(color, n)

coloring <- col[factor(field)]
E(g)$color <- coloring

plot(g)