增加igraph节点之间的距离

时间:2023-02-01 17:19:16

I have a graph that I have produced using igraph. I'd like to spread out the nodes. The only way I have found so far to do this is to scale the layout and force the plot command to not rescale.

我有一个使用igraph生成的图表。我想分散节点。到目前为止我找到的唯一方法是缩放布局并强制绘图命令不重新缩放。

png("kmeansColouredNetwork.png", width=1200,height = 1000)
col=c("yellow", "saddlebrown", "brown1","chartreuse2", "chocolate1","darkorange" ,"deepskyblue1", "hotpink1","plum2")
for(i in 1:9){
  V(graph)$cluster[which(V(graph)$name %in% kmeans[,i])]<-col[i]
}
V(graph)$color=V(graph)$cluster
coords <- layout.fruchterman.reingold(graph)*0.5
plot(graph, layout = coords, vertex.label=NA, rescale=FALSE,  vertex.size=degree(graph)*.25,vertex.color=V(graph)$cluster)
labels = paste("cluster:", 1:length(colours))
legend("left",legend=labels, col=col, pch=16, title="K means clustered subgroups")
dev.off()

If I don't rescale, the central highly connected nodes clump together and I get a graph like this, where the patterns in the body of the graph are impossible to discern: 增加igraph节点之间的距离

如果我不重新缩放,*高度连接的节点聚集在一起,我得到一个这样的图形,其中图形体中的模式无法辨别:

On the other hand, if I tell the plot command not to rescale, then I get this : 增加igraph节点之间的距离

另一方面,如果我告诉plot命令不要重新缩放,那么我得到这个:

where the patterns are discernible, but half the graph is off the plot. It's not a matter of plot size as if I increase the dimensions of the png, it still centres the graph off the edge of the plot.

图案可辨别的地方,但图表的一半不在图中。这不是绘图大小的问题,就好像我增加了png的尺寸一样,它仍然使图形偏离绘图的边缘。

It's not a matter of the layout - I've tried fruchterman.reingold, layout_nicely, reingold.tilford, layout.circle, layout random, the same thing happens.

这不是布局的问题 - 我尝试过fruchterman.reingold,layout_nicely,reingold.tilford,layout.circle,布局随机,同样的事情发生了。

There apparently used to be a variable to set a repulsion factor between nodes, but that appears to be deprecated.

显然曾经有一个变量来设置节点之间的排斥因子,但这似乎已被弃用。

How does one spread the nodes of the graph out or rescale and recenter the plot?

如何将图表的节点展开或重新缩放并重新定位图?

2 个解决方案

#1


2  

It is NOT my answer, just found on *:
igraph axes xlim ylim plot incorrectly

这不是我的答案,只是在*上找到:igraph axis xlim ylim plot错误

Basically, you can set ylim and xlim and asp. You can set which part of the graph to display (as usual with xlim and ylim) and if the two axis are dependent on each other.

基本上,你可以设置ylim和xlim和asp。您可以设置要显示的图形的哪个部分(与xlim和ylim一样)以及两个轴是否相互依赖。

plot(g, rescale = FALSE, ylim=c(1,4),xlim=c(-17,24), asp = 0)

#2


1  

Option 1: make the vertices smaller

选项1:使顶点更小

node.size= c(10,10,10)
plot(net, vertex.size=node.size*0.25)

Option 2 (in case the distances between the vertices are not important to you):

选项2(如果顶点之间的距离对您不重要):

# Use the tkplot option to edit your graph in GUI
tkplot (net)

Note: tkplot outputs the graph as eps. If you want to edit it further or export it to pdf I suggest using inkscape (I use it for all my graph editing - just save the graph as pdf in RStudio and edit it in inkscape). For the case of eps if you are on a windows machine you will need to tweak inkscape to open this format. A very short and simple process which is detailed here:

注意:tkplot将图形输出为eps。如果你想进一步编辑它或将其导出为pdf我建议使用inkscape(我将它用于我的所有图形编辑 - 只需将图形保存为RSt中的pdf并在inkscape中编辑它)。对于eps的情况,如果你在Windows机器上,你需要调整inkscape来打开这种格式。这是一个非常简短的过程,详细说明如下:

#1


2  

It is NOT my answer, just found on *:
igraph axes xlim ylim plot incorrectly

这不是我的答案,只是在*上找到:igraph axis xlim ylim plot错误

Basically, you can set ylim and xlim and asp. You can set which part of the graph to display (as usual with xlim and ylim) and if the two axis are dependent on each other.

基本上,你可以设置ylim和xlim和asp。您可以设置要显示的图形的哪个部分(与xlim和ylim一样)以及两个轴是否相互依赖。

plot(g, rescale = FALSE, ylim=c(1,4),xlim=c(-17,24), asp = 0)

#2


1  

Option 1: make the vertices smaller

选项1:使顶点更小

node.size= c(10,10,10)
plot(net, vertex.size=node.size*0.25)

Option 2 (in case the distances between the vertices are not important to you):

选项2(如果顶点之间的距离对您不重要):

# Use the tkplot option to edit your graph in GUI
tkplot (net)

Note: tkplot outputs the graph as eps. If you want to edit it further or export it to pdf I suggest using inkscape (I use it for all my graph editing - just save the graph as pdf in RStudio and edit it in inkscape). For the case of eps if you are on a windows machine you will need to tweak inkscape to open this format. A very short and simple process which is detailed here:

注意:tkplot将图形输出为eps。如果你想进一步编辑它或将其导出为pdf我建议使用inkscape(我将它用于我的所有图形编辑 - 只需将图形保存为RSt中的pdf并在inkscape中编辑它)。对于eps的情况,如果你在Windows机器上,你需要调整inkscape来打开这种格式。这是一个非常简短的过程,详细说明如下: