Question

I am using iGraph to plot graphs. I have just two columns in my matrix- A and B. I need to color my nodes with just 2 colors – that indicates nodes that belong to A and those that belong to B. eg:

# k is a df with 2 columns – A and B
k_mx <- as.matrix(k)
k_mx_g <- graph.edgelist(k_mx, directed = FALSE)
V(k_mx_g)$color = ?? ( want blue for A and red for B)

Please let me know how to do this.

Thanks, PD

Was it helpful?

Solution

Assuming this dataframe:

k <-     structure(list(A = 1:4, B = 5:8), 
                 .Names = c("A", "B"), row.names = c(NA, -4L),
                  class = "data.frame")

... you could use rep with an each argument. Otherwise they will be sequentially labelled with c("blue," "red", "blue", "red", "blue", "red", "blue", "red") due to argument recycling:

V(k_mx_g)$color <- rep(c("blue", "red"), each=4)

Gabor's comment leads me to offer this option, which seems more general:

V(k_mx_g)[k$A]$color <- "blue"
V(k_mx_g)[k$B]$color <- "red"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top