![](/img/trans.png)
[英]How can I assign a color range to edges in igraph plot in R based on edge attributes?
[英]How to assign edge weights to certain edges in R igraph
我想为最短路径中使用的某些边分配一个小的、非负的边权重。 这是一个示例图:
library(igraph)
data <- read.table(text="
1 2
1 4
1 5
2 3
2 4
3 4
5 7
5 8
3 6", header=FALSE)
gmatrix <- data.matrix(data, rownames.force = NA) #convert into a matrix to use in igraph
g <- graph_from_edgelist(gmatrix, directed = FALSE)
如果我找到节点 1 和节点 3 之间的最短路径,则使用的边是 1-2 和 1-3。
get.shortest.paths(g, 1,3)
$vpath
$vpath[[1]]
+ 3/9 vertices, from 634c426:
[1] 1 2 3
我想要做的是为这些边分配一个小的 epsilon 值。 然后,我想调用get.shortest.paths(g, 1,3)
来测试该函数是否会识别相同的路径。
好的,这个我可以帮忙:
使用E()
查询带有从get.shortest.paths
的 id 的边,并将值分配给新的边属性名称(例如,“重量”或其他名称):
p <- get.shortest.paths(g, 1, 3)$vpath[[1]]
E(g, path = p)$weight <- 0.1
> E(g)
+ 9/9 edges from 605e8c7:
[1] 1--2 1--4 1--5 2--3 2--4 3--4 5--7 5--8 3--6
> E(g)$weight
[1] 0.1 NA NA 0.1 NA NA NA NA NA
从 1 到 2 和 2 到 3 的路径现在具有加权边。
现在,将零分配给其他边,“get.shortest.paths”标识另一条路径:
> E(g)$weight <- ifelse(is.na(E(g)$weight), 0, E(g)$weight)
> E(g)$weight
[1] 0.1 0.0 0.0 0.1 0.0 0.0 0.0 0.0 0.0
> get.shortest.paths(g, 1, 3, weights = E(g)$weight)
$vpath
$vpath[[1]]
+ 3/8 vertices, from 605e8c7:
[1] 1 4 3
$epath
NULL
$predecessors
NULL
$inbound_edges
NULL
更简洁一点:
g <- set_edge_attr(g, "weight", value = ifelse(E(g) %in% E(g, path = p), 0.1, 0))
E(g)$weight
[1] 0.1 0.0 0.0 0.1 0.0 0.0 0.0 0.0 0.0
在接下来的代码中,我将使用函数all_shortest_paths
而不是get.shortest.paths
来获取所有最短路径。 这是因为我稍后会为其中之一的边缘分配权重。
询问的路径在顶点3
和5
之间,因为有两条路径。
首先,看看get.shortest.paths
返回什么。
get.shortest.paths(g, 3, 5)
#$vpath
#$vpath[[1]]
#+ 4/8 vertices, from 98e8e26:
#[1] 3 2 1 5
#
#
#$epath
#NULL
#
#$predecessors
#NULL
#
#$inbound_edges
#NULL
该函数仅返回一个路径: 3 2 1 5
。 但是顶点3
和5
之间有两条最短路径。
p <- all_shortest_paths(g, 3, 5)
p
#$res
#$res[[1]]
#+ 4/8 vertices, from fc330b4:
#[1] 3 2 1 5
#
#$res[[2]]
#+ 4/8 vertices, from fc330b4:
#[1] 3 4 1 5
#
#
#$nrgeo
#[1] 2 1 1 1 2 1 0 0
现在将epsilon权重分配给其中一条路径的边缘,即第一个。
E(g, path = p$res[[1]])$weight <- .Machine$double.eps^0.5
E(g)$weight
#[1] 1.490116e-08 NA 1.490116e-08 1.490116e-08 NA
#[6] NA NA NA NA
再次获取所有最短路径。
all_shortest_paths(g, 3, 5)
#$res
#$res[[1]]
#+ 4/8 vertices, from fc330b4:
#[1] 3 4 1 5
#
#
#$nrgeo
#[1] 1 0 1 1 1 1 0 0
现在只有一条路径,路径3 4 1 5
,以前的路径p$res[[1]]
现在更重,不再是最短路径。
而get.shortest.paths
也返回一条路径。
get.shortest.paths(g, 3, 5)
#$vpath
#$vpath[[1]]
#+ 4/8 vertices, from 98e8e26:
#[1] 3 4 1 5
#
#
#$epath
#NULL
#
#$predecessors
#NULL
#
#$inbound_edges
#NULL
编辑。
继评论用户paqmo ,下面的代码使用的唯一功能get.shortest.paths
获得的最短路径。 给它的边分配权重后,同一个函数返回的路径不再是p2
,它和上面的代码一样。
只有在没有权重属性的情况下重新初始化图形时,此代码才有意义。
p2 <- get.shortest.paths(g, 3, 5)
p2
#$vpath
#$vpath[[1]]
#+ 4/8 vertices, from 61bfc89:
#[1] 3 2 1 5
#
#
#$epath
#NULL
#
#$predecessors
#NULL
#
#$inbound_edges
#NULL
E(g, path = p2$vpath[[1]])$weight <- .Machine$double.eps^0.5
E(g)$weight
get.shortest.paths(g, 3, 5)
#$vpath
#$vpath[[1]]
#+ 4/8 vertices, from 61bfc89:
#[1] 3 4 1 5
#
#
#$epath
#NULL
#
#$predecessors
#NULL
#
#$inbound_edges
#NULL
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.