如何创建一个用其他信息表示不同集群的网络?

2024-09-28 22:24:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个数据帧nodes,其信息如下所示:

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

因此,在nodes数据帧中,有4列Names是基因名,type是不同的类型,type_num是每个基因类型的数字,Clusters列显示每个基因所属的3个簇

类似地,我还有其他数据帧edges,其信息如下:

dput(边缘)

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

尝试了igraph,但看起来不是我想要的样子

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)

看起来是这样的:

enter image description here

任何人都可以帮助我如何创建一个网络,如上所述的数据。感谢您的帮助。先谢谢你


Tags: 数据nettypenodestypeaabhd4undirectedaass
2条回答

这主要是对Grouped layout based on attribute答案的重复

认为您希望通过Clusters属性对顶点进行分组,并使用type属性为它们着色。我将在这个回答中这样做。 创建网络的代码很好,但简单的绘图不会按簇对顶点进行分组(我添加了按类型对顶点着色)

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

Ungrouped network plot

你需要的是一个强调集群的布局。上面引用的回答显示了如何通过生成具有相同顶点但在同一簇中的顶点之间具有重边权重的不同图来实现这一点。在你的情况下,应该是

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

如果有大量顶点,也可以使用vertex减小它们的大小。size=4

我不确定下面的代码是否有效

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

enter image description here

相关问题 更多 >