1
votes

I'm trying to color the vertices according to the dummy variable [color_thresh]. Data looks like the following:

Data

I have tried several suggested methods, but the closest (and simplest) I can do produces the following:

Graph

Here is my code for reproducing data and the graph above:

library(igraph)
library(data.tree)

Level_0 = c("TAC_310B_124","TAC_310B_124","TAC_310B_124","TAC_310B_124","TAC_310B_124","TAC_310B_124","TAC_310B_124","TAC_310B_124","TAC_310B_124")
Level_1 = c("","SC_776E_86","SS_A66D_9","SC_776E_86","SC_776E_86","SC_776E_86","SC_776E_86","SC_776E_86","SC_776E_86")
Level_2 = c("","","","TGW_1DD0_42","TGW_1DD0_42","TGW_1DD0_42","TGW_1DD0_42","TGW_1DD0_42","TGW_1DD0_42")
Level_3 = c("","","","","CORB_F2E4_17","CORB_F2E4_22","CORB_AE16_10921","CORB_AE16_10921","CORB_AE16_10921")
Level_4 = c("","","","","","","","CORB_D81E_45","CORB_D81E_45")
Level_5 = c("","","","","","","","","SC_B949_3156")
color_thresh = c(1,0,0,0,0,0,1,0,1)

dataset <- data.frame(Level_0,Level_1,Level_2,Level_3,Level_4,Level_5,color_thresh)

dataset$pathString <- paste(dataset$Level_0, 
                            dataset$Level_1, 
                            dataset$Level_2, 
                            dataset$Level_3, 
                            dataset$Level_4, 
                            dataset$Level_5, 
                            sep = "/")

g <- as.Node(dataset)
g <- as.igraph(g, directed=TRUE)

V(g)$color <- ifelse(dataset$color_thresh==1, "red", "green")

plot.igraph(g, vertex.label.color="black", vertex.label.dist=0, vertex.label.family="Helvetica", vertex.frame.color="white", vertex.size=25, layout=layout_as_tree)

The colors change when i sort the data frame by [color_thresh], but still doesn't give me the right colors. Any comments helping me to understand how the conditional coloring works is much appreciated! Thank you so much in advance.

1
You have to sort your color according to your nodes. - patL
Thanks for your comment, patL, but can you be more specific? How can i sort the color according to the nodes? - Daniel Safai
If you have generated a vector for colors called colors it could be done with V(g)$color[colors] and it will sort your colors according to the colors vector. - patL
@Daniel Safai edited my answer with another aproach - missuse
The first approach is better as the color_thresh variable is dynamic so the graph has to update the colors with the most recent value. Thanks again! I would never have come up with such elegant solution as the first you gave. - Daniel Safai

1 Answers

1
votes

Here is an approach:

g <- as.Node(dataset)
g <- as.igraph(g, directed=TRUE)

Check how the nodes are sorted

g_data <-  get.data.frame(g,  what="vertices")$name

see how the nodes are sorted in your data:

data_vert <- apply(dataset[1:6], 1, function(x) tail(x[x!=""], 1))

Sort data according to nodes and apply color

 V(g)$color <- ifelse(dataset$color_thresh[match(g_data , data_vert)] == 1, "red", "green")


plot.igraph(g, vertex.label.color="black", vertex.label.dist=0, vertex.label.family="Helvetica", vertex.frame.color="white", vertex.size=25, layout=layout_as_tree)

enter image description here

Is that the correct color coding?

another approach would be explicitly stating which nodes should have which color:

g <- as.Node(dataset)
g <- as.igraph(g, directed = TRUE)

V(g)$color <- "green" 
V(g)["TAC_310B_124"]$color <- "red"
V(g)["SC_B949_3156" ]$color <- "red"
V(g)["CORB_AE16_10921"]$color <- "red" 

same output image