【问题标题】:How to create a network representing different clusters with other information?如何使用其他信息创建代表不同集群的网络?
【发布时间】:2020-08-27 15:26:24
【问题描述】:

我有一个数据框 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,其信息如下:

输入(边)

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)

它看起来像这样:

谁能帮我用上面给出的数据创建一个像上面这样的网络。任何帮助表示赞赏。提前谢谢你。

【问题讨论】:

  • 您的示例数据不可用 Nodes 数据框和 Edges 数据框包含不同的节点。但是,Grouped layout based on attribute 的答案显示了如何制作一个按集群对节点进行分组的图
  • @G5W 非常感谢您的回复。我现在更改了上述帖子中的数据。请现在检查它们是否具有相同的节点。你能帮我用上面的数据绘制它吗?谢谢

标签: python r data-visualization networkx igraph


【解决方案1】:

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

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

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

您需要的是一个强调集群的布局。上面引用的先前答案显示了如何通过生成具有相同顶点但在同一簇中的顶点之间具有重边权重的不同图来做到这一点。在你的情况下,它会是

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

如果您有大量顶点,您可能还希望通过 vertex.size=4 减小它们的大小

【讨论】:

  • 非常感谢。很有用。但我需要一点帮助。我将此代码应用于大量数据,它看起来像这样 [i.imgur.com/MsPhXOL.png] 1) 在这里我想命名图像中的集群。 2) 我看不到typeC 有没有办法将那些typeC 放在顶部。 3)如何将集群移动一点,以便我可以看到边缘(因为现在我只能看到整个灰色)。
  • 我将从减小顶点的大小开始。以vertex.size=4开头,每种类型有多少个顶点?
  • 是的,我提供链接的图片是vertex.size=4。 A型(930),B型(110),C型(3)。当然 typeC 的数量很少,但希望它们能占据上风。
【解决方案2】:

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

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

这给了

【讨论】:

    猜你喜欢
    • 2018-08-04
    • 1970-01-01
    • 1970-01-01
    • 2022-08-16
    • 1970-01-01
    • 2020-07-05
    • 2021-04-22
    • 2021-02-28
    • 1970-01-01
    相关资源
    最近更新 更多