1

I would like to perform network analyses using igraph package in R comparing edges and vertices between 2 different experimental conditions (A and B). Is there a way to perform something similar to a facet function using igraph?

I generated vertice and edge dataframes containing duplicate vertices and edges (one per experimental condition). Below I reported a short version of my data frames:

vertices<-data.frame(marker = c("CD3", "CD25", "pCREB", "CD4", "pSTAT5", "pS6", "pRB",  "CD25", "CD3", "pSTAT4"),
cond_1 = c("A", "A", "A", "A", "B", "B", "B", "B", "B", "B"),
EMD= c(-0.83, -1.43, -0.07, -1.16, 0.08, 0.16, 0.28, 1.83,-0.37, 0.12 )
)

edges<-data.frame(marker_x = c("CD3","CD3", "CD4", "pSTAT5", "pRB", "CD25", "CD3"),
                  marker_y = c("CD25", "pCREB", "CD25", "pS6", "pS6", "pS6", "pSTAT4"),
                  cond_1 = c("A", "A", "A", "B", "B", "B", "B"),
                  DREMI=c(0.53, 0.80, 0.7, 1, 0.35,0.24, 0.44)
)

So far, I only managed to run igraph splitting the dataframes considering 1 experimental condition (A or B) at a time as reported below. Analysis of condition B:

#selected and run igraph for condition B
t<- unique(vertices$cond_1[vertices$cond_1 == "A"]) 
t
vertices2<-vertices[!vertices$cond_1 %in% t, ]

q<- unique(edges$cond_1[edges$cond_1 == "A"]) 
q
edges2<-edges[!edges$cond_1 %in% q, ]
g<-graph_from_data_frame(d=edges2, vertices=vertices2, directed=TRUE)
g

#weighted edges according to the numeric value DREMI
w1 <-E(g)$DREMI
#generated a gradient scale for vertices
my_resolution = 100
my_palette = colorRampPalette(brewer.pal(n = 9, name= "Reds"), alpha=TRUE)
my_max    = max(V(g)$EMD, na.rm=TRUE)
my_vector = V(g)$EMD / my_max
my_colors = my_palette(my_resolution)[as.numeric(cut(my_vector, breaks=my_resolution))]
#generated a gradient scale for edges
my_resolutionE = 5
my_paletteE = colorRampPalette(brewer.pal(n = 5, name= "Blues"), alpha=TRUE)
my_maxE    = max(E(g)$DREMI, na.rm=TRUE)
my_vectorE = E(g)$DREMI / my_maxE
my_colorsE = my_paletteE(my_resolutionE)[as.numeric(cut(my_vectorE, breaks=my_resolutionE))]

plot(g, vertex.color=my_colors,
     layout = layout_nicely(g), 
     vertex.label.color ="black",
     edge.color = my_colorsE,
     edge.width = w1,
     edge.arrow.size = 0.5,
     vertex.size =15,
     vertex.label.cex = 0.5)

Analysis for condition A:

#selected and run igraph for condition A
w<- unique(vertices$cond_1[vertices$cond_1 == "B"]) 
w
vertices3<-vertices[!vertices$cond_1 %in% w, ]

r<- unique(edges$cond_1[edges$cond_1 == "B"]) 
r
edges3<-edges[!edges$cond_1 %in% r, ]
g3<-graph_from_data_frame(d=edges3, vertices=vertices3, directed=TRUE)
g3

#weighted edges according to the numeric value DREMI
w1 <-E(g3)$DREMI
#generated a gradient scale for vertices
my_resolution = 100
my_palette = colorRampPalette(brewer.pal(n = 9, name= "Reds"), alpha=TRUE)
my_max    = max(V(g3)$EMD, na.rm=TRUE)
my_vector = V(g3)$EMD / my_max
my_colors = my_palette(my_resolution)[as.numeric(cut(my_vector, breaks=my_resolution))]
#generated a gradient scale for edges
my_resolutionE = 5
my_paletteE = colorRampPalette(brewer.pal(n = 5, name= "Blues"), alpha=TRUE)
my_maxE    = max(E(g3)$DREMI, na.rm=TRUE)
my_vectorE = E(g3)$DREMI / my_maxE
my_colorsE = my_paletteE(my_resolutionE)[as.numeric(cut(my_vectorE, breaks=my_resolutionE))]

plot(g3, vertex.color=my_colors,
     layout = layout_nicely(g3), 
     vertex.label.color ="black",
     edge.color = my_colorsE,
     edge.width = w1,
     edge.arrow.size = 0.5,
     vertex.size =15,
     vertex.label.cex = 0.5)

In this way however I cannot directly compare the 2 conditions in terms of gradient colour scale of vertices and edges. I would like to plot A and B network analyses side by side with a common gradient colour scale for edges and vertices in the 2 conditions.

When I try to run my igraph using my entire data frames (not selecting one specific condition):

g0<-graph_from_data_frame(d=edges, vertices=vertices, directed=TRUE)
g0

I get this error:

Error in graph_from_data_frame(d = edges, vertices = vertices, directed = TRUE) : 
  Duplicate vertex names

Many thanks for your help.

Ilaria
  • 11
  • 2
  • Question is too broad, please provide example input data, expected output, current codes. – zx8754 Jul 07 '20 at 11:57
  • Thank you, I updated my post – Ilaria Jul 07 '20 at 14:23
  • Welcome to SO. Sharing pictures of your data is not too helpful, as we cannot work with it. You can either share (part of) your data using `dput`, or create some dummy data, so that all of your code is reproducible. See here for more info https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – desval Jul 07 '20 at 17:14
  • Many thanks for the advice, I updated my post – Ilaria Jul 07 '20 at 20:25

1 Answers1

0

I'm not super clear on what you want to achieve, but if I get it right, you want to be able to have both plots side by side so that you can compare them.

You could use par() to set the plots side by side like this.

par(mfrow = c(1,2)) # graphical parameter function (you can tinker with what you
# need)

# plot g

plot(g, vertex.color=my_colors,
         layout = layout_nicely(g), 
         vertex.label.color ="black",
         edge.color = my_colorsE,
         edge.width = w1,
         edge.arrow.size = 0.5,
         vertex.size =15,
         vertex.label.cex = 0.5)

# plot g3

    plot(g3, vertex.color=my_colors,
             layout = layout_nicely(g3), 
             vertex.label.color ="black",
             edge.color = my_colorsE,
             edge.width = w1,
             edge.arrow.size = 0.5,
             vertex.size =15,
             vertex.label.cex = 0.5)

I know that ggraph has a facet_graph() function that operates similarly to that found in ggplot--and that might be another solution.

avgoustisw
  • 213
  • 1
  • 7