如何使用固定位置控制igraph绘图布局? [英] How to control the igraph plot layout with Fixed Positions?

查看:381
本文介绍了如何使用固定位置控制igraph绘图布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试绘制类似于流程图的网络可视化.我与以下代码相当接近,但是我有几个问题:

I am trying to draw a network visualization to resemble a flow diagram. I'm fairly close with the following code, but I have a couple questions:

  1. 这是最好的layout()算法,还是可以为每个节点手动分配位置>
  2. 如何确保这些节点在图中不重叠(如此处所示)?
  3. 是否可以将一个节点分配为锚点"或起点?即可以将"C"设置为最顶部还是最左侧的节点?

非常感谢!

library("igraph")
L3 <- LETTERS[1:8]
d <- data.frame(start = sample(L3, 16, replace = T), end = sample(L3, 16, replace = T),
                weight = c(20,40,20,30,50,60,20,30,20,40,20,30,50,60,20,30))


g <- graph.data.frame(d, directed = T)

V(g)$name 
E(g)$weight

ideg <- degree(g, mode = "in", loops = F)

col=rainbow(12) # For edge colors

plot.igraph(g, 
  vertex.label = V(g)$name, vertex.label.color = "gray20",
  vertex.size = ideg*25 + 40, vertex.size2 = 30,
  vertex.color = "gray90", vertex.frame.color = "gray20",
  vertex.shape = "rectangle",
  edge.arrow.size=0.5, edge.color=col, edge.width = E(g)$weight / 10,
  edge.curved = T, 
  layout = layout.reingold.tilford)

推荐答案

igraph中的布局是在矩阵中定义的,每个节点有2列和1行.第一列指示其x位置,第二列指示y位置,并且比例无关紧要(它总是重新缩放以适合-1到1的绘图区域.您可以在绘图之前通过调用图形上的layout函数来获得此布局. :

The layout in igraph is defined in a matrix with 2 columns and a row for each node. The first column indicates its x position and the second its y position, and scale is not relevant (it is always rescaled to fit a -1 to 1 plotting area. You can get this layout before plotting by just calling the layout function on the graph:

 l <-layout.reingold.tilford(g) 
 l
     [,1] [,2]
[1,]    0    0
[2,]   -1    3
[3,]    0    1
[4,]    0    3
[5,]    0    2
[6,]    0    4
[7,]    1    3

通过这种方式,您可以按照自己想要的任何方式对其进行更改,然后将其发送到绘图:

This way you can change it in any way you want manually, and then send it to the plot:

plot.igraph(g, 
  vertex.label = V(g)$name, vertex.label.color = "gray20",
  vertex.size = ideg*25 + 40, vertex.size2 = 30,
  vertex.color = "gray90", vertex.frame.color = "gray20",
  vertex.shape = "rectangle",
  edge.arrow.size=0.5, edge.color=col, edge.width = E(g)$weight / 10,
  edge.curved = T, 
  layout = l)

似乎您还可以设置参数params来控制布局.这是一个包含参数root的列表,显然可以用于设置图的根.为该节点分配一个编号(igraph使用C索引作为索引的节点,第一个为0).因此,将根设置为"C":

It also seems that you can set the argument params to control the layout abit. This is a list containing an argument root that apparently can be used to set the root of the graph. Assign this a number of the node (renember that igraph uses C like indexes for nodes, first one is 0). So setting the root at "C":

l <- layout.reingold.tilford(g,params=list(root=2))

RGraphViz里面也有一些不错的树形布局,可能值得一看.

Also the RGraphViz has some nice tree-layouts in it that might be worth checking out.

这是我程序包中源代码的修改片段,它使用相同的布局矩阵来定义图中节点的位置,您可能会发现它有用:

This is a modified snippet of the source codes from my package, which uses a same kind of layout matrix to define placement of nodes in a graph, that you might find useful:

gridLayout <- function(x)
{
    LmatX <- seq(-1,1,length=ncol(x))
    LmatY <- seq(1,-1,length=nrow(x))

    loc <- t(sapply(1:max(x),function(y)which(x==y,arr.ind=T)))
    layout <- cbind(LmatX[loc[,2]],LmatY[loc[,1]])
    return(layout)
}

此函数的作用是将指定网格中布局的矩阵(类似于layout())转换为具有x和y位置的两列布局.定义一个零矩阵,并为每个节点从1到节点总数(这是igraph ID +1)的整数.

What this function does is transform a matrix specifying the layout in a grid (similar to layout()) to a two-column layout with x and y positions. Define a matrix of zeros and for each node integer from 1 to the total number of nodes ( this is the igraph ID + 1 ).

例如,对于一个愚蠢的4节点图:

For example, for a silly 4 node graph:

grid <- matrix(c(
    0,0,1,0,0,
    2,0,3,0,4),nrow=2,byrow=TRUE)

library("igraph")

g <- graph.adjacency(matrix(1,4,4))

plot(g,layout=gridLayout(L))

这篇关于如何使用固定位置控制igraph绘图布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