如何在R中绘制二部图 [英] How to plot a bipartite graph in R

查看:463
本文介绍了如何在R中绘制二部图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在R中绘制类型为这里。但是由于我对该主题的知识不足,因此无法从中受益匪浅。预先感谢您的任何帮助。

解决方案

?bipartite_graph 帮助中:


二部图在igraph中具有类型顶点属性,对于第一种类型的顶点为boolean和FALSE,对于以下类型的顶点为TRUE第二种。


因此,您可以执行以下操作(]:

 库(igraph)

set.seed(123)

#生成随机二分图。
g<-sample_bipartite(10,5,p = .4)
#检查type属性:
V(g)$ type

#定义颜色并形状映射。
col<-c( steelblue, orange)
shape<-c( circle, square)

图(g,
vertex.color = col [as.numeric(V(g)$ type)+1],
vertex.shape = shape [as.numeric(V(g)$ type)+1]



还要检查?bipartite






使用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,权重=符((10 ))
d
> d
V1 V2重量
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,有向= FALSE)
V(g)$ label<-V(g)$ name#设置标签。

#创建一个将中央节点FOO连接到每个V2的图形。
e<-expand.grid(V2 =唯一(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)

#加入两个图形。
g <-g + g2

#集合类型。
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 1 2 2 2 2 2 2 2 2 2 3

col<-c( steelblue,橙色, green)
shape<-c( circle, square, circle)

library(rTRM)#包含layout.concentric()的生物导体包装b $ b#同心列表中的第一个元素是中心节点。
l<-layout.concentric(g,concentric = list( FOO,1:10,LETTERS [1:10])))
图(g,
layout = l,
vertex.color = col [V(g)$ type],
vertex.shape = shape [V(g)$ type],
edge.width = E(g)$ weights * 5#可选,绘制边缘宽度与权重成比例。



函数 layout.concentric() 在(a)包 rTRM 中,可从生物导体。这确实是我写的一个简单实现,可以完全按照您的要求进行。我不完全确定最新的 igraph 版本是否具有相同的功能。


How do I plot a network of type bipartite in R? Similar to this:

I have similar data but with weights for both genes and diseases and SARS. This network is an example. I have different kind of attributes. I followed a link here. But due to my little knowledge in this topic, I could not get much out of it. Thanks in advance for any help.

解决方案

From the ?bipartite_graph help:

Bipartite graphs have a type vertex attribute in igraph, this is boolean and FALSE for the vertices of the first kind and TRUE for vertices of the second kind.

So you could do something like this (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]
)

Check also ?bipartite.


Using the example provided by the OP in the comments. Since the graph is multipartite and given the provided data format, I would first create a bipartite graph, then add the additional edges. Note that although the resulting graph returns TRUE for is_bipartite() the type argument is specified as numeric instead of logical and may not work properly with other bipartite functions.

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.
)

The function layout.concentric() is in (my) package rTRM, available from Bioconductor. It is really a simple implementation I wrote to do exactly what you want. I am not completely sure whether the latest igraph version has the same functionality though (it may be).

这篇关于如何在R中绘制二部图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