在R中的iGraph中更改顶点之间的间距 [英] changing the spacing between vertices in iGraph in R

查看:470
本文介绍了在R中的iGraph中更改顶点之间的间距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我想用以下数据作图:

Suppose I want to make a plot with the following data:

pairs <- c(1, 2, 2, 3, 2, 4, 2, 5, 2, 6, 2, 7, 2, 8, 2, 9, 2, 10, 2, 11, 4, 
           14, 4, 15, 6, 13, 6, 19, 6, 28, 6, 36, 7, 16, 7, 23, 7, 26, 7, 33,
           7, 39, 7, 43, 8, 35, 8, 40, 9, 21, 9, 22, 9, 25, 9, 27, 9, 33, 9,
           38, 10, 12, 10, 18, 10, 20, 10, 32, 10, 34, 10, 37, 10, 44, 10, 45,
           10, 46, 11, 17, 11, 24, 11, 29, 11, 30, 11, 31, 11, 33, 11, 41, 11, 
           42, 11, 47, 14, 50, 14, 52, 14, 54, 14, 55, 14, 56, 14, 57, 14, 58,
           14, 59, 14, 60, 14, 61, 15, 48, 15, 49, 15, 51, 15, 53, 15, 62, 15, 
           63)

g <- graph( pairs )
plot( g,layout = layout.reingold.tilford )

我得到如下图所示的情节:

I get a plot like the one below:

如您所见,某些顶点之间的间距太小,以至于这些顶点重叠.

As you can see the spaces between some of the vertices are so small that these vertices overlap.

1.我想知道是否有办法改变顶点之间的间距.

2.另外,顶点之间的间距是任意的吗?例如,顶点3、4和5彼此非常接近,而顶点5和6彼此相距很远.

2. In addition, is the spacing between vertices arbitrary? For example, Vertices 3, 4, and 5 are very close to each other, but 5 and 6 are far apart.

对于第二个问题,我猜想间距取决于下面的节点数.例如,10和11比8和9距离更远是因为10和11以下的孩子比8和9以下的孩子多.

For my 2nd question, I guess the spacing is dependent on the number of nodes below. E.g., 10 and 11 are farther from each other than 8 and 9 are because there are more children below 10 and 11 than there are below 8 and 9.

推荐答案

我敢打赌,有更好的解决方案,但我找不到.这是我的方法.由于似乎缺少通用的宽度参数,因此您必须手动调整参数以获得所需的输出.

I bet there is a better solution but I cannot find it. Here my approach. Since seems that a general parameter for width is missing you have to adjust manually parameters in order to obtain the desired output.

我的方法主要是调整图的某些元素的大小,以使其具有正确的大小,调整边距,以尽可能地优化空间.这里最重要的参数是控制图长宽比的asp参数(因为在这种情况下,我猜图长于高,长宽比甚至小于0.5是正确的).其他技巧是减小顶点和字体的大小.这是代码:

My approach is primarily to resize some elements of the plot in order to make them of the right size, adjust margins in order to optimize the space as much as possible. The most important parameter here is the asp parameter that controls the aspect ratio of the plot (since in this case the plot I guess is better long than tall an aspect ratio of even less than 0.5 is right). Other tricks are to diminish the size of vertex and fonts. Here is the code:

plot( g, layout = layout.reingold.tilford,
      edge.width = 1,
      edge.arrow.width = 0.3,
      vertex.size = 5,
      edge.arrow.size = 0.5,
      vertex.size2 = 3,
      vertex.label.cex = 1,
      asp = 0.35,
      margin = -0.1)

这将产生以下情节:

That produces this plot:

另一种方法是将图形设备设置为PDF(或JPEG等),然后将rescale设置为FALSE.使用Rstudio Viewer可以截取大量数据,但是对于其他图形设备,它可能(不能保证)可以很好地工作.

another approach would be to set graphical devices to PDF (or JPEG etc.) and then set the rescale to FALSE. With Rstudio viewer this cut off a huge piece of the data but with other graphic devices it might (not guarantee) work well.

无论如何对这些参数的使用有任何疑问(有时非常棘手),请键入help(igraph.plotting)

Anyway for every doubt about how to use these parameters (that are very tricky sometimes) type help(igraph.plotting)

对于问题的第二部分,我不确定,但是从函数内部看时,我无法找出确切的答案,但是我猜想,同一级别上元素之间的间隔是根据它们具有的子元素(例如3)计算得出的, 4,5必须靠近,因为他们有孩子和子孩子,然后他们需要更多的空间.

For the second part of the question I am not sure but looking inside the function I cannot figure out a precise answer but I guess that the space between elements on the same level is calculated on the child elements they have, say 3,4,5 have to be closer because they have child and sub-child and then they require more space.

这篇关于在R中的iGraph中更改顶点之间的间距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