简体   繁体   English

如何在R中绘制二部图

[英]How to plot a bipartite graph in R

How do I plot a network of type bipartite in R? 如何在R中绘制类型为bipartite的网络? Similar to this: 与此类似:

二分

I have similar data but with weights for both genes and diseases and SARS. 我有相似的数据,但是具有基因,疾病和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. 在此先感谢您的帮助。

For the example you provided, I would recommend using the x and y attributes for visualizing a bipartite graph. 对于您提供的示例,我建议使用xy属性可视化二部图。 Eg: 例如:

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)

二分图。

EDIT: added code to give the vertices and edges different colors for clarity. 编辑:添加了代码,以使顶点和边缘具有不同的颜色,以保持清晰度。

From the ?bipartite_graph help: ?bipartite_graph帮助中:

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. 二部图在igraph中具有类型顶点属性,对于第一种顶点,这是布尔值和FALSE,对于第二种顶点是TRUE。

So you could do something like this ( igraph 1.0.1 ): 因此,您可以执行以下操作( 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 . 也检查?bipartite


Using the example provided by the OP in the comments. 使用OP在注释中提供的示例。 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. 请注意,尽管生成的图形为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.
)

同心

The function layout.concentric() is in (my) package rTRM , available from Bioconductor . layout.concentric()函数位于(my)软件包rTRM中 ,可从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). 我不确定最新的igraph版本是否具有相同的功能(可能是)。

Or you can use the multigraph package. 或者您可以使用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)

that can produce the binomial projection of the two-mode data set 可以产生双模数据集的二项式投影

南方女子两模网

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

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