Mauro
Mauro

Reputation: 107

How to draw custom labels by factors to a ggnet plot?

The following code

ggnet2(net, mode = "fruchtermanreingold",
       color = "module", palette = y, alpha = 0.75, size = 1, edge.alpha = 0.3,
       edge.color = c("color", "grey50"),
       set.seed(123))

returns this plot

enter image description here

Since the complexity of the network is high I want to draw unique labels on the plot, which means adding the module's name over the cluster of points that belong to the module. Hence, identifying each module will be easier.

I've tried with the label argument in various ways without getting the result

ggnet2(net, mode = "fruchtermanreingold",
       color = "module", palette = y, alpha = 0.75, size = 1, edge.alpha = 0.3,
       edge.color = c("color", "grey50"), **label = "module"**,
       set.seed(123))

modColor <- unique(geneME$moduleColor) #geneME is the data frame in which the gene-to-module relation is stored

ggnet2(net, mode = "fruchtermanreingold",
       color = "module", palette = y, alpha = 0.75, size = 1, edge.alpha = 0.3,
       edge.color = c("color", "grey50"), label = modColor,
       set.seed(123))

The net is directed with 9000 vertices and 1.5M edges

Upvotes: 1

Views: 194

Answers (1)

Mauro
Mauro

Reputation: 107

This network came from a weighted gene coexpression network analysis (WGCNA). To solve this issue through a sort of hand-made recipe do the following steps.

-First, obtain intra-modular connectivity for each module by using the

intramodularConnectivity.fromExpr(...., getWholeNetworkConnectivity = TRUE) 

function of WGCNA R package.

-Then filter the most within-module-connected gene (highest kWithin) for each module (maxGeneConnectME), (a df with two columns, one is the module, and the other the gene id)

-Obtain modules names

modsNet <- net %v% "module" %>% unique

-After that obtain "module-numbers" lists where in each list only the maxGene will be labeled,the rest genes will be NAs

ls <- lapply(modsNet, function(x){

maxGene <- maxGeneConnectME[maxGeneConectME$module == x,]$geneid


modsNet2 <- net %v% "module"

# assign NA to the nodes in which are not **maxGene**
modsNet2[which(network.vertex.names(net) != maxGene)] <- NA


return(modsNet2)})

-Generate a vector with the same length of total nodes of the net, and then assign NA to the nodes that not represent all the maxGenes and assign the name of the module to the nodes which correspond to maxGene

vec <- numeric(length = *nodes.number.of.net*)
for(i in 1:length(ls)){
  
  # obtain maxGene position in each list
  numb <- which(!is.na(ls[[i]]))
  # replace numb-position of vec with the name of the module
  # where the gene belongs
  vec[numb] <- ls[[i]][numb]
  
}

vec[vec == "0"] <- NA

-This will allow us to label only the maxGenes with the name of the module

ggnet2(net, mode = "fruchtermanreingold",
       color = "module", palette = y, alpha = 0.75, size = 1, edge.alpha = 0.3,
       edge.color = c("color", "grey50"), label = vec,
       set.seed(123))

producing something like this

enter image description here

Upvotes: 1

Related Questions