简体   繁体   中英

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. 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. For example the two following layouts produce graph that look exactly the same despite the drastic adjustment in the x-coordinates.

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. 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.

Regarding your first question: since you are constructing the layout manually in the layOUT variable, nothing prevents you from adjusting the Y coordinates manually. First, get the minimum and maximum Y coordinates for the vertices with X=1 from layOUT :

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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