如何解决如何重新缩放图以将群集节点进一步分开,并在igraph中命名群集?
我有节点和边的信息,并试图以此绘制网络图。节点信息具有1552行,其中包含以下信息:
边缘信息包含四列,其中包含1203576个条目。
使用我在下面的代码中使用的节点和边数据来绘制网络图。
library(igraph)
net <- graph_from_data_frame(d=edges,vertices=nodes,directed=F)
plot(net,edge.arrow.size=.4,vertex.label=NA,vertex.color=as.numeric(factor(nodes$type)))
Grouped.net = net
E(Grouped.net)$weight = 1
colnames(nodes)[4] <- "Clusters"
## 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=500))
}
## Now create a layout based on G_Grouped
set.seed(567)
LO = layout_with_fr(Grouped.net)
# Generate colors based on media type:
colrs <- c("gray50","yellow","tomato")
V(net)$color <- colrs[V(net)$type_num]
plot(net,layout=LO,edge.arrow.size=0,asp=0,vertex.size=4)
legend(x=-1.5,y=-1.1,c("typeA","typeB","typeC"),pch=21,col="#777777",pt.bg=colrs,pt.cex=2,cex=.8,bty="n",ncol=1)
我得到的情节如下:
上图中有5个簇。
-
如何增加群集之间的空间?如何将它们移远?以及如何调整边缘?他们看起来很奇怪。
-
如何在图中命名群集?
-
如何将节点typeC置于顶部?他们的数量很少。由于typeA的数量众多,因此typeC在下面。
感谢您的帮助。 thanq。
解决方法
您有几个问题。我将尝试回答所有问题,但顺序不同。
设置
library(igraph)
edges = read.csv("temp/edges_info_5Clusters.csv",stringsAsFactors=T)
nodes = read.csv("temp/nodes_info_5Clusters.csv",stringsAsFactors=T)
问题3。如何将typeC节点置于顶部?
按照节点编号的顺序绘制节点。为了得到
要显示的类型很少,我们需要那些节点来获得最高的
节点号。因此,只需对类型进行排序即可强制节点进入
订单TypeA,TypeB,TypeC。
nodes = nodes[order(nodes$type),]
net <- graph_from_data_frame(d=edges,vertices=nodes,directed=F)
我将直接转到您在其中进行过的分组绘图 您的代码以显示结果。
Grouped.net = net
E(Grouped.net)$weight = 1
colnames(nodes)[4] <- "Clusters"
## 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=500))
}
## Now create a layout based on G_Grouped
set.seed(567)
LO = layout_with_fr(Grouped.net)
colrs <- c("gray50","yellow","tomato")
V(net)$color <- colrs[V(net)$type_num]
plot(net,layout=LO,edge.arrow.size=0,vertex.label=NA,vertex.size=4,edge.color="lightgray")
legend(x=-1.5,y=-1.1,c("typeA","typeB","typeC"),pch=21,col="#777777",pt.bg=colrs,pt.cex=2,cex=.8,bty="n",ncol=1)
好的,现在TypeC和TypeB更加可见,但是五个群集的布局很差。为了获得更像您的第二张(示例)图的图像,我们需要按层次构造布局:首先对集群进行布局,然后分别对集群中的点进行布局。五个群集的布局很简单。
F5 = make_full_graph(5)
Stretch = 6
LO_F5 = Stretch*layout.circle(F5)
plot(F5,layout=LO_F5)
现在,我们需要在每个聚类中布置点,并将它们隔开
使用刚创建的集群布局。但是这里需要权衡。
如果将群集分开,则所有节点都将很小
很难看到。如果您希望节点更大,则需要使
聚拢在一起(以便它们都适合地块)。你有
如此多的链接,无论您做什么,这些链接都会一起模糊
作为灰色背景。我选择了吸引我的中间立场,
但我邀请您探索因子Stretch
的不同值。
Stretch
的值越大,聚类越远
较小的节点。较小的值将使群集更加紧密
与更大的节点。选择适合您的东西。
set.seed(1234)
HierLO = matrix(0,ncol=2,nrow=vcount(net))
for(i in 1:length(levels(nodes$Clusters))) {
CLUST = which(nodes$Clusters == levels(nodes$Clusters)[i])
SubNet = induced_subgraph(net,V(net)[CLUST])
LO_SN = scale(layout_nicely(SubNet))
HierLO[CLUST,] = LO_SN +
matrix(LO_F5[i,],nrow=vcount(SubNet),byrow=TRUE)
}
plot(net,layout=HierLO,edge.color="lightgray")
现在您可以看到所有TypeC节点和大多数TypeB(群集1中有很多TypeB的情况除外)。
最后,让我们添加集群标签。这些仅需要相对于群集中心放置。这些中心是布局LO_F5给定的种,但是igraph绘图会重新调整布局的比例,以使绘图实际具有范围(-1,1)。 我们可以自己重新缩放LO_F5,然后稍微拉伸位置,使标签位于圆的外面。
LO_Text = LO_F5
LO_Text[,1] = 2*(LO_F5[,1] - min(LO_F5[,1]))/(max(LO_F5[,1]) - min(LO_F5[,1])) -1
LO_Text[,2] = 2*(LO_F5[,2] - min(LO_F5[,2]))/(max(LO_F5[,2]) - min(LO_F5[,2])) -1
text(1.2*LO_Text,labels=levels(nodes$Clusters))
legend(x=-1.5,ncol=1)
链接仍然是一个问题,但是我认为这可以解决您的其他问题。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。