ggClusterNet:手写算法在R语言中实现Gephi布局之一

写在前面
像不像Gephi出图,Gephi布局还是有很多人喜欢,R语言确实不好实现,这个算法需要大量的迭代,让模块化点之间连线最短,不断修正,很耗费时间,我将聚类算法和修改后的距离算法合并起来并结合节点的性质对这个布局做了一个简化版,速度非常快。很适合R。
导入ggClusterNet和其他R包
ggClusterNet目前只能在github上有,从这里安装得到。
#
library(ggClusterNet)
library(phyloseq)
library(sna)
library(tidyverse)
library(igraph)
corMicro 函数用于计算相关关系
data(ps)
result = corMicro (ps = ps,N = 0.0008,r.threshold=0.7,p.threshold=0.05,method = "pearson")
cor = result[[1]]
dim(cor)## [1] 191 191
ps_net = result[[3]]
otu_table = as.data.frame(t(vegan_otu(ps_net)))
tax_table = as.data.frame(vegan_tax(ps_net))
核心布局函数:model_Gephi.2
用于形成类似Gephi中的一种布局的样式,我们这里其实有好几个变形布局,后面给大家在做一个演示:
参数解释:
method :使用的模块化聚类算法,对于生成的可视化布局具有重要影响
seed:随机种子设置,会影响最终布局
result2 <- model_Gephi.2(cor = cor,
method = "cluster_fast_greedy",
seed = 12
)
node = result2[[1]]
nodeadd 节点添加注释;edgeBuild计算网络节点之间的连线
#---node节点注释#-----------
nodes = nodeadd(plotcord =node,otu_table = otu_table,tax_table = tax_table)
head(nodes)## X1 X2 elements Kingdom Phylum
## ASV_1 2.6268690 6.488417 ASV_1 Bacteria Actinobacteria
## ASV_10 2.8531695 -0.927051 ASV_10 Bacteria Proteobacteria
## ASV_100 3.3684782 3.695045 ASV_100 Bacteria Proteobacteria
## ASV_101 -1.3680806 -3.758770 ASV_101 Bacteria Proteobacteria
## ASV_102 0.9224635 3.892179 ASV_102 Bacteria Proteobacteria
## ASV_103 -2.4270510 1.763356 ASV_103 Bacteria Proteobacteria
## Class Order Family Genus
## ASV_1 Actinobacteria Actinomycetales Thermomonosporaceae Unassigned
## ASV_10 Alphaproteobacteria Rhizobiales Rhizobiaceae Rhizobium
## ASV_100 Betaproteobacteria Burkholderiales Comamonadaceae Hydrogenophaga
## ASV_101 Unassigned Unassigned Unassigned Unassigned
## ASV_102 Gammaproteobacteria Unassigned Unassigned Unassigned
## ASV_103 Alphaproteobacteria Rhizobiales Phyllobacteriaceae Phyllobacterium
## Species mean
## ASV_1 Unassigned 0.041022288
## ASV_10 Unassigned 0.011729515
## ASV_100 Hydrogenophaga_intermedia 0.001693538
## ASV_101 Unassigned 0.001734661
## ASV_102 Unassigned 0.001779072
## ASV_103 Phyllobacterium_bourgognense 0.001490287#-----计算边#--------
edge = edgeBuild(cor = cor,plotcord = node)
head(edge)## X1 Y1 OTU_1 X2 Y2 OTU_2 weight wei_label
## ASV_6 8.660254e-01 -0.5000000 ASV_6 0 0 ASV_2 0.8975938 +
## ASV_39 8.660254e-01 0.5000000 ASV_39 0 0 ASV_2 0.9190153 +
## ASV_41 1.224606e-16 -1.0000000 ASV_41 0 0 ASV_2 0.7800088 +
## ASV_60 -8.660254e-01 -0.5000000 ASV_60 0 0 ASV_2 0.8991011 +
## ASV_56 -8.660254e-01 0.5000000 ASV_56 0 0 ASV_2 0.8528787 +
## ASV_17 1.870032e+00 -0.7092098 ASV_17 0 0 ASV_2 0.7460074 +
## midX midY
## ASV_6 4.330127e-01 -0.2500000
## ASV_39 4.330127e-01 0.2500000
## ASV_41 6.123032e-17 -0.5000000
## ASV_60 -4.330127e-01 -0.2500000
## ASV_56 -4.330127e-01 0.2500000
## ASV_17 9.350162e-01 -0.3546049colnames(edge)[8] = "cor"
head(nodes)## X1 X2 elements Kingdom Phylum
## ASV_1 2.6268690 6.488417 ASV_1 Bacteria Actinobacteria
## ASV_10 2.8531695 -0.927051 ASV_10 Bacteria Proteobacteria
## ASV_100 3.3684782 3.695045 ASV_100 Bacteria Proteobacteria
## ASV_101 -1.3680806 -3.758770 ASV_101 Bacteria Proteobacteria
## ASV_102 0.9224635 3.892179 ASV_102 Bacteria Proteobacteria
## ASV_103 -2.4270510 1.763356 ASV_103 Bacteria Proteobacteria
## Class Order Family Genus
## ASV_1 Actinobacteria Actinomycetales Thermomonosporaceae Unassigned
## ASV_10 Alphaproteobacteria Rhizobiales Rhizobiaceae Rhizobium
## ASV_100 Betaproteobacteria Burkholderiales Comamonadaceae Hydrogenophaga
## ASV_101 Unassigned Unassigned Unassigned Unassigned
## ASV_102 Gammaproteobacteria Unassigned Unassigned Unassigned
## ASV_103 Alphaproteobacteria Rhizobiales Phyllobacteriaceae Phyllobacterium
## Species mean
## ASV_1 Unassigned 0.041022288
## ASV_10 Unassigned 0.011729515
## ASV_100 Hydrogenophaga_intermedia 0.001693538
## ASV_101 Unassigned 0.001734661
## ASV_102 Unassigned 0.001779072
## ASV_103 Phyllobacterium_bourgognense 0.001490287library(ggrepel)
p1 <- ggplot() + geom_segment(aes(x = X1, y = Y1, xend = X2, yend = Y2,color = cor),
data = edge, size = 0.5,alpha = 0.3) +
geom_point(aes(X1, X2,fill = Phylum,size = mean),pch = 21, data = nodes) +
# geom_text_repel(aes(X1, X2,label = elements),pch = 21, data = nodes) +
# geom_text(aes(X1, X2,label = elements),pch = 21, data = nodes) +
scale_colour_manual(values = c("#377EB8","#E41A1C")) +
scale_size(range = c(4, 14)) +
scale_x_continuous(breaks = NULL) + scale_y_continuous(breaks = NULL) +
theme(panel.background = element_blank()) +
theme(axis.title.x = element_blank(), axis.title.y = element_blank()) +
theme(legend.background = element_rect(colour = NA)) +
theme(panel.background = element_rect(fill = "white", colour = NA)) +
theme(panel.grid.minor = element_blank(), panel.grid.major = element_blank())p2 <- ggplot() + geom_curve(aes(x = X1, y = Y1, xend = X2, yend = Y2,color = cor,curvature = 0.2),
data = edge, size = 0.5,alpha = 0.3) +
geom_point(aes(X1, X2,fill = Phylum,size = mean),pch = 21, data = nodes) +
# geom_text(aes(X1, X2,label = elements),pch = 21, data = nodes) +
scale_colour_manual(values = c("#377EB8","#E41A1C")) +
scale_size(range = c(4, 14)) +
scale_x_continuous(breaks = NULL) + scale_y_continuous(breaks = NULL) +
theme(panel.background = element_blank()) +
theme(axis.title.x = element_blank(), axis.title.y = element_blank()) +
theme(legend.background = element_rect(colour = NA)) +
theme(panel.background = element_rect(fill = "white", colour = NA)) +
theme(panel.grid.minor = element_blank(), panel.grid.major = element_blank())library(patchwork)
( p1 | p2)

