1

我有一个大型空间显式 igraph 对象(2746 个节点/ 3205 个边),显示集水区中的沉积物输送路径。使用命令 all_simple_paths,我提取了从定义的源节点开始并最终在集水区出口的所有路径(使用 lapply)。all_simple_paths 的结果是 1841 个元素的列表,每个元素显示相应路径的顶点 ID。现在我正在努力绘制这些路径。我想在一张图像中以空间明确的方式绘制所有路径。就像到达出口的所有路径组成的图的一个子集。

“情节”给出了错误: Error in xy.coords(x, y, xlabel, ylabel, log) : 'x' is a list, but does not have components 'x' and 'y' 我用“ggraph”绘制的完整图表,但对于“出口图表”,这个命令也给出了一个错误: Error in create_layout.default(graph, layout, ...) : No layout function defined for objects of class list

可能这很容易解决,但我已经尝试了各种可能性,并没有设法得到情节。

这里有一点(希望不要太简化)的例子:

`library(igraph)

a <- c(1,2,2,3,4,4,7,7,7)
b <- c(2,3,10,4,5,6,8,9,3)
c <- c("rf","se","se","ft","fd","ft","st", "st","st")

edges <- cbind(a,b,c)

id <- c(1,2,3,4,5,6,7,8,9,10)
x <- c(623096,622839,622475,622581,622480,622376,620313,621551,621142,622927) 
y <- c(5149975,5150159,5150591,5151056,5151367,5151399,5150039,5150077,5150649,5150274)

nodes <- cbind(id,x,y)

my_graph <- graph_from_data_frame(edges, directed = TRUE,vertices = nodes)

plot(my_graph)

graph_outlet <- all_simple_paths(my_graph,from=1,to= 6,mode = "out")
plot (graph_outlet)`

非常感谢已经提前!

4

2 回答 2

2

您可以通过制作每个路径的子图来绘制相应的路径,graph_outlet如下所示:

sub_graphs <- lapply(graph_outlet, function(vs) induced_subgraph(my_graph, vs))
plot(sub_graphs[[1]])

请注意各种 igraph 函数返回的对象类型。查看示例代码中的这些行:

graph_outlet <- all_simple_paths(my_graph,from=1,to= 6,mode = "out")
class(graph_outlet[[1]])
class(my_graph)

编辑:

我意识到您试图在同一张图中可视化所有路径。我对您的示例数据进行了一个小操作,以在顶点 1 和 6 之间创建多条路径,然后在整个图中为顶点和边着色以突出显示路径:

# Your example data with path between 4-->10
a <- c(1,2,2,3,4,4,7,7,7,10)
b <- c(2,3,10,4,5,6,8,9,3,4)
c <- c("rf","se","se","ft","fd","ft","st", "st","st","st")
edges <- cbind(a,b,c)
id <- c(1,2,3,4,5,6,7,8,9,10)
x <- c(623096,622839,622475,622581,622480,622376,620313,621551,621142,622927) 
y <- c(5149975,5150159,5150591,5151056,5151367,5151399,5150039,5150077,5150649,5150274)
nodes <- cbind(id,x,y)
my_graph <- graph_from_data_frame(edges, directed = TRUE,vertices = nodes)

# Preferences
FROM_V <- 1
TO_V <- 6

# Calculate all simple paths from FROM_V to TO_V as list of vertecy sequences
graph_outlet <- all_simple_paths(my_graph,from=FROM_V,to=TO_V, mode = "out")

# Build sub-graphs to test
sub_graphs <- lapply(graph_outlet, function(vs) induced_subgraph(my_graph, vs))
plot(sub_graphs[[1]])

plot(my_graph)
# Colour and style vertecies
V(my_graph)$color <- "white"
V(my_graph)$color[unique(unlist(graph_outlet))] <- "gray"
V(my_graph)$color[c(FROM_V,TO_V)] <- "yellow"
# Colour each of the paths
E(my_graph)$color <- "gray"
lapply(graph_outlet, function(x) E(my_graph, path=x)$color <<- "black")

# Plot all paths and mark the group of vertecies through which paths flow
plot(my_graph)
plot(my_graph, mark.groups=unique(unlist(graph_outlet)))
于 2020-03-02T11:10:02.563 回答
0

我自己设法找到了答案;)发布它以防万一有一天有人可能会遇到同样的问题。

这是我现在在完整数据集上应用的代码。结果“subbi”是一个 igraph 对象,因此可以用 ggraph 绘制它。(V_source 是预定义的源节点,outlet_node 是路径结束的节点):

paths2 <- lapply(V_source[1:length(V_source)] , function(x) all_simple_paths(my_graph, x , to=outlet_node, mode = "out") )
A1 <- unlist(paths2, recursive = FALSE)
C <- list()
for (i in 1:length(A1)) {
  C[[i]] = E(graph=my_graph, path=A1[[i]])$ EdgeID 
}
seledges <- lapply(C, function(x) E(my_graph)[EdgeID %in% x])
subbi <- subgraph.edges(my_graph,unlist(seledges))


于 2020-03-03T09:53:58.190 回答