2

I have a dataframe nodes with information that looks like below:

dput(nodes)

structure(list(Names = c("A4GALT", "AASS", "ABCA10", "ABCA7", 
"ABCD4", "ABHD4", "ABTB1", "AC006978.2", "AC009119.2"), type = c("typeA", 
"typeA", "typeC", "typeA", "typeC", "typeC", "typeB", "typeB", 
"typeB"), type_num = c(1L, 1L, 3L, 1L, 3L, 3L, 2L, 2L, 2L), Clusters = c("Cluster1", 
"Cluster1", "Cluster2", "Cluster3", "Cluster3", "Cluster1", "Cluster2", 
"Cluster3", "Cluster2")), row.names = c(NA, 9L), class = "data.frame") 

So, in the nodes dataframe, there are 4 columns. Names is the gene name, type is different types, type_num is the number given to each genetype and column Clusters show 3 clusters in which each gene belong to.

Similarly, I have other dataframe edges with with information like below:

dput(edges)

structure(list(fromNode = c("A4GALT", "A4GALT", "A4GALT", "A4GALT", 
"A4GALT", "A4GALT", "A4GALT", "A4GALT", "AASS", "AASS", "AASS", 
"AASS", "AASS", "AASS", "AASS", "ABCA10", "ABCA10", "ABCA10", 
"ABCA10", "ABCA10", "ABCA10", "ABCA7", "ABCA7", "ABCA7", "ABCA7", 
"ABCA7", "ABCD4", "ABCD4", "ABCD4", "ABCD4", "ABHD4", "ABHD4", 
"ABHD4", "ABTB1", "ABTB1", "AC006978.2"), toNode = c("AASS", 
"ABCA10", "ABCA7", "ABCD4", "ABHD4", "ABTB1", "AC006978.2", "AC009119.2", 
"ABCA10", "ABCA7", "ABCD4", "ABHD4", "ABTB1", "AC006978.2", "AC009119.2", 
"ABCA7", "ABCD4", "ABHD4", "ABTB1", "AC006978.2", "AC009119.2", 
"ABCD4", "ABHD4", "ABTB1", "AC006978.2", "AC009119.2", "ABHD4", 
"ABTB1", "AC006978.2", "AC009119.2", "ABTB1", "AC006978.2", "AC009119.2", 
"AC006978.2", "AC009119.2", "AC009119.2"), weight = c(0.005842835, 
0.002253695, 0.014513253, 0.004851739, 0.066702792, 0.009418991, 
0.001136938, 0.000474221, 0.004405601, 0.000666001, 0.005625977, 
0.0333554, 0.004666223, 0.000103131, 0.00026302, 0.004514819, 
0.029632695, 0.001825839, 0.028379806, 0.001403298, 0.008339397, 
0.02393394, 0.004782329, 0.024767355, 0.002986813, 0.00559471, 
0.005961539, 0.064831874, 0.013023138, 0.027935729, 0.006618816, 
0.001134219, 0.012798368, 0.007961242, 0.01640476, 0.007997743
), direction = c("undirected", "undirected", "undirected", "undirected", 
"undirected", "undirected", "undirected", "undirected", "undirected", 
"undirected", "undirected", "undirected", "undirected", "undirected", 
"undirected", "undirected", "undirected", "undirected", "undirected", 
"undirected", "undirected", "undirected", "undirected", "undirected", 
"undirected", "undirected", "undirected", "undirected", "undirected", 
"undirected", "undirected", "undirected", "undirected", "undirected", 
"undirected", "undirected")), row.names = c(NA, -36L), class = "data.frame")

Tried with igraph but didn't look the way I want.

library(igraph)
net <- graph_from_data_frame(d=edges, vertices=nodes, directed=F)

as_edgelist(net, names=T)
as_adjacency_matrix(net, attr="weight")

# Removing loops from the graph:
net <- simplify(net, remove.multiple = F, remove.loops = T) 

# Let's and reduce the arrow size and remove the labels:
plot(net, edge.arrow.size=.4,vertex.label=NA)

And it looks like this:

enter image description here

Can anyone please help me how to create a network like above with the data given above. Any help is appreciated. thank you in advance.

beginner
  • 1,059
  • 8
  • 23
  • 1
    Your example data is not useable The Nodes data frame and the Edges data frame contain different nodes. However, the answer to [Grouped layout based on attribute](https://stackoverflow.com/q/37378744/4752675) shows how to make a plot that groups nodes by cluster – G5W Aug 28 '20 at 17:15
  • @G5W thanks a lot for reply. I changed the data in the above post now. Please check now they have same nodes. Could you please help me how to plot that with the above data? thanq – beginner Aug 28 '20 at 22:13

2 Answers2

1

This is mostly a repetition of the answer to Grouped layout based on attribute.

I think that you want to group the vertices by the Clusters attribute and color them using the type attribute. I will do that in this answer. Your code creating the network is fine, but a simple plot does not group the vertices by the clusters (I have added coloring the vertices by type).

plot(net, edge.arrow.size=.4,vertex.label=NA, 
    vertex.color=as.numeric(factor(nodes$type)))

Ungrouped network plot

What you need is a layout that will emphasize the clusters. The previous answer cited above shows how to do that by generating a different graph with the same vertices but with heavy edge weights between vertices in the same cluster. In your case, it would be

Grouped.net = net
E(Grouped.net)$weight = 1

## Add edges with high weight between all nodes in the same group
for(Clus in unique(nodes$Clusters)) {
    GroupV = which(nodes$Clusters == Clus)
    Grouped.net = add_edges(Grouped.net, combn(GroupV, 2), attr=list(weight=80))
} 

## Now create a layout based on G_Grouped
set.seed(567)
LO = layout_with_fr(Grouped.net)

## Use the layout to plot the original graph
plot(net, layout=LO, edge.arrow.size=.4,vertex.label=NA, 
    vertex.color=as.numeric(factor(nodes$type)))

network grouped by cluster

If you have a large number of vertices, you may also wish to reduce their size with vertex.size=4

G5W
  • 36,531
  • 10
  • 47
  • 80
  • thank you very much. Very useful. But I need small help. I'm applying this code for a huge data and it looks like this [https://i.imgur.com/MsPhXOL.png] 1) Here I want to name the clusters in the image. 2) I'm not able to see `typeC` Is there a way to bring those `typeC` on the top. 3) How to move the clusters little far so that I can see the edges (because now I can only see the whole grey color). – beginner Aug 29 '20 at 21:17
  • 1
    I would start by reducing the size of the vertices. Start with `vertex.size=4` How many vertices of each type do you have? – G5W Aug 29 '20 at 21:36
  • yes the image which I gave the link is `vertex.size=4`. typeA (930), typeB (110), typeC (3). Ofcourse typeC are very less but want them to bring on the top. – beginner Aug 29 '20 at 21:40
  • Any help with this please https://stackoverflow.com/questions/64122201/how-to-rescale-the-plot-to-push-the-clusters-nodes-a-bit-further-apart-and-nam – beginner Sep 29 '20 at 15:10
0

I am not sure if the code below works

plot(net,
     edge.width = E(net)$weight,
     vertex.color = factor(V(net)$name),
     mark.groups = split(V(net)$name,V(net)$Clusters))

which gives enter image description here

ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81