简体   繁体   English

二部图中的节点顺序

[英]Node order in bipartite igraph

I have following adjacency matrix "dat": 我有以下邻接矩阵“ 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()

and used the below code to make the bipartite 并使用以下代码制作了两方

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)

The order of the nodes is however not according to below 但是,节点的顺序不符合以下条件

RSP1    RSN1    RSP2    RSN2    RSP3    RSN3    RSP4    RSN4

and

TZ1   TZ2   TZ3   TZ4

How can we make graph with above ordering of the nodes? 我们如何用节点的上述顺序制作图?

So a lot of the layout functions in igraph , layout.bipartite included, focus on minimizing edge crossings: 因此, 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, 然后优化行内的位置,以最大程度地减少边缘交叉,

If you want to control the node locations, then you will have to make a custom layout by making a matrix with x and y positions in the columns and the rows in the same order as the V(g) . 如果要控制节点的位置,则必须制作一个自定义布局,方法是制作一个矩阵,在列和行中的xy位置的顺序与V(g)相同 purrr can help you out here: 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)

which will give you a matrix of: 这将为您提供以下矩阵:

      [,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

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM