如何使用固定位置控制 igraph 绘图布局?
我正在尝试绘制一个类似于流程图的网络可视化。我非常接近以下代码,但我有几个问题:
- 这是最好的布局()算法吗?或者我可以手动为每个节点分配一个位置吗?
- 我如何确保这些节点在图中不会重叠(正如它们在此处所做的那样)?
- 我可以将一个节点指定为“锚点”或起点吗?即,我可以将“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)
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:
- Is this the best layout() algorithm, or can I manually assign a position for each node>
- How can I make sure that these nodes don't overlap in the plot (as they do here)?
- Can I assign one node as an "anchor" or starting point? i.e., can I make "C" the top-most or left-most node?
Thanks so much!!
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
igraph 中的布局是在一个矩阵中定义的,每个节点有 2 列和一行。第一列表示它的 x 位置,第二列表示它的 y 位置,并且比例不相关(它总是重新缩放以适合 -1 到 1 的绘图区域。您可以在绘图之前通过调用图形上的布局函数来获取此布局:
这样您可以手动更改它,然后将其发送到绘图:
似乎您还可以设置参数
params
来控制布局 abit 这是一个包含的列表。一个显然可以用来设置图的根的参数(记住 igraph 使用类似 C 的索引,第一个是 0)。根位于“C”:编辑:
RGraphViz
中还有一些不错的树布局,可能值得检查一下。编辑 2:
这是我的包中源代码的修改片段,其中。使用相同类型的布局矩阵来定义图形中节点的位置,您可能会发现它很有用:
此函数的作用是转换指定网格中布局的矩阵(类似于
layout()
)具有 x 和 y 位置的两列布局。定义一个由零组成的矩阵,并为每个节点定义从 1 到节点总数的整数(这是 igraph ID + 1 )。例如,对于一个愚蠢的 4 节点图:
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:
This way you can change it in any way you want manually, and then send it to the plot:
It also seems that you can set the argument
params
to control the layout abit. This is a list containing an argumentroot
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":EDIT: Also the
RGraphViz
has some nice tree-layouts in it that might be worth checking out.EDIT 2:
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:
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 ).For example, for a silly 4 node graph:
如果您想自己分配节点位置,比上述方法简单的方法是在数据表中添加标记为 x 和 y 的列,并在这些列中添加相应节点的 x 和 y 坐标。例如
A less complicated method than the above if you want to assign the node locations yourself is to add columns labelled x and y in your datasheet with the x and y coordinates for the respective nodes in those columns. e.g.
比上面更简单的方法是直接使用坐标矩阵:
An even less complicated method than above is to use the coordinate matrix directly: