首页 > 解决方案 > 是否可以使用 R 中的 igraph 对每个实验条件进行网络分析?

问题描述

我想使用 R 中的 igraph 包执行网络分析,比较 2 个不同实验条件(A 和 B)之间的边和顶点。有没有办法使用 igraph 执行类似于 facet 函数的操作?

我生成了包含重复顶点和边的顶点和边数据帧(每个实验条件一个)。下面我报告了我的数据框的简短版本:

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)
)

到目前为止,我只设法运行 igraph,一次考虑 1 个实验条件(A 或 B)拆分数据帧,如下所述。条件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)

条件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)

但是,通过这种方式,我无法直接比较这两个条件的顶点和边缘的渐变色标。我想在 2 种条件下将 A 和 B 网络分析与边缘和顶点的通用渐变色标并排绘制。

当我尝试使用我的整个数据框(不选择一个特定条件)运行我的 igraph 时:

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

我收到此错误:

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

非常感谢您的帮助。

标签: rigraph

解决方案


我不太清楚你想要实现什么,但如果我做对了,你希望能够将两个图并排放置,以便你可以比较它们。

您可以par()像这样并排设置图。

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)

我知道它ggraphfacet_graph()功能与 -- 中的功能相似,ggplot这可能是另一种解决方案。


推荐阅读