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
2.3k views
in Technique[技术] by (71.8m points)

r - Union of igraph objects loses attributes

I have two igraph objects, which have different color attributes. Vertices "A" and "B" in first graph are colored red. Vertices "AA" and "BB" in second graph are colored green. After joining the two, the different colors are lost.

library(igraph)

graph.1= graph.data.frame(data.frame(start=c("a", "b"), end=c("A", "B")))
V(graph.1)[name%in% c("A", "B")]$color= "red"

graph.2= graph.data.frame(data.frame(start=c("a", "b"), end=c("AA", "BB")))
V(graph.2)[name%in% c("AA", "BB")]$color= "green"

graph= graph.union.by.name(graph.1, graph.2)

plot(graph)

enter image description here

How can I preserve the distinct colors when joining ?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

igraph doesn't loose the colors, it stores them in $color_1 and $color_2. I think this is because in the general case there might be common vertices with different colors. What would you do then?

Try this:

V(graph)$color <- ifelse(is.na(V(graph)$color_1),
                         V(graph)$color_2,V(graph)$color_1)
plot(graph)

BTW: your code didn't run for me. I had to use:

graph <- graph.union(graph.1, graph.2, byname=T)

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

...