igraph:定位标签和在网格布局中删除空白空间

7

我想编写一个能够绘制有定向的图形并使每个顶点对齐并在下面添加一些文本的函数,例如下面的示例绘图。绘图功能应该能够处理将饼状图作为顶点,所以我使用了R语言中的igraph包。

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.

  1. Why is it that the longer the vertex label, the longer the distance to the vertex centre as in the following example? Can I force them to appear at the same level without manually finding and defining them 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

  2. How to remove empty space when plotting aligned plots? If I make a horizontal graph it will be placed at the bottom of the window with a lot of empty space. The flatter the image, the smaller the graph, as in the third plot. (It seems that the size of the graph is determined such that the plot would fit in 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 argument asp=0 to keep the vertices the right size in a flatter window but the automatic curvatures and the positions of the vertex labels change. I can manually tweak negative upper margin, label distances, and edge curvature, but of course this is not a solution for a generic function. The best result so far was the result of rescale=FALSE as in the code for the first plot, but the edge curvatures are still too small and with larger vertices they get behind the vertices completely. (Also, the problem with varying vertex label distances still remains.) Direct use of autocurve.edges is not giving me any curvature. Any ideas of what to try next?

1个回答

5
为了解决这个问题,你可能需要微调plot.igraph函数。为此,你可以尝试使用trace(plot.igraph,edit=TRUE)进行实验并修改函数的代码。
以下是几个小技巧以帮助你达成目标: 对于标签,函数中的第319行代码如下:
y <- layout[, 2] + label.dist * sin(-label.degree) * (vertex.size + 6 * 8 * log10(nchar(labels) + 1))/200

因此,标签的 y 位置与标签字符数成比例,只需删除log10(nchar(labels)+1),标签将保持在同一水平。

对于大小问题,如果要保留rescale,则图的 y ​​位置在第55行中定义。

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

您可以更改第三个数字为任何您想要的数字,这将使绘图更高(例如,layout <- layout.norm(layout, -1, 1, 0, 1)将使顶点居中于0)。您也可以通过更改par设置来更改边距。例如:

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

在执行 par(mar=oldMargins) 之后,生成的图表如下:

enter image description here

为了便于微调,我保留了坐标轴,并删除了饼图,因为您在帖子中没有给出任何值。

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)

当您重新启动R时,您对函数所做的任何更改都将被删除,或者您可以使用untrace返回到原始函数。您可以查看此帖子获取更多信息。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接