【问题标题】:How to plot a bipartite graph in R如何在 R 中绘制二部图
【发布时间】:2015-09-30 16:42:24
【问题描述】:

如何在 R 中绘制bipartite 类型的网络?类似这样:

我有类似的数据,但有基因和疾病以及 SARS 的权重。这个网络就是一个例子。我有不同的属性。我在这里关注了link。但由于我对这个主题知之甚少,我无法从中得到太多。提前感谢您的帮助。

【问题讨论】:

    标签: r social-networking igraph bipartite


    【解决方案1】:

    来自?bipartite_graph 帮助:

    二分图在 igraph 中有一个 type vertex 属性,对于第一种顶点是布尔值和 FALSE,对于第二种顶点是 TRUE。

    所以你可以这样做 (igraph 1.0.1):

    library(igraph)
    
    set.seed(123)
    
    # generate random bipartite graph.
    g <- sample_bipartite(10, 5, p=.4)
    # check the type attribute:
    V(g)$type
    
    # define color and shape mappings.
    col <- c("steelblue", "orange")
    shape <- c("circle", "square")
    
    plot(g,
      vertex.color = col[as.numeric(V(g)$type)+1],
      vertex.shape = shape[as.numeric(V(g)$type)+1]
    )
    

    还要检查?bipartite


    使用 cmets 中 OP 提供的示例。由于该图是多方图并且给定了提供的数据格式,我将首先创建一个二分图,然后添加其他边。请注意,虽然结果图为 is_bipartite() 返回 TRUE,但类型参数被指定为数字而不是逻辑,并且可能无法与其他二分函数一起正常工作。

    set.seed(123)
    V1 <- sample(LETTERS[1:10], size = 10, replace = TRUE)
    V2 <- sample(1:10, size = 10, replace = TRUE)
    
    d <- data.frame(V1 = V1, V2 = V2, weights = runif(10))
    d
    > d
       V1 V2   weights
    1   C 10 0.8895393
    2   H  5 0.6928034
    3   E  7 0.6405068
    4   I  6 0.9942698
    5   J  2 0.6557058
    6   A  9 0.7085305
    7   F  3 0.5440660
    8   I  1 0.5941420
    9   F  4 0.2891597
    10  E 10 0.1471136
    
    g <- graph_from_data_frame(d, directed = FALSE)
    V(g)$label <- V(g)$name # set labels.
    
    # create a graph connecting central node FOO to each V2.
    e <- expand.grid(V2 = unique(d$V2), V2 = "FOO")
      > e
      V2  V2
    1 10 FOO
    2  5 FOO
    3  7 FOO
    4  6 FOO
    5  2 FOO
    6  9 FOO
    7  3 FOO
    8  1 FOO
    9  4 FOO
    
    g2 <- graph.data.frame(e, directed = FALSE)
    
    # join the two graphs.
    g <- g + g2
    
    # set type.
    V(g)$type <- 1
    V(g)[name %in% 1:10]$type <- 2
    V(g)[name %in% "FOO"]$type <- 3
    
    V(g)$type
    > V(g)$type
     [1] 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 3
    
    col <- c("steelblue", "orange", "green")
    shape <- c("circle", "square", "circle")
    
    library(rTRM) # Bioconductor package containing layout.concentric()
    # the fist element in the list for concentric is the central node.
    l <- layout.concentric(g, concentric = list("FOO", 1:10, LETTERS[1:10]))
    plot(g,
         layout = l,
         vertex.color = col[V(g)$type],
         vertex.shape = shape[V(g)$type],
         edge.width = E(g)$weights * 5 # optional, plot edges width proportional to weights.
    )
    

    函数layout.concentric() 在(我的)包rTRM 中,可从Bioconductor 获得。这真的是一个简单的实现,我写的正是你想要的。我不完全确定最新的igraph 版本是否具有相同的功能(可能是)。

    【讨论】:

    • 亲爱的 ddiez, 感谢您的回复。我有所有的节点和边。例如,V1 &lt;- c(A, A, A, B, B, B, B, C)V2 &lt;- c(1, 1, 2, 3, 1, 4, 1, 2)。边是一一对应的。在这种情况下,如何做到这一点?
    • @Pankaj 我使用您的示例添加了一些代码。不确定我是否理解了你的意思——请告诉我。看来您可以通过添加 type 属性,而不是直接使用 bipartite_graph() 函数,从常规图hack二分图。
    • 亲爱的 ddiez,它部分工作。这是示例数据的link。 V1 是疾病,V2 是基因,V3 是单节点连接所有的,就像 SARS 一样。边缘的重量也在那里。我没有得到确切的布局,因为实际数据稍微大一点,而且视图不好。请查看该文件以帮助实现这个方向,
    • @Pankaj,示例文件看起来像一个多部分图。也就是说,您有三组顶点(在这种情况下,集合 V1 和 V2 中的所有节点都连接到 V3 中的唯一节点)。那么边权重是什么意思呢?每条线代表三条边,一条从 V1 到 V2,一条从 V2 到 V3,一条从 V1 到 V3,如果我理解正确的话。
    • 无论如何,对于多部分图,添加 type 属性似乎有效。例如,V(g)$type &lt;- c(1,1,1,2,2,3,3,3) 用于集合 V1、V2 和 V3 中的节点。
    【解决方案2】:

    对于您提供的示例,我建议使用xy 属性来可视化二分图。例如:

    V(g)$x <- c(1, 1, 1, 2, 2, 2, 2)
    V(g)$y <- c(3, 2, 1, 3.5, 2.5, 1.5, 0.5)
    V(g)$shape <- shape[as.numeric(V(g)$type) + 1]
    V(g)$color <- c('red', 'blue', 'green', 'steelblue', 'steelblue', 'steelblue', 'steelblue')
    E(g)$color <- 'gray'
    E(g)$color[E(g)['A' %--% V(g)]] <- 'red'
    E(g)$color[E(g)['B' %--% V(g)]] <- 'blue'
    E(g)$color[E(g)['C' %--% V(g)]] <- 'green'
    plot(g)
    

    编辑:添加代码以赋予顶点和边缘不同的颜色以清晰明了。

    【讨论】:

    • 亲爱的克里斯,感谢您的回复。我在上面与 ddiez 的聊天中提供了示例数据的链接。上图不包含该中心节点。
    • 使用layout.bipartite() 会更简单,尽管这样布局是水平的而不是垂直的。但这可以使用l &lt;- layout.bipartite(g) 然后调用plot(g, layout = l[, c(2,1)]) 轻松更改。
    【解决方案3】:

    或者您可以使用multigraph 包。

    swomen <- read.dl(file = "http://moreno.ss.uci.edu/davis.dat")
    
    bmgraph(swomen, layout = "force", seed = 1, cex = 3, tcex = .8, pch = c(19, 15), lwd = 2,  
    +  vcol = 2:3, ecol = 8, rot = 65)
    

    可以产生二模数据集的二项式投影

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-14
      • 2020-03-12
      • 2019-09-04
      相关资源
      最近更新 更多