简体   繁体   English

在R中的igraph中均匀间隔顶点

[英]spacing vertices evenly in igraph in r

I have the following graph graph 我有以下图

set.seed(1410)
df<-data.frame(
"site.x"=c(rep("a",3),rep("b",3),rep("c",3),rep("d",3)),
"site.y"=c(rep(c("e","f","g"),4)),
"bond.strength"=sample(1:100,12, replace=TRUE))

library(igraph)
df<-graph.data.frame(df)
V(df)$names <- c("a","b","c","d","e","f","g")
layOUT<-data.frame(x=c(rep(1,4),rep(2,3)),y=c(4:1,3:1))
E(df)[ bond.strength < 101 ]$color <- "red"
E(df)[ bond.strength < 67 ]$color <- "yellow"
E(df)[ bond.strength < 34 ]$color <- "green"
V(df)$color <- "white"
l<-as.matrix(layOUT)
plot(df,layout=l,vertex.size=10,vertex.label=V(df)$names,
edge.arrow.size=0.01,vertex.label.color = "black")

在此处输入图片说明

I would like to space the vertices "ge" evenly along the vertical distance between vertex a and d to make my current graph (see below) prettier. 我想将顶点“ ge”沿着顶点a和d之间的垂直距离均匀分布,以使我的当前图形(见下文)更漂亮。 As you can see it is pretty crowded. 如您所见,它非常拥挤。

在此处输入图片说明

Also I would like to move the two column of vertices closer together on the x-axis but I have noticed that adjusting the x coordinates in the layout is not responding. 另外,我想将两列顶点在x轴上移动得更近一些,但是我注意到在布局中调整x坐标没有响应。 For example the two following layouts produce graph that look exactly the same despite the drastic adjustment in the x-coordinates. 例如,以下两个布局生成的图形尽管在x坐标中进行了大幅度的调整,但看起来却完全相同。

layOUT<-data.frame(x=c(rep(1,4),rep(2,3)),y=c(4:1,3:1))

layOUT<-data.frame(x=c(rep(1,4),rep(100,3)),y=c(4:1,3:1)) 

Thanks for any advice you may have. 感谢您的任何建议。

Your second question is easier to answer: igraph rescales the layout into the rectangle spanned by (-1, -1) and (1,1) in the coordinate system. 您的第二个问题更容易回答:igraph将布局重新缩放为在坐标系中由(-1,-1)和(1,1)跨越的矩形。 If you want to avoid this, pass rescale=FALSE to the plot function -- but in this case, it is up to you to ensure that the coordinates actually make sense and are not outside the plot area. 如果要避免这种情况,请将rescale=FALSE传递给plot函数-但在这种情况下,您可以确保坐标确实有意义并且不在绘图区域之外。

Regarding your first question: since you are constructing the layout manually in the layOUT variable, nothing prevents you from adjusting the Y coordinates manually. 关于第一个问题:由于您是在layOUT变量中手动构造布局,因此没有什么可以阻止您手动调整Y坐标。 First, get the minimum and maximum Y coordinates for the vertices with X=1 from layOUT : 首先,从layOUT X = 1的顶点的最小和最大Y坐标:

min.y <- min(layOUT$y[layOUT$x == 1])
max.y <- max(layOUT$y[layOUT$x == 1])

Then just space the Y coordinates of the vertices with an X coordinate of 2 evenly between min.y and max.y: 然后,将顶点的Y坐标与X坐标2均匀地分布在最小y和最大y之间:

vs.on.right <- which(layOUT$x == 2)
n <- length(vs.on.right)
layOUT$y[vs.on.right] <- (0:n)*(max.y-min.y)/n + min.y

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

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