二分 igraph 中的节点顺序
Node order in bipartite igraph
我有以下邻接矩阵 "dat":
dat <- read.table(text = ' TZ1 TZ2 TZ3 TZ4
RSP1 0.456 0.232 0.234 0.000
RSN1 0.248 0.613 0.754 0.413
RSP2 0.206 0.000 0.493 0.000
RSN2 0.000 0.000 0.000 0.000
RSP3 0.000 0.000 0.218 0.000
RSN3 0.000 0.000 0.000 0.000
RSP4 0.000 0.000 0.000 0.851
RSN4 0.000 0.000 0.000 0.000' ) %>%
as.matrix()
并使用下面的代码生成二分法
g<-graph_from_incidence_matrix(dat, directed = TRUE, mode = "out", multiple = FALSE, weighted = T, add.names = NULL)
V(g)$color <- ifelse(V(g)$type, "lightblue", "salmon")
V(g)$shape <- ifelse(V(g)$type, "square","circle")
V(g)$frame.color <- "gray"
V(g)$size <- 18
E(g)$color <- "blue"
plot.igraph(g, edge.width=E(g)$weight*5, layout=layout.bipartite,edge.arrow.size=0.5,
vertex.label.cex = 0.8, vertex.label.color = "black", vertex.shape=V(g)$shape, vertex.size=degree(g)*5)
但是节点的顺序不是按照下面的顺序
RSP1 RSN1 RSP2 RSN2 RSP3 RSN3 RSP4 RSN4
和
TZ1 TZ2 TZ3 TZ4
我们如何制作具有上述节点排序的图形?
因此 igraph
、layout.bipartite
中的许多布局功能都包括在内,重点是最小化边缘交叉:
The layout is created by first placing the vertices in two rows,
according to their types. Then the positions within the rows are
optimized to minimize edge crossings,
如果你想控制节点位置,那么你将不得不通过使用 x 和 y[=28= 制作矩阵来制作自定义布局] 在列和行中的位置与 V(g) 的顺序相同。 purrr
可以帮到你:
Rs <- V(g)$name[grepl('^R', V(g)$name)] %>%
purrr::imap(function(x, i){
c(i, 2)
}) %>%
do.call(rbind, .)
Ts <- V(g)$name[grepl('^T', V(g)$name)] %>%
purrr::map2(3:6, function(x, i){
c(i, 1)
}) %>%
do.call(rbind, .)
l <- rbind(Rs, Ts)
这会给你一个矩阵:
[,1] [,2]
[1,] 1 2
[2,] 2 2
[3,] 3 2
[4,] 4 2
[5,] 5 2
[6,] 6 2
[7,] 7 2
[8,] 8 2
[9,] 3 1
[10,] 4 1
[11,] 5 1
[12,] 6 1
我有以下邻接矩阵 "dat":
dat <- read.table(text = ' TZ1 TZ2 TZ3 TZ4
RSP1 0.456 0.232 0.234 0.000
RSN1 0.248 0.613 0.754 0.413
RSP2 0.206 0.000 0.493 0.000
RSN2 0.000 0.000 0.000 0.000
RSP3 0.000 0.000 0.218 0.000
RSN3 0.000 0.000 0.000 0.000
RSP4 0.000 0.000 0.000 0.851
RSN4 0.000 0.000 0.000 0.000' ) %>%
as.matrix()
并使用下面的代码生成二分法
g<-graph_from_incidence_matrix(dat, directed = TRUE, mode = "out", multiple = FALSE, weighted = T, add.names = NULL)
V(g)$color <- ifelse(V(g)$type, "lightblue", "salmon")
V(g)$shape <- ifelse(V(g)$type, "square","circle")
V(g)$frame.color <- "gray"
V(g)$size <- 18
E(g)$color <- "blue"
plot.igraph(g, edge.width=E(g)$weight*5, layout=layout.bipartite,edge.arrow.size=0.5,
vertex.label.cex = 0.8, vertex.label.color = "black", vertex.shape=V(g)$shape, vertex.size=degree(g)*5)
但是节点的顺序不是按照下面的顺序
RSP1 RSN1 RSP2 RSN2 RSP3 RSN3 RSP4 RSN4
和
TZ1 TZ2 TZ3 TZ4
我们如何制作具有上述节点排序的图形?
因此 igraph
、layout.bipartite
中的许多布局功能都包括在内,重点是最小化边缘交叉:
The layout is created by first placing the vertices in two rows, according to their types. Then the positions within the rows are optimized to minimize edge crossings,
如果你想控制节点位置,那么你将不得不通过使用 x 和 y[=28= 制作矩阵来制作自定义布局] 在列和行中的位置与 V(g) 的顺序相同。 purrr
可以帮到你:
Rs <- V(g)$name[grepl('^R', V(g)$name)] %>%
purrr::imap(function(x, i){
c(i, 2)
}) %>%
do.call(rbind, .)
Ts <- V(g)$name[grepl('^T', V(g)$name)] %>%
purrr::map2(3:6, function(x, i){
c(i, 1)
}) %>%
do.call(rbind, .)
l <- rbind(Rs, Ts)
这会给你一个矩阵:
[,1] [,2]
[1,] 1 2
[2,] 2 2
[3,] 3 2
[4,] 4 2
[5,] 5 2
[6,] 6 2
[7,] 7 2
[8,] 8 2
[9,] 3 1
[10,] 4 1
[11,] 5 1
[12,] 6 1