Pan*_*kaj 9 r social-networking igraph bipartite
如何在R中绘制二分类型的网络?与此类似:
我有类似的数据,但基因和疾病和SARS的权重.这个网络就是一个例子.我有不同的属性.我在这里关注了一个链接.但是由于我对这个主题的了解不多,我无法从中得到很多.在此先感谢您的帮助.
对于您提供的示例,我建议使用x
和y
属性来可视化二分图.例如:
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)
Run Code Online (Sandbox Code Playgroud)
编辑:为了清晰起见,添加了代码以给出顶点和边缘不同的颜色.
从?bipartite_graph
帮助:
二元图在igraph中具有类型顶点属性,对于第一类顶点,它是boolean和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]
)
Run Code Online (Sandbox Code Playgroud)
检查一下?bipartite
.
使用评论中OP提供的示例.由于图是多方的并且给定了提供的数据格式,我首先创建一个二分图,然后添加其他边.请注意,虽然结果图形返回TRUE,is_bipartite()
但类型参数被指定为数字而不是逻辑,并且可能无法与其他二分函数一起正常工作.
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.
)
Run Code Online (Sandbox Code Playgroud)
该功能layout.concentric()
在(我的)包rTRM中,可从Bioconductor获得.它实际上是一个简单的实现,我写的就是你想做的.我不完全确定最新igraph
版本是否具有相同的功能(可能).