ggnetwork
ggnetwork
PeRl
简介
ggnetwork是根据ggplot2的语法,开发的用于网络图可视化的包.虽然igraph是优秀的network处理包,但是在可视化方面依然是弱势,为了加强网络可视化,在这里,我们结合igraph和ggnetwork填补igraph的空白. 在使用时,需要下载一下依赖包:
install.packages(c("ggplot2", "igraph", "ggnetwork", "sna", "intergraph"))
ggnetwork
整个包都围绕ggnetwork
函数展开,为了能和igraph相结合,我们需要intergraph将igraph对象转换为可用的network对象,具体参数如下:
ggnetwork(x, ...)
其中x
是一个network对象或igraph对象. 例如,随意建立一个igraph网络,转换为ggnetwork对象:
- 首先载入需要的包:
library(ggplot2) library(ggnetwork) library(igraph) library(sna) library(intergraph)
- 建立简单igraph网络
edge_df <- data.frame( Source = c("A", "B", "C", "D", "F"), Target = c("B", "D", "A", "E", "B") ) igraph_net <- graph_from_edgelist(as.matrix(edge_df), directed = F) V(igraph_net)$weight <- 1:5#点的权重 ## Warning in vattrs[[name]][index] <- value: 被替换的项目不是替换值长度的倍数 node_count <- length(V(igraph_net)) V(igraph_net)$class <- sample(c("lncRNA","miRNA","gene"), node_count, replace = T)#点的权重
通过edge的信息,我们构建了一个igraph网络
igraph_net ## IGRAPH UN-- 6 5 -- ## + attr: name (v/c), weight (v/n), class (v/c) ## + edges (vertex names): ## [1] A--B B--D A--C D--E B--F
简单利用igraph内置的plot
可视化
plot(igraph_net)
- 利用ggnetwork可视化
由于ggnetwork脱胎于ggplot2,所以+
依然在ggnetwork中起了至关重要的作用.而geom_edges
与geom_nodes
则是整个包可视化network的核心. 利用ggnetwork
得到ggnetwork对象:
ggnetwork_net <- ggnetwork(x = igraph_net) head(ggnetwork_net) ## x y class na.x vertex.names weight xend ## 1 0.76175696 0.4648356 lncRNA FALSE A 1 0.76175696 ## 2 0.33427150 0.3553622 gene FALSE B 2 0.33427150 ## 3 0.03084336 0.6462895 gene FALSE D 3 0.03084336 ## 4 1.00000000 0.7479681 gene FALSE C 4 1.00000000 ## 5 0.00000000 1.0000000 lncRNA FALSE E 5 0.00000000 ## 6 0.22656508 0.0000000 lncRNA FALSE F 1 0.22656508 ## yend na.y ## 1 0.4648356 NA ## 2 0.3553622 NA ## 3 0.6462895 NA ## 4 0.7479681 NA ## 5 1.0000000 NA ## 6 0.0000000 NA
从ggnetwork_net
的返回值,我们可以清楚的了解到ggnetwork的工作原理,其实就是在二维平面上画点.
ggplot(data = ggnetwork_net, aes(x = x, y = y, xend = xend, yend = yend)) + geom_edges() + geom_point() + theme_blank()
通过以上步骤,我们得到了一个最简单的可视化结果,接下来我们需要对图像进行一定程度的修饰.
针对边的修饰
经常我们需要对边的颜色进行改变,和ggplot2一样,我们只需要改变colour
属性就行了.
ggplot(data = ggnetwork_net, aes(x = x, y = y, xend = xend, yend = yend)) + geom_edges(colour = "#FF00FF") + geom_point() + theme_blank()
同样,我们也可以修饰边的粗细和线型:
ggplot(data = ggnetwork_net, aes(x = x, y = y, xend = xend, yend = yend)) + geom_edges(colour = "#FF00FF", size = 2, linetype = 2) + geom_point() + theme_blank()
针对点的修饰
和边类似,我们可以修饰点的大小和颜色.
ggplot(data = ggnetwork_net, aes(x = x, y = y, xend = xend, yend = yend)) + geom_edges(colour = "grey50") + geom_point(aes(colour = class, size = weight)) + theme_blank()
而和边不同,通常情况下,我们都需要在图中展示点的标签,此时,我们需要geom_nodetext
来帮助我们做到这一点
ggplot(data = ggnetwork_net, aes(x = x, y = y, xend = xend, yend = yend)) + geom_edges(colour = "grey50") + geom_point(aes(colour = class, size = weight)) + geom_nodetext(aes(label = vertex.names), fontface = "bold")+ theme_blank()
虽然把标签放在点中央还不错,但是通常情况下,我们用在生物学网络中时标签名会很长,这样就会显得很不合适,因此我们需要调整标签的位置. 而且有时候我们不需要把所有的标签都列出来.只需要把重要的node信息标注就行了.
ggplot(data = ggnetwork_net, aes(x = x, y = y, xend = xend, yend = yend)) + geom_edges(colour = "grey50") + geom_point(aes(colour = class, size = weight)) + geom_nodetext(data = ggnetwork_net[1:3,],aes(label = vertex.names), fontface = "bold", nudge_x = 0.04, nudge_y = -0.04)+ theme_blank()
layout
sna
提供了许多不同的layout:
circle | eigen | fruchtermanreingold | hall | kamadakawai | mds | princoord | random | spring | target |
更细致的描述可以上包中查询.
简单的示例:
new_ggnetwork_net <- ggnetwork(igraph_net, layout = "circle") ggplot(data = new_ggnetwork_net, aes(x = x, y = y, xend = xend, yend = yend)) + geom_edges(colour = "grey50") + geom_point(aes(colour = class, size = weight)) + geom_nodetext(aes(label = vertex.names), fontface = "bold", nudge_x = 0.04, nudge_y = -0.04)+ theme_blank()