如何重新缩放图以将群集节点进一步分开,并在igraph中命名群集?

如何解决如何重新缩放图以将群集节点进一步分开,并在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)

我得到的情节如下:

enter image description here

上图中有5个簇。

  1. 如何增加群集之间的空间?如何将它们移远?以及如何调整边缘?他们看起来很奇怪。

  2. 如何在图中命名群集?

  3. 如何将节点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)

Network Graph - version 1

好的,现在TypeC和TypeB更加可见,但是五个群集的布局很差。为了获得更像您的第二张(示例)图的图像,我们需要按层次构造布局:首先对集群进行布局,然后分别对集群中的点进行布局。五个群集的布局很简单。

F5 = make_full_graph(5)
Stretch = 6
LO_F5 = Stretch*layout.circle(F5)
plot(F5,layout=LO_F5)
 

Layout for clusters

现在,我们需要在每个聚类中布置点,并将它们隔开 使用刚创建的集群布局。但是这里需要权衡。 如果将群集分开,则所有节点都将很小 很难看到。如果您希望节点更大,则需要使 聚拢在一起(以便它们都适合地块)。你有 如此多的链接,无论您做什么,这些链接都会一起模糊 作为灰色背景。我选择了吸引我的中间立场, 但我邀请您探索因子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")

Network Graph - Version 2

现在您可以看到所有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)

Network Graph - Version 3

链接仍然是一个问题,但是我认为这可以解决您的其他问题。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res