对于不同的模块添加聚类范围,其实很简单 比基础包作图都简单
library(ggalt)
library(ggnewscale)
netClu = result2[[3]]
head(netClu)## ID group degree
## 1 ASV_10 14 29
## 2 ASV_2 5 28
## 3 ASV_6 5 27
## 4 ASV_39 5 24
## 5 ASV_41 5 24
## 6 ASV_66 16 24row.names(netClu) = netClu$ID
nodeG = merge(nodes,netClu,by = "row.names",all =FALSE)
dim(nodeG)## [1] 191 15
head(nodeG)
## Row.names X1 X2 elements Kingdom Phylum
## 1 ASV_1 2.6268690 6.488417 ASV_1 Bacteria Actinobacteria
## 2 ASV_10 2.8531695 -0.927051 ASV_10 Bacteria Proteobacteria
## 3 ASV_100 3.3684782 3.695045 ASV_100 Bacteria Proteobacteria
## 4 ASV_101 -1.3680806 -3.758770 ASV_101 Bacteria Proteobacteria
## 5 ASV_102 0.9224635 3.892179 ASV_102 Bacteria Proteobacteria
## 6 ASV_103 -2.4270510 1.763356 ASV_103 Bacteria Proteobacteria
## Class Order Family Genus
## 1 Actinobacteria Actinomycetales Thermomonosporaceae Unassigned
## 2 Alphaproteobacteria Rhizobiales Rhizobiaceae Rhizobium
## 3 Betaproteobacteria Burkholderiales Comamonadaceae Hydrogenophaga
## 4 Unassigned Unassigned Unassigned Unassigned
## 5 Gammaproteobacteria Unassigned Unassigned Unassigned
## 6 Alphaproteobacteria Rhizobiales Phyllobacteriaceae Phyllobacterium
## Species mean ID group degree
## 1 Unassigned 0.041022288 ASV_1 6 1
## 2 Unassigned 0.011729515 ASV_10 14 29
## 3 Hydrogenophaga_intermedia 0.001693538 ASV_100 2 4
## 4 Unassigned 0.001734661 ASV_101 16 10
## 5 Unassigned 0.001779072 ASV_102 2 2
## 6 Phyllobacterium_bourgognense 0.001490287 ASV_103 15 6ggplot() + geom_curve(aes(x = X1, y = Y1, xend = X2, yend = Y2,color = cor,curvature = 0.2),
data = edge, size = 0.5,alpha = 0.3) +
scale_colour_manual(values = c("#377EB8","#E41A1C")) +
new_scale_fill()+
geom_point(aes(X1, X2,fill = group,size = mean),pch = 21, data = nodeG) +
geom_encircle(aes(X1, X2,group = group,fill = group), linetype = 2,alpha = 0.1, data = nodeG) +
geom_encircle(aes(X1, X2,group = group), linetype = 2,alpha = 1, data = nodeG,color = "black") +
scale_fill_gradientn(colours =colorRampPalette(RColorBrewer::brewer.pal(11,"Spectral"))(60)) +
scale_size(range = c(4, 14)) +
scale_x_continuous(breaks = NULL) + scale_y_continuous(breaks = NULL) +
theme(panel.background = element_blank()) +
theme(axis.title.x = element_blank(), axis.title.y = element_blank()) +
theme(legend.background = element_rect(colour = NA)) +
theme(panel.background = element_rect(fill = "white", colour = NA)) +
theme(panel.grid.minor = element_blank(), panel.grid.major = element_blank())

