Home > Blockchain >  Coloring nodes and edges based on module colors attributes
Coloring nodes and edges based on module colors attributes

Time:01-18

I have simple dataframe which I have obtained after running WGCNA where the correaltion was turned into and enter image description here

CodePudding user response:

Here's an approach leveraging ggraph. The first thing I do here is create a separate dataframe of the vertices. It has two columns, one for the gene and one for the color.

df1 <- dd %>%
    select(gene = gene1, color = module1)

df2 <- dd %>%
    select(gene = gene2, color = module2)

df_color <- unique(rbind(df1,df2))

I take a slightly different approach to creating the igraph object. Here we just feed it the first three columns of dd for the relationships, and the new df_color dataframe for the list of vertices.

g2 <- graph_from_data_frame(dd[,1:3], directed = FALSE, vertices = df_color)

You can see now that we've captured both the names and the colors associated with the vertices in the igraph object.

V(g2)$name
 [1] "GBA3"      "IGHV3-52"  "GGNBP1"    "OR52B6"    "LRP2BP"    "ADGB"      "ASNSP3"    "HSD17B2"  
 [9] "HSP90B1"   "IFT22"     "P4HB"      "TTC22"     "XKR9"      "IQSEC2"    "NECAB2"    "ANO1"     
[17] "CPPED1"    "MAGEE1"    "MAPRE3"    "COTL1P1"   "OR13G1"    "FTH1P11"   "KRT8P44"   "LINC00243"
[25] "MYOZ1"     "PARD6G"    "PDLIM5"    "RN7SL67P"  "PARP3"     "SH3BGRL3"  "KIF1B"     "CDK6"     
[33] "CYP24A1"   "TFEB" 

V(g2)$color
 [1] "midnightblue" "midnightblue" "tan"          "tan"          "brown"        "grey"        
 [7] "blue"         "grey"         "black"        "cyan"         "black"        "brown"       
[13] "green"        "turquoise"    "turquoise"    "grey"         "turquoise"    "brown"       
[19] "turquoise"    "turquoise"    "blue"         "turquoise"    "blue"         "brown"       
[25] "grey"         "yellow"       "turquoise"    "purple"       "turquoise"    "turquoise"   
[31] "turquoise"    "blue"         "brown"        "turquoise"  

From here we can use aesthetics as we can do in ggplot. I could not follow your second point regarding what you want to do with size. We can set the width aesthetic to another variable as we did for color to set the size of the edge. Here I've set it to the correlation. Hopefully this gives you a good starting point and you can finish it off. Otherwise, please clarify what you are looking to do with size and I'll try to offer more help.

ggraph(g2, layout = "igraph", algorithm = "fr")  
    geom_node_point(aes(color = color), shape = 1, size = 3) 
    geom_edge_link(aes(width = correlation), edge_color = "steelblue", 
                   arrow = arrow(angle = 30, length = unit(0.05, "inches"), ends = "last", type = "open"))  
    geom_node_text(aes(label = name, color = color))  
    theme_void()  
    theme(legend.position = "none") 


enter image description here

  • Related