边缘的颜色与 igraph 中的节点颜色相同

时间:2021-01-08 17:57:39

标签: r graph igraph

我有一个类似的数据集。

    library(igraph)

df <- data.frame(Account= c('Luca', 'Alberto', 'Luisa', 'Marcello', 'Tania'),
                 friend = c('Alberto', 'Luisa', 'Tania', 'Alberto', 'Luisa'))


edges1 = data.frame("from" =df$Account , "to" =df$friend , stringsAsFactors = F )
net <- graph_from_data_frame(d= edges1, directed = T)
V(net)$degree <- degree(net)
plot(net, vertex.size=20,
     vertex.color=rainbow(11),
     edge.arrow.width=0.7,
     edge.lty= 6)

这是结果 enter image description here

我想给每条边涂上与它开始的节点相同的颜色。例如,来自 Luca 的每个链接为红色,来自 Luisa 的每个链接为黄色。 找不到任何相关信息,提前致谢

1 个答案:

答案 0 :(得分:0)

在问题中,顶点颜色是在 plot 命令中设置的,因此它们不是图形的一部分。而是在图形本身中设置顶点颜色,然后根据这些设置边缘颜色。

V(net)$color <- rainbow(ecount(net))
E(net)$color <- tail_of(net, E(net))$color
plot(net)

screenshot