Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
431 views
in Technique[技术] by (71.8m points)

r - 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 enter image description here

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

screenshot


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...