2

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 ?

Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455
user2030503
  • 3,064
  • 2
  • 36
  • 53

1 Answers1

5

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)
jlhoward
  • 58,004
  • 7
  • 97
  • 140
  • Hi Jihoward. Have you any idea how to do this for `n` igraph objects? (When you might have hundreds of graphs and literally can't write out the ifelse statement...) I posted a question here: https://stackoverflow.com/questions/47637321/combine-non-na-values-for-n-lists-r Thanks, C – Chuck Dec 04 '17 at 16:54