r - 是否可以使用 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
非常感谢您的帮助。
解决方案
我不太清楚你想要实现什么,但如果我做对了,你希望能够将两个图并排放置,以便你可以比较它们。
您可以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)
我知道它ggraph
的facet_graph()
功能与 -- 中的功能相似,ggplot
这可能是另一种解决方案。
推荐阅读
- javascript - 如何在php的模式弹出窗口中传递多个变量
- c++ - malloc:对象 0x00 的 *** 错误:未分配被释放的指针
- angular - 如何将具有不同 API 密钥的 Google Places API Javascript 库动态加载到 Ionic 应用程序中
- python - 在 Django 的内置 LoginView 中添加另一个模型的字段进行登录
- django - 如何在序列化程序中更改 ManyToManyField 名称
- ansible - 从服务器列表中将特定文件复制到特定服务器
- autocad - 如何将两个命令合二为一,在数量计划表中选择实体和添加属性集?
- c# - 即使设置了值,打印范围也不起作用
- sql-server - 如何修复 vbscript 中的 CopyFromRecordset 错误?
- ios - 为什么 GMSMapView 中没有显示标记?