Model_Gephi 模块化布局 不同于Gephi.2
微生物网络模块化是一个必须要好好展示的内容,这里我们尝试通过算法将模块分别展示到布局的不同位置,会按照圈图的不同层次进行可是化同一个模块的内容,更加方便进行模块化分析和布局。
result2 <- Model_Gephi(cor = cor,
method = "cluster_fast_greedy"
)
node = result2[[1]]
#---node节点注释#-----------
nodes = nodeadd(plotcord =node,otu_table = otu_table,tax_table = tax_table)
head(nodes)## X1 X2 elements Kingdom Phylum
## ASV_1 -2.853170 0.927051 ASV_1 Bacteria Actinobacteria
## ASV_10 -1.806208 4.662361 ASV_10 Bacteria Proteobacteria
## ASV_100 -3.451912 4.907576 ASV_100 Bacteria Proteobacteria
## ASV_101 -2.632161 -4.251086 ASV_101 Bacteria Proteobacteria
## ASV_102 3.435023 6.099231 ASV_102 Bacteria Proteobacteria
## ASV_103 -5.785521 -1.589889 ASV_103 Bacteria Proteobacteria
## Class Order Family Genus
## ASV_1 Actinobacteria Actinomycetales Thermomonosporaceae Unassigned
## ASV_10 Alphaproteobacteria Rhizobiales Rhizobiaceae Rhizobium
## ASV_100 Betaproteobacteria Burkholderiales Comamonadaceae Hydrogenophaga
## ASV_101 Unassigned Unassigned Unassigned Unassigned
## ASV_102 Gammaproteobacteria Unassigned Unassigned Unassigned
## ASV_103 Alphaproteobacteria Rhizobiales Phyllobacteriaceae Phyllobacterium
## Species mean
## ASV_1 Unassigned 0.041022288
## ASV_10 Unassigned 0.011729515
## ASV_100 Hydrogenophaga_intermedia 0.001693538
## ASV_101 Unassigned 0.001734661
## ASV_102 Unassigned 0.001779072
## ASV_103 Phyllobacterium_bourgognense 0.001490287#-----计算边#--------
edge = edgeBuild(cor = cor,plotcord = node)
head(edge)## X1 Y1 OTU_1 X2 Y2 OTU_2 weight
## ASV_18 1.326245e+00 -1.497021 ASV_18 0.000000 0.000000 ASV_28 0.7802901
## ASV_34 2.427051e+00 1.763356 ASV_34 0.000000 0.000000 ASV_28 0.7676010
## ASV_34.1 2.427051e+00 1.763356 ASV_34 1.326245 -1.497021 ASV_18 0.8135225
## ASV_1 -2.853170e+00 0.927051 ASV_1 2.427051 1.763356 ASV_34 0.7922074
## ASV_6 -7.347638e-16 3.000000 ASV_6 -2.427051 1.763356 ASV_2 0.8975938
## ASV_17 9.224635e-01 3.892179 ASV_17 -2.427051 1.763356 ASV_2 0.7460074
## wei_label midX midY
## ASV_18 + 0.6631227 -0.7485107
## ASV_34 + 1.2135255 0.8816779
## ASV_34.1 + 1.8766481 0.1331671
## ASV_1 + -0.2130593 1.3452034
## ASV_6 + -1.2135255 2.3816779
## ASV_17 + -0.7522938 2.8277676colnames(edge)[8] = "cor"
head(nodes)## X1 X2 elements Kingdom Phylum
## ASV_1 -2.853170 0.927051 ASV_1 Bacteria Actinobacteria
## ASV_10 -1.806208 4.662361 ASV_10 Bacteria Proteobacteria
## ASV_100 -3.451912 4.907576 ASV_100 Bacteria Proteobacteria
## ASV_101 -2.632161 -4.251086 ASV_101 Bacteria Proteobacteria
## ASV_102 3.435023 6.099231 ASV_102 Bacteria Proteobacteria
## ASV_103 -5.785521 -1.589889 ASV_103 Bacteria Proteobacteria
## Class Order Family Genus
## ASV_1 Actinobacteria Actinomycetales Thermomonosporaceae Unassigned
## ASV_10 Alphaproteobacteria Rhizobiales Rhizobiaceae Rhizobium
## ASV_100 Betaproteobacteria Burkholderiales Comamonadaceae Hydrogenophaga
## ASV_101 Unassigned Unassigned Unassigned Unassigned
## ASV_102 Gammaproteobacteria Unassigned Unassigned Unassigned
## ASV_103 Alphaproteobacteria Rhizobiales Phyllobacteriaceae Phyllobacterium
## Species mean
## ASV_1 Unassigned 0.041022288
## ASV_10 Unassigned 0.011729515
## ASV_100 Hydrogenophaga_intermedia 0.001693538
## ASV_101 Unassigned 0.001734661
## ASV_102 Unassigned 0.001779072
## ASV_103 Phyllobacterium_bourgognense 0.001490287library(ggrepel)
p3 <- ggplot() + geom_segment(aes(x = X1, y = Y1, xend = X2, yend = Y2,color = cor),
data = edge, size = 0.5,alpha = 0.3) +
geom_point(aes(X1, X2,fill = Phylum,size = mean),pch = 21, data = nodes) +
# geom_text_repel(aes(X1, X2,label = elements),pch = 21, data = nodes) +
# geom_text(aes(X1, X2,label = elements),pch = 21, data = nodes) +
scale_colour_manual(values = c("#377EB8","#E41A1C")) +
scale_size(range = c(4, 14)) +
scale_x_continuous(breaks = NULL) + scale_y_continuous(breaks = NULL) +
theme(panel.background = element_blank()) +
theme(axis.title.x = element_blank(), axis.title.y = element_blank()) +
theme(legend.background = element_rect(colour = NA)) +
theme(panel.background = element_rect(fill = "white", colour = NA)) +
theme(panel.grid.minor = element_blank(), panel.grid.major = element_blank())
p3

