Igraph: positioning labels and removing blank space in the grid

I want to create a function that displays oriented graphs with aligned vertices and adds text under each vertex, i.e. something like the example below. The plotting function should be able to process graphical charts as vertices, so I used the igraph package in R.

 adjm <- matrix(c(0,1,0,1, 0,0,1,1, 0,0,0,1, 0,0,0,0), nrow=4, byrow=TRUE) g1 <- graph.adjacency(adjm, mode="directed") values <- lapply(1:4, function(x) sample(1:4,4)) windows(width=7, height=3.5) plot(g1, layout=layout.grid(g1, width=4), vertex.shape="pie", vertex.pie=values, vertex.pie.color=list(heat.colors(4)), edge.curved=TRUE, vertex.label=LETTERS[1:4], vertex.size=50, vertex.label.dist=5, vertex.label.degree=pi/2, edge.label=1:5, rescale=FALSE, xlim=c(-0.2,3.5), ylim=c(0,0.5)) 

enter image description here

I have two problems.

  • Why, the longer the vertex mark, the greater the distance to the center of the vertex, as in the following example? Can I make them appear on the same level without manually searching and identifying them at the right distance?

     windows(width=7, height=3.5) plot(g1, layout=layout.grid(g1, width=4), vertex.shape="pie", vertex.pie=values, vertex.pie.color=list(heat.colors(4)), edge.curved=TRUE, vertex.label=c("A", "AA", "AAAA", "AAAAAA"), vertex.size=50, vertex.label.dist=5, vertex.label.degree=pi/2, edge.label=1:5, rescale=FALSE, xlim=c(-0.2,3.5), ylim=c(0,0.5)) 

    enter image description here

  • How to remove empty space when building aligned graphs? If I make a horizontal graph, it will be located at the bottom of the window with a lot of empty space. The denser the image, the smaller the graph, as in the third plot. (It seems that the size of the graph is determined so that the graph matches any position, vertical or horizontal.)

     plot(g1, layout=layout.grid(g1, width=4), vertex.shape="pie", vertex.pie=values, vertex.pie.color=list(heat.colors(4)), edge.curved=TRUE, vertex.label=c("A", "AA", "AAAA", "AAAAAA"), vertex.size=20, vertex.label.dist=2, vertex.label.degree=pi/2, edge.label=1:5) 

    enter image description here I can write the argument asp=0 to keep the correct vertex sizes in a denser window, but the automatic curvatures and positions of the vertex labels change. I can manually adjust the negative top edge, marks, distances and edge curvature, but of course this is not a solution for the general function. The best result so far has been the result of rescale=FALSE , as in the code for the first graph, but the curvatures of the edges are still too small and with large vertices they completely go beyond the vertices. (Also, the problem with different spacing between vertex labels still remains.) Direct use of autocurve.edges does not give me curvature. Any ideas on what to do next?

+6
source share
1 answer

To fix this, you can tweak the plot.igraph function a plot.igraph . To do this, you can experiment with trace(plot.igraph,edit=TRUE) and change the function code.

A few tips to get what you want: For labels, line 319 of the function:

 y <- layout[, 2] + label.dist * sin(-label.degree) * (vertex.size + 6 * 8 * log10(nchar(labels) + 1))/200 

So, placing the y labels is proportional to the number of label characters, just delete log10(nchar(labels) + 1) and the labels will remain at the same level.

For size problems, if you want to keep rescale , the y position of the chart is determined by line 55

 layout <- layout.norm(layout, -1, 1, -1, 1) 

You can change the third number to whatever you want, and the graph will be higher (for example, layout <- layout.norm(layout, -1, 1, 0, 1) will center the vertexes on 0). You can also change the margins by changing the layout <- layout.norm(layout, -1, 1, 0, 1) will center the vertexes on 0). You can also change the margins by changing the par`. You can, for example, do:

 oldMargins<-par("mar") par(mar=c(10,4,4,4)) 

and after the graph par(mar=oldMargins)

This is the graph that I get with this code after settings:

enter image description here

I left the axes to simplify the setup and remove the pies, because there was no value in your message.

 oldMargins<-par("mar") par(mar=c(10,4,4,4)) plot(g1, layout=layout.grid(g1, width=4), vertex.pie.color=list(heat.colors(4)), edge.curved=TRUE, vertex.label=c("A", "AA", "AAAA", "AAAAAA"), vertex.size=20, vertex.label.dist=1, vertex.label.degree=pi/2, edge.label=1:5,axes=TRUE, ylim=c(-0.5,0.5),xlim=c(-1,1)) par(mar=oldMargins) 

When you restart R, any changes made to the function will be deleted, or you can use untrace to return to the original function. You can see this post for more information.

+4
source

Source: https://habr.com/ru/post/982559/


All Articles