生成igraph布局时修复y坐标

时间:2017-07-20 11:18:48

标签: r layout igraph

我正在尝试使用R包igraph生成树状图。我从邻接矩阵Y开始,看起来像这样:

Y <- matrix(c(1,1,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,
              1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,
              0,0,0,0,0,0,0,1,1,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,1,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,
              0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,0,0,0,0,1,0,1,0,
              0,0,0,0,0,0,1,0,0,0,1,0,1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,
              0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,
              0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,
              0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,1,0,1,
              0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,
              0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,
              0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,
              0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1),
              ncol=25)

然后我使用

创建图表
g <- graph_from_adjacency_matrix(Y,mode="undirected",diag=F)

现在我想绘制一个树状图,第一个节点位于树顶部,我为此做了

L <- layout.reingold.tilford(g,root=1)
plot.igraph(g,layout=L)

结果看起来非常接近我要找的东西,但现在我想为顶点使用自定义y坐标。原因是我希望y轴有意义:我对每个顶点都有score值,我希望y坐标与该分数成比例。当然,我可以通过简单地使用

来实现
L[,2] <- score

问题在于,通过这样做,我弄乱了绘图,获得了重叠的顶点和相交的边缘,这是不可取的。我可以通过用tkplot手动调整x坐标来解决它,但最终我需要自动生成几个图并逐个手动检查它们是不可行的。

所以问题是,是否有任何方法可以获得最佳的&#34;使用预定义的顶点y坐标的树图表示?

希望我已经足够清楚了。提前感谢您的时间!

Ĵ

编辑:如果我dput我的图变量g,这就是我得到的(请注意scores已作为顶点属性添加,并且可以通过V(g)$scores加入)

> dput(g)    
structure(list(25, FALSE, c(1, 2, 5, 4, 6, 3, 12, 8, 11, 13, 
14, 17, 7, 9, 19, 20, 10, 18, 22, 24, 15, 16, 21, 23), c(0, 0, 
0, 1, 1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 7, 7, 8, 8, 8, 8, 13, 13, 
16, 16), c(0, 1, 5, 3, 2, 4, 12, 7, 13, 16, 8, 6, 9, 10, 20, 
21, 11, 17, 14, 15, 22, 18, 23, 19), c(0, 1, 2, 3, 4, 5, 6, 7, 
8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23), 
    c(0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 
    16, 17, 18, 19, 20, 21, 22, 23, 24), c(0, 3, 5, 7, 9, 12, 
    14, 14, 16, 20, 20, 20, 20, 20, 22, 22, 22, 24, 24, 24, 24, 
    24, 24, 24, 24, 24), list(c(1, 0, 1), structure(list(), .Names = character(0)), 
        structure(list(name = c("GO:0008150", "GO:0050896", "GO:0044699", 
        "GO:0044767", "GO:0051716", "GO:0016043", "GO:0002544", 
        "GO:0071822", "GO:0048513", "GO:0007044", "GO:0007517", 
        "GO:0030855", "GO:0006816", "GO:0007165", "GO:0042178", 
        "GO:0007169", "GO:0009966", "GO:0070458", "GO:0090131", 
        "GO:0007052", "GO:0006335", "GO:2000587", "GO:0045653", 
        "GO:0070372", "GO:0003257"), scores = c(0, 0.301029995663981, 
        0.301029995663981, 0.602059991327962, 0.602059991327962, 
        0.602059991327962, 0.778151250383644, 0.778151250383644, 
        1, 1.04139268515822, 1.07918124604762, 1.07918124604762, 
        1.11394335230684, 1.11394335230684, 1.14612803567824, 
        1.20411998265592, 1.23044892137827, 1.25527250510331, 
        1.25527250510331, 1.27875360095283, 1.39794000867204, 
        1.50514997831991, 1.63346845557959, 1.70757017609794, 
        1.96378782734556), color = c("black", "black", "black", 
        "black", "black", "black", "red", "black", "black", "red", 
        "red", "red", "red", "black", "red", "red", "black", 
        "red", "red", "red", "red", "red", "red", "red", "red"
        )), .Names = c("name", "scores", "color")), list()), 
    <environment>), class = "igraph")

1 个答案:

答案 0 :(得分:0)

由于layout_with_sugiyama不起作用,我打算提出更多的黑客攻击, 或者更确切地说是两个黑客,一个建筑在另一个。

首先,稍加清洁。使用g的dput版本,我收到警告

graph was created by an old(er) igraph version.
Call upgrade_graph() on it to use with the current igraph version
For now we convert it on the fly...

所以我从g = upgrade_graph(g)开始 接下来,当我绘制图表时,我发现很难阅读因为 黑色节点上的海军文字。所以,我正在将您的节点更改为灰色。 如果你愿意,你可以把它留下来。
V(g)$color[V(g)$color == "black"] = "#888888"

现在我们需要布局图形。我会像你一样开始 莱因戈尔德-蒂尔福德
L <- layout.reingold.tilford(g,root=1)
但这不会使用您的scores。当然,布局,只是给出 节点的x-y位置,因此第一个黑客只是覆盖 y值和你的分数。

L2 = L
L2[,2] = V(g)$scores 
plot(g,layout=L2)

情节有点挤在绘图区的中心。所以我会 摆脱边缘。

par(mar=rep(0,4))
plot(g,layout=L2)

First plot of graph

这不是太糟糕,但是一些节点标签仍然重叠。 第二个黑客是我将“手动”移动这些。

L3 = L2
L3[18,1] = -2.2
L3[19,1] = -1.1
L3[12,1] = 0
L3[13,1] = 1
plot(g,layout=L3)

此时,高度与您的分数成正比,没有标签重叠。

Cleaner Plot