### 随机排布网络
这里会失去模块化的信息,仅仅展示打乱后的网络,使用Model_Gephi 中第二个表格就可以实现这样的操作。
result2 <- Model_Gephi(cor = cor,
method = "cluster_fast_greedy"
)
node = result2[[2]]
#---node节点注释#-----------
nodes = nodeadd(plotcord =node,otu_table = otu_table,tax_table = tax_table)
head(nodes)## X1 X2 elements Kingdom Phylum
## ASV_1 0.000000 0.0000000 ASV_1 Bacteria Actinobacteria
## ASV_10 -1.806208 4.6623611 ASV_10 Bacteria Proteobacteria
## ASV_100 -1.763356 2.4270510 ASV_100 Bacteria Proteobacteria
## ASV_101 -6.967654 -0.6721612 ASV_101 Bacteria Proteobacteria
## ASV_102 -5.785521 -1.5898890 ASV_102 Bacteria Proteobacteria
## ASV_103 4.771757 3.6373525 ASV_103 Bacteria Proteobacteria
## Class Order Family Genus
## ASV_1 Actinobacteria Actinomycetales Thermomonosporaceae Unassigned
## ASV_10 Alphaproteobacteria Rhizobiales Rhizobiaceae Rhizobium
## ASV_100 Betaproteobacteria Burkholderiales Comamonadaceae Hydrogenophaga
## ASV_101 Unassigned Unassigned Unassigned Unassigned
## ASV_102 Gammaproteobacteria Unassigned Unassigned Unassigned
## ASV_103 Alphaproteobacteria Rhizobiales Phyllobacteriaceae Phyllobacterium
## Species mean
## ASV_1 Unassigned 0.041022288
## ASV_10 Unassigned 0.011729515
## ASV_100 Hydrogenophaga_intermedia 0.001693538
## ASV_101 Unassigned 0.001734661
## ASV_102 Unassigned 0.001779072
## ASV_103 Phyllobacterium_bourgognense 0.001490287#-----计算边#--------
edge = edgeBuild(cor = cor,plotcord = node)
head(edge)## X1 Y1 OTU_1 X2 Y2 OTU_2 weight
## ASV_34 -3.451912e+00 4.9075762 ASV_34 0.0000000 0.0 ASV_1 0.7922074
## ASV_18 4.978671e+00 0.4613418 ASV_18 0.8660254 0.5 ASV_28 0.7802901
## ASV_34.1 -3.451912e+00 4.9075762 ASV_34 0.8660254 0.5 ASV_28 0.7676010
## ASV_106 -2.449213e-16 1.0000000 ASV_106 -0.8660254 -0.5 ASV_8 0.8978529
## ASV_117 4.475816e+00 2.2286918 ASV_117 -0.8660254 -0.5 ASV_8 0.8207087
## ASV_112 -6.910272e+00 1.1171993 ASV_112 -0.8660254 -0.5 ASV_8 0.7032096
## wei_label midX midY
## ASV_34 + -1.7259560 2.4537881
## ASV_18 + 2.9223481 0.4806709
## ASV_34.1 + -1.2929433 2.7037881
## ASV_106 + -0.4330127 0.2500000
## ASV_117 + 1.8048955 0.8643459
## ASV_112 + -3.8881489 0.3085996colnames(edge)[8] = "cor"
head(nodes)## X1 X2 elements Kingdom Phylum
## ASV_1 0.000000 0.0000000 ASV_1 Bacteria Actinobacteria
## ASV_10 -1.806208 4.6623611 ASV_10 Bacteria Proteobacteria
## ASV_100 -1.763356 2.4270510 ASV_100 Bacteria Proteobacteria
## ASV_101 -6.967654 -0.6721612 ASV_101 Bacteria Proteobacteria
## ASV_102 -5.785521 -1.5898890 ASV_102 Bacteria Proteobacteria
## ASV_103 4.771757 3.6373525 ASV_103 Bacteria Proteobacteria
## Class Order Family Genus
## ASV_1 Actinobacteria Actinomycetales Thermomonosporaceae Unassigned
## ASV_10 Alphaproteobacteria Rhizobiales Rhizobiaceae Rhizobium
## ASV_100 Betaproteobacteria Burkholderiales Comamonadaceae Hydrogenophaga
## ASV_101 Unassigned Unassigned Unassigned Unassigned
## ASV_102 Gammaproteobacteria Unassigned Unassigned Unassigned
## ASV_103 Alphaproteobacteria Rhizobiales Phyllobacteriaceae Phyllobacterium
## Species mean
## ASV_1 Unassigned 0.041022288
## ASV_10 Unassigned 0.011729515
## ASV_100 Hydrogenophaga_intermedia 0.001693538
## ASV_101 Unassigned 0.001734661
## ASV_102 Unassigned 0.001779072
## ASV_103 Phyllobacterium_bourgognense 0.001490287p4 <- ggplot() + geom_segment(aes(x = X1, y = Y1, xend = X2, yend = Y2,color = cor),
data = edge, size = 0.5,alpha = 0.3) +
geom_point(aes(X1, X2,fill = Phylum,size = mean),pch = 21, data = nodes) +
# geom_text_repel(aes(X1, X2,label = elements),pch = 21, data = nodes) +
# geom_text(aes(X1, X2,label = elements),pch = 21, data = nodes) +
scale_colour_manual(values = c("#377EB8","#E41A1C")) +
scale_size(range = c(4, 14)) +
scale_x_continuous(breaks = NULL) + scale_y_continuous(breaks = NULL) +
theme(panel.background = element_blank()) +
theme(axis.title.x = element_blank(), axis.title.y = element_blank()) +
theme(legend.background = element_rect(colour = NA)) +
theme(panel.background = element_rect(fill = "white", colour = NA)) +
theme(panel.grid.minor = element_blank(), panel.grid.major = element_blank())
p4


根际互作生物学研究室 简介
根际互作生物学研究室是沈其荣教授土壤微生物与有机肥团队下的一个关注于根际互作的研究小组。本小组由袁军副教授带领,主要关注:1.植物和微生物互作在抗病过程中的作用;2 环境微生物大数据整合研究;3 环境代谢组及其与微生物过程研究体系开发和应用。团队在过去三年中在 isme J, Microbiome, PCE,SBB,Horticulture Research等期刊上发表了多篇文章。欢迎关注 微生信生物 公众号对本研究小组进行了解。
团队工作及其成果 (点击查看)
了解 交流 合作
团队成员邮箱 袁军:
junyuan@njau.edu.cn;
文涛:
2018203048@njau.edu.cn
团队公众号: