如何保存具有预定义布局的图形对象的边缘列表? [英] How to save the edge list of igraph object with the predefined layout?
本文介绍了如何保存具有预定义布局的图形对象的边缘列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经读取了R igraph - save layout?,但在我的情况下,它被要求将Begin和End的边的位置与边列表一起保存到一个文件中。
我有tree
图形对象和平面上预定义的mylayout
布局。
tree <- make_tree(5, 2, mode = "undirected")
mylayout <- matrix(c(1, 2, 0, 3, 2,
1, 2, 0, 2, 3), ncol=2)
我添加了一个新属性name
tree <- make_tree(5, 2, mode = "undirected") %>%
set_vertex_attr("name", value = seq(1:vcount(tree)))
通过get.edgelist()
函数得到图的边列表,我将使用name
属性:
df1 <- data.frame(V1 = get.edgelist(tree)[,1],
V2 = get.edgelist(tree)[,2],
# V1_x = mylayout[as.integer(names(V(tree))), 1],
# V1_y = mylayout[as.integer(names(V(tree))), 2],
# V2_x = mylayout[, 1],
# V2_y = mylayout[, 2],
stringsAsFactors = FALSE)
问题。如何将节点位置与边的开始和结束位置匹配?
推荐答案
我不知道是否有现有的方法来完成此操作,但编写帮助器函数来完成此操作并不需要太多工作
join_layout <- function(g, layout) {
edges <- as_edgelist(g)
idx1 <- match(edges[,1], V(g)$name)
idx2 <- match(edges[,2], V(g)$name)
result <- cbind(data.frame(edges),
layout[idx1, ],
layout[idx2, ]
)
names(result) <- c("V1", "V2", "V1_x", "V1_y", "V2_x","V2_y")
result
}
基本上,我们使用match()
将顶点名称与布局矩阵中的行进行匹配。您可以通过传入igraph
对象和布局来调用它
join_layout(tree, mylayout)
# V1 V2 V1_x V1_y V2_x V2_y
# 1 1 2 1 1 2 2
# 2 1 3 1 1 0 0
# 3 2 4 2 2 3 2
# 4 2 5 2 2 2 3
这篇关于如何保存具有预定义布局的图形对象的边缘列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文