[英]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.