all_simple_paths 的绘图结果 (igraph, R)

Plot result of all_simple_paths (igraph, R)

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

"plot" 给出错误: Error in xy.coords(x, y, xlabel, ylabel, log) : 'x' is a list, but does not have components 'x' and 'y' 我用 "ggraph" 绘制的完整图表,但对于 "outlet graph",此命令也给出了错误: 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)`

非常感谢!

您可以通过在 graph_outlet 中制作每个路径的子图来绘制相应的路径,例如:

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

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

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)))

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

这里是我现在在我的完整数据集上应用的代码。 结果 "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))