简体   繁体   English

在R中改变iGraph中顶点之间的间距

[英]changing the spacing between vertices in iGraph in R

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. I wonder if there is a way to change the spacing between vertices. 我想知道是否有办法改变顶点之间的间距。

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. 例如,顶点3,4和5彼此非常接近,但5和6相距很远。

EDIT: 编辑:

For my 2nd question, I guess the spacing is dependent on the number of nodes below. 对于我的第二个问题,我猜间距取决于下面的节点数。 Eg, 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. 例如,10和11比8和9更远是因为10和11以下的孩子比8和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. 由于似乎缺少宽度的一般参数,您必须手动调整参数以获得所需的输出。

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). 这里最重要的参数是控制绘图宽高比的asp参数(因为在这种情况下,我认为曲线图比长高更好,甚至小于0.5的宽高比是正确的)。 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: 这产生了这个情节: 在此输入图像描述

another approach would be to set graphical devices to PDF (or JPEG etc.) and then set the rescale to FALSE . 另一种方法是将图形设备设置为PDF(或JPEG等),然后将rescale设置为FALSE With Rstudio viewer this cut off a huge piece of the data but with other graphic devices it might (not guarantee) work well. 使用Rstudio查看器可以截断大量数据,但是对于其他图形设备,它可能 (不能保证)运行良好。

Anyway for every doubt about how to use these parameters (that are very tricky sometimes) type help(igraph.plotting) 无论如何对于如何使用这些参数(有时候非常棘手)类型的help(igraph.plotting)

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. 对于问题的第二部分,我不确定但是在函数内部我无法找出一个精确的答案,但我想同一级别的元素之间的空间是根据他们拥有的子元素计算的,比如说3,4,5必须更近,因为他们有孩子和子孩子,然后他们需要更多的空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM