简体   繁体   中英

Color the edges the same color as the nodes in igraph

I have a dataset similar to this.

    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)

this is the result在此处输入图像描述

I would like to color each edge the same color as the node it started from. For example, each link from Luca in red, each link from Luisa in yellow. Couldn't find anything about it, thanks in advance

In the question the vertex colors were set in the plot command so they were not part of the graph. Instead set the vertex colors in the graph itself and then set the edge colors based on those.

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

截屏

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM