Entering edit mode
9.1 years ago
Bioinformatist Newbie
▴
270
Hi,
I want to merge 2 networks but I want different color of their node. For example N1's vertices should be represented with color "Red" while of N2's with color "Green". So that I can identify which nodes in the merged network belongs to which network (N1 or N2).
I am using following code:
library(igraph)
dd <- read.table("g1.txt")
g1 <- graph.data.frame(dd) # N1
cd <- read.table("g2.txt")
g2 <- graph.data.frame(cd) #N2
n1 <- set.vertex.attribute(g1, "color", value=c("red"))
plot(n1) # Color of N1's nodes is Red
n2 <- set.vertex.attribute(g2, "color", value=c("green"))
plot(n2) # Color of N2's nodes is green
Problem arises when I merge them. The color of all nodes get same (I guess the by default node color)
g = graph.union(n1, n2, byname=TRUE) # Merging N1 and N2
plot(g) # Color of all nodes is same (by default=orange)
Please advise me how I can obtain different colors of nodes in merged network. Thanks.
It looks like the union method doesn't preserve the node attributes. In this case, a solution is to simply assign attributes after merging e.g. looping through all union nodes and assigning red or green based on whether they come from n1 or n2 (although there's likely a better way to do it).
Edit: A better way may be to use the option
keep.x.vertex.attributes=TRUE
as mentioned in the doc.OR
Is not working for me...!
From
?graph.union
:So I think the color attributes gets renamed
color_1
andcolor_2
. You should be able to just do: