一、引言
用于微生物组网络分析的工具包括MENA、WGCNA、igraph、ggraph、SpiecEasi、Cytoscape、Gephi、NetworkX和SparCC等。每个工具在网络构建和可视化上各具优势,例如MENA基于随机矩阵理论,具备良好的抗噪性能;WGCNA构建无尺度加权基因网络;SpiecEasi结合数据转换与图形模型推理框架。一些工具如Cytoscape、Gephi和R包(igraph、ggraph)提供可视化功能,但存在操作繁琐、重复性差和美观性不足的问题。
为解决这些局限,南京农业大学袁军和中科院刘永鑫团队开发了R包ggClusterNet,该包于2022年发表在国内主办的微生物领域全球第一期刊iMeta上,截止推文发稿日期已经被引141次,广受微生物生态研究者关注和喜爱。特点包括分析快速、通过少量代码即可完成网络分析且具有可重复性,最大的特点是提供了多种微生物生态网络可视化工具,及网络特性的深度挖掘。本期带给大家ggClusterNet高达十种网络布局,带你摆脱绘制出的网络图不好看以及专业软件Cytoscape和Gephi入门难得问题。下期我们会接着探讨网络特性深度挖掘,包括网络属性,节点属性,zi-pi图等。
如下展示了ggClusterNet的分析流程:
图1 分析流程图
二、基本知识
图2 ggClusterNet中网络可视化的十种布局算法
ggClusterNet特点之一就是提供了十种可供选择的可视化布局,这大大减少了网络绘制的时间和难度,提升科研效率,如下详细介绍了十种网络图特点。
randomClusterG: 随机布局,环状模块。模块(组)的所有节点都排列成一个环。多个模块绘制为相同半径的多个圆。接着,设计了一个函数,可以在绘图面板中随机排列这些圆环。
ArtifCluster: 环状模块,人工布局。模块(组)的所有节点都排列成一个环。多个模块绘制为相同半径的多个圆,然后通过设置坐标值手动排列这些圆。
randSNEClusterG: sna包中的布局按照模块布局。模块(组)的所有节点分别按 sna 包中的多种布局排列。多个模块随机排列在绘图面板中。
PolygonModsquareG: 环状布局,顺序行列排布。模块(组)的所有节点都排列成一个环。多个模块绘制为不同半径的多个圆(节点数量越多,圆的半径越大)。然后这些圆手动排列成一行或多行。
model_maptree: 按照maptree算法布局模块。首先对网络进行模块化分析,并按网络模块化对节点进行分组,然后用于计算坐标。节点的相对位置根据 Weixin Wang 等人开发的算法计算,该算法试图在添加圆时找到最密集的排列方式。
model_igraph: 模仿igraph布局。所有节点使用 Fruchterman 和 Reingold 的力导向布局算法放置在平面上。通过 R 包 igraph 计算的高度节点趋向于聚集,而低度节点分布在周围网络中。
model_Gephi.2: 模仿Gephi布局。所有节点绘制为一个圆,并计算坐标。然后使用每个节点的坐标值来构建簇,并重新分配给节点。
PolyRdmNotdCirG: 根据模块信息,节点随机分布在不同半径的多个圆中(节点数量越多,圆的半径越大)。然后这些模块按规则排列成一个多边形的顶点(边的数量等于模块的数量),以坐标轴的原点为中心。
PolygonRrClusterG: 环状模块,模块半径正比于节点数量,环状布局。模块(组)的所有节点都排列成一个环。多个模块绘制为不同半径的多个圆(节点数量越多,圆的半径越大)。然后这些圆按规则排列成多边形的顶点,以坐标轴的中心为中心。
PolygonClusterG: 环状模块,环状布局。模块(组)的所有节点都排列成一个环。多个模块绘制为相同半径的多个圆。然后这些圆按规则排列成多边形的顶点(边的数量等于模块的数量),以坐标轴的原点为中心。
三、示例数据和代码
这套代码基于ggClusterNet包自带的数据进行分析,同时我们也提供了读取自己数据的方法,代码均可留言,小编会及时发送原始代码和示例数据。
🌟数据基本操作之包安装,微生物网络模型构建
# 清理工作环境中的所有对象
rm(list = ls())
#检测并安装所需R包#
if (!require("phyloseq")) { install.packages("phyloseq") }
if (!require("igraph")) { install.packages("igraph") }
if (!require("network")) { install.packages("network") }
if (!require("sna")) { install.packages("sna") }
if (!require("tidyverse")) { install.packages("tidyverse") }
library(devtools)
if (!require("ggClusterNet")) {remotes::install_github("taowenmicro/ggClusterNet") }
if (!require("ggrepel")) { install.packages("ggrepel") }
if (!require("tidyfst")) { install.packages("tidyfst") }
#导入所需R包
library(phyloseq)
library(igraph)
library(network)
library(sna)
library(tidyverse)
library(ggClusterNet)
library(ggrepel)
#加载包里的数据
data(ps)
#计算微生物网络
result = corMicro (ps = ps,
N = 250,
r.threshold=0.8,
p.threshold=0.05
)
#提取数据
cor = result[[1]]
dim(cor)
ps_net = result[[3]]
#导出表格
otu_table = as.data.frame(t(vegan_otu(ps_net)))
tax_table = as.data.frame(vegan_tax(ps_net))
#OTU模块化分组
netClu = modulGroup( cor = cor,cut = 3,method = "cluster_fast_greedy" )
head(netClu)
#微生物模块化分组,得到微生物分组信息(可选)
netClu = data.frame(ID = row.names(tax_table),group =rep(1:5,length(row.names(tax_table)))[1:length(row.names(tax_table))] )
netClu$group = as.factor(netClu$group)
3.1 randomClusterG布局
# 设置随机种子以确保结果的可重复性
set.seed(1112)
# 创建节点分组信息,将tax_table的行名作为节点ID,每个节点随机分配到1-12的组别
netClu = data.frame(ID = row.names(tax_table),
group = rep(1:12, length(row.names(tax_table)))[1:length(row.names(tax_table))])
# 将group列转换为因子类型
netClu$group = as.factor(netClu$group)
# 使用随机聚类函数randomClusterG生成节点布局,cor为相关性矩阵,netClu为节点分组信息
result2 = randomClusterG(cor = cor, nodeGroup = netClu)
# 从result2中提取节点信息
node = result2[[1]]
# 将节点坐标、OTU表格、分类学表格等信息整合到一个数据框中
nodes = nodeadd(plotcord = node, otu_table = otu_table, tax_table = tax_table)
# -----构建边(连接)信息-----
# 基于相关性矩阵构建节点之间的边(连接)
edge = edgeBuild(cor = cor, node = node)
# 显示边的数据框的维度
dim(edge)
# 修改edge数据框中第8列的列名为"cor",代表相关性值
colnames(edge)[8] = "cor"
# 绘制网络图
p1 <- ggplot() +
# 使用geom_segment绘制边,颜色由相关性值确定,边的透明度和宽度设定
geom_segment(aes(x = X1, y = Y1, xend = X2, yend = Y2, color = cor),
data = edge, linewidth = 0.2, alpha = 0.6) +
# 使用geom_point绘制节点,节点大小由平均值决定,节点填充颜色由分类信息(Phylum)决定
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())
# 显示绘制的网络图
p1
# 保存绘制的网络图为PDF文件,设置宽度和高度,并允许保存超出限制的图像
ggsave("1.pdf", p1, width = 20, height = 19, limitsize = FALSE)
3.2 PolygonClusterG布局
##################PolygonClusterG布局###################
netClu = data.frame(ID = row.names(tax_table),group =rep(1:12,length(row.names(tax_table)))[1:length(row.names(tax_table))] )
netClu$group = as.factor(netClu$group)
set.seed(12)
result2 = PolygonClusterG(cor = cor,nodeGroup =netClu,zoom = 0.8,zoom2 = 0.8 )
node = result2[[1]]
head(node)
#node节点注释
nodes = nodeadd(plotcord =node,otu_table = otu_table,tax_table = tax_table)
head(nodes)
#计算边
edge = edgeBuild(cor = cor,node = node)
colnames(edge)[8] = "cor"
head(edge)
p1 <- ggplot() + geom_segment(aes(x = X1, y = Y1, xend = X2, yend = Y2,color = cor),
data = edge, size = 0.5,alpha = 0.6) +
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())
p1
ggsave("2.pdf",p1,width = 20,height = 19)
3.3 PolygonRrClusterG布局
######################PolygonRrClusterG布局####################
netClu = modulGroup( cor = cor,cut = 3,method = "cluster_fast_greedy" )
head(netClu)
netClu$group = as.factor(netClu$group)
set.seed(12)
result2 = PolygonRrClusterG(cor = cor,nodeGroup =netClu,zoom2 = 2,zoom = 0.01)
node = result2[[1]]
head(node)
#node节点注释
nodes = nodeadd(plotcord =node,otu_table = otu_table,tax_table = tax_table)
head(nodes)
#计算边
edge = edgeBuild(cor = cor,node = node)
colnames(edge)[8] = "cor"
head(edge)
p1 <- ggplot() + geom_segment(aes(x = X1, y = Y1, xend = X2, yend = Y2,color = cor),
data = edge, size = 0.5,alpha = 0.6) +
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())
p1
ggsave("3.pdf",p1,width = 17,height = 16)
3.4 ranSNEClusterG布局
#########################ranSNEClusterG布局##################
netClu = data.frame(ID = row.names(tax_table),group =rep(1:5,length(row.names(tax_table)))[1:length(row.names(tax_table))] )
netClu$group = as.factor(netClu$group)
#?ranSNEClusterG
result2 = ranSNEClusterG (cor= cor,layout ="circrand")
node = result2[[1]]
head(node)
#node节点注释
nodes = nodeadd(plotcord =node,otu_table = otu_table,tax_table = tax_table)
head(nodes)
#计算边#
edge = edgeBuild(cor = cor,node = node)
colnames(edge)[8] = "cor"
head(edge)
p1 <- ggplot() + geom_segment(aes(x = X1, y = Y1, xend = X2, yend = Y2,color = cor),
data = edge, size = 0.5,alpha = 0.6) +
geom_point(aes(X1, X2,fill = Phylum),pch = 21, data = nodes,size = 5) +
# 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())
p1
#
ggsave("4.pdf",p1,width = 12,height = 10)
result2 = ranSNEClusterG (cor= cor,layout ="random")
node = result2[[1]]
head(node)
#node节点注释
nodes = nodeadd(plotcord =node,otu_table = otu_table,tax_table = tax_table)
head(nodes)
#计算边
edge = edgeBuild(cor = cor,node = node)
colnames(edge)[8] = "cor"
head(edge)
p1 <- ggplot() + geom_segment(aes(x = X1, y = Y1, xend = X2, yend = Y2,color = cor),
data = edge, size = 0.5,alpha = 0.6) +
geom_point(aes(X1, X2,fill = Phylum),pch = 21, data = nodes,size = 4) +
# 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())
p1
ggsave("5.pdf",p1,width = 12,height = 10)
3.5 PolygonModsquareG布局
########################PolygonModsquareG布局#####################
netClu = data.frame(ID = row.names(tax_table),group =rep(1:50,length(row.names(tax_table)))[1:length(row.names(tax_table))] )
netClu$group = as.factor(netClu$group)
result2 <- PolygonModsquareG(cor = cor,nodeGroup =netClu,r1 = 10,N = 10,cut = 8)
node = result2[[1]]
#node节点注释
nodes = nodeadd(plotcord =node,otu_table = otu_table,tax_table = tax_table)
head(nodes)
#计算边
edge = edgeBuild(cor = cor,node = node)
colnames(edge)[8] = "cor"
head(edge)
p1 <- ggplot() + geom_segment(aes(x = X1, y = Y1, xend = X2, yend = Y2,color = cor),
data = edge, size = 0.5,alpha = 0.6) +
geom_point(aes(X1, X2,fill = Phylum),pch = 21, data = nodes,size =3) +
# 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())
p1
ggsave("6.pdf",p1,width = 50,height =50,limitsize = FALSE)
3.6 PolyRdmNotdCirG布局
netClu = data.frame(ID = row.names(tax_table),group =rep(1:3,length(row.names(tax_table)))[1:length(row.names(tax_table))] )
netClu$group = as.factor(netClu$group)
result2 = PolyRdmNotdCirG (cor = cor,nodeGroup =netClu )
node = result2[[1]]
head(node)
# ---node节点注释#-----------
nodes = nodeadd(plotcord =node,otu_table = otu_table,tax_table = tax_table)
head(nodes)
#-----计算边#--------
edge = edgeBuild(cor = cor,node = node)
colnames(edge)[8] = "cor"
head(edge)
p1 <- ggplot() + geom_segment(aes(x = X1, y = Y1, xend = X2, yend = Y2,color = cor),
data = edge, size = 0.5,alpha = 0.6) +
geom_point(aes(X1, X2,fill = Phylum),pch = 21, data = nodes,size = 3) +
# 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())
p1
ggsave("8.pdf",p1,width = 10,height =9)
3.7 rArtifCluster布局(人工布局)
#################rArtifCluster布局(人工布局)#####################
if(!require("gganatogram")){devtools::install_github("jespermaag/gganatogram")}
if(!require("viridis")){install.packages("viridis")}
library(gganatogram)
library(viridis)
##-----人工置顶半径大小和圆心位置
#--这里我设置r都是相同的,也可以设置不同,然后包装成一个向量就可以了#-------
art = netClu
art$group = 1:5
xs = as.data.frame(table(art$group))
r = rep(15,length(xs$Freq))
r
#----准备圆心坐标,往往与你的设计有关#---------
# 有多少个模块就提供多少个坐标
#--指定坐标吮顺序按照一下指定
#-------人工准备坐标
ax1 = c(120,0)
ax2 = c(130,-30)
ax3 = c(140,-70)
ax4 = c(130,-110)
ax5 = c(120,-140)
da = rbind(ax1,ax2,ax3,ax4,ax5)
#--------计算布局#---------
#-------计算网络布局-得到节点坐标=node#---------
result2 = ArtifCluster(cor = cor,nodeGroup =art,r = r,da =da)
node = result2[[1]]
head(node)
# ---node节点注释#-----------
nodes = nodeadd(plotcord =node,otu_table = otu_table,tax_table = tax_table)
head(nodes)
#-----计算边#--------
edge = edgeBuild(cor = cor,node = node)
colnames(edge)[8] = "cor"
head(edge)
library(gganatogram)
library(viridis)
organism = "solanum_lycopersicum.whole_plant"
data =other_key[[organism]]
library(scales)
show_col(data$colour)
data$colour =c("#4DAF4A")
# data$colour[1] = ""
# , sex='female', fill="colour"
p = gganatogram(data=data, organism=organism,ggplot2_only = TRUE,fillOutline='#a6bddb',fill = "colour") +
# theme_void() +
ggtitle(organism) +
theme(plot.title = element_text(hjust=0.5, size=9))
p
p1 <- p + geom_segment(aes(x = X1, y = Y1, xend = X2, yend = Y2,color = cor),
data = edge, size = 0.5,alpha = 0.6) +
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())
p1
#--------按照每个部分添加一个箭头
da2 = data.frame(x = da[,1] -50,y = da[,2])
da = as.data.frame(da)
colnames(da) = c("x0","y0")
da3 = cbind(da2,da)
da3
p2 = p1 + geom_segment(aes(x = x, y = y, xend = x0, yend = y0),data = da3,arrow = arrow(length = unit(0.2,"cm")),size = 0.7)
p2
ggsave("9.pdf",p2,width = 12,height = 11)
3.8 model_Gephi.2布局
result2 <- model_Gephi.2(cor = cor,
method = "cluster_fast_greedy",
seed = 12
)
node = result2[[1]]
head(node)
#node节点注释
nodes = nodeadd(plotcord =node,otu_table = otu_table,tax_table = tax_table)
head(nodes)
#计算边
edge = edgeBuild(cor = cor,node = node)
colnames(edge)[8] = "cor"
head(edge)
p1 <- ggplot() + geom_segment(aes(x = X1, y = Y1, xend = X2, yend = Y2,color = cor),
data = edge, size = 0.5,alpha = 0.6) +
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())
p1
ggsave("10.pdf",p1,width = 12,height = 11)
3.9 model_maptree布局
result2 <- model_maptree(cor = cor,
method = "cluster_fast_greedy",
seed = 12
)
node = result2[[1]]
head(node)
#---node节点注释#-----------
nodes = nodeadd(plotcord =node,otu_table = otu_table,tax_table = tax_table)
head(nodes)
#-----计算边#--------
edge = edgeBuild(cor = cor,node = node)
colnames(edge)[8] = "cor"
head(edge)
p1 <- ggplot() +
geom_segment(aes(x = X1, y = Y1, xend = X2, yend = Y2,color = cor),
data = edge, size = 0.5,alpha = 0.6) +
# geom_curve(aes(x = X1, y = Y1, xend = X2, yend = Y2,color = cor),
# data = edge, size = 0.5,alpha = 0.3,curvature = -0.2) +
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())
p1
ggsave("./11.pdf",p1,width = 12,height = 12)
3.10 model_igraph布局
####################model_igraph布局######################
library(igraph)
library(dplyr)
library(Hmisc)
p.threshold = 0.05
r.threshold = 0.7
x = ps %>%
# scale_micro(method = "TMM") %>%
vegan_otu() %>%
t() %>%
as.data.frame()
occor<-WGCNA::corAndPvalue(t(x)/colSums(x),method = 'pearson')
mtadj<-multtest::mt.rawp2adjp(unlist(occor$p),proc='BH')
adpcor<-mtadj$adjp[order(mtadj$index),2]
occor.p<-matrix(adpcor,dim(t(x)/colSums(x))[2])
## R value
occor.r<-occor$cor
diag(occor.r) <- 0
# occor.r[occor.p > 0.05|abs(occor.r)<0.4] = 0
occor.r[occor.p > p.threshold | abs(occor.r)< r.threshold] = 0
occor.r[is.na(occor.r)]=0
cor = occor.r
result= cor_Big_micro(ps = ps,N = 0,p.threshold = 0.05,r.threshold = 0.6,scale = FALSE)
cor = result[[1]]
p.threshold = 0.05
r.threshold = 0.6
x = ps %>%
# scale_micro(method = "TMM") %>%
vegan_otu() %>%
t() %>%
as.data.frame()
occor<-WGCNA::corAndPvalue(t(x)/colSums(x),method = 'pearson')
mtadj<-multtest::mt.rawp2adjp(unlist(occor$p),proc='BH')
adpcor<-mtadj$adjp[order(mtadj$index),2]
occor.p<-matrix(adpcor,dim(t(x)/colSums(x))[2])
## R value
occor.r<-occor$cor
diag(occor.r) <- 0
# occor.r[occor.p > 0.05|abs(occor.r)<0.4] = 0
occor.r[occor.p > p.threshold | abs(occor.r)< r.threshold] = 0
occor.r[is.na(occor.r)]=0
cor = occor.r
result2 <- model_igraph(cor = cor,
method = "cluster_fast_greedy",
seed = 12
)
node = result2[[1]]
head(node)
dat = result2[[2]]
head(dat)
tem = data.frame(mod = dat$model,col = dat$color) %>% dplyr::distinct( mod, .keep_all = TRUE)
col = tem$col
names(col) = tem$mod
#node节点注释
otu_table = as.data.frame(t(vegan_otu(ps)))
tax_table = as.data.frame(vegan_tax(ps))
nodes = nodeadd(plotcord =node,otu_table = otu_table,tax_table = tax_table)
head(nodes)
#计算边
edge = edgeBuild(cor = cor,node = node)
colnames(edge)[8] = "cor"
head(edge)
tem2 = dat %>%
dplyr::select(OTU,model,color) %>%
dplyr::right_join(edge,by =c("OTU" = "OTU_1" ) ) %>%
dplyr::rename(OTU_1 = OTU,model1 = model,color1 = color)
head(tem2)
tem3 = dat %>%
dplyr::select(OTU,model,color) %>%
dplyr::right_join(edge,by =c("OTU" = "OTU_2" ) ) %>%
dplyr::rename(OTU_2 = OTU,model2 = model,color2 = color)
head(tem3)
tem4 = tem2 %>%inner_join(tem3)
head(tem4)
edge2 = tem4 %>% mutate(color = ifelse(model1 == model2,as.character(model1),"across"),
manual = ifelse(model1 == model2,as.character(color1),"#C1C1C1")
)
head(edge2)
col_edge = edge2 %>% dplyr::distinct(color, .keep_all = TRUE) %>%
select(color,manual)
col0 = col_edge$manual
names(col0) = col_edge$color
library(ggnewscale)
p1 <- ggplot() + geom_segment(aes(x = X1, y = Y1, xend = X2, yend = Y2,color = color),
data = edge2, size = 1) +
scale_colour_manual(values = col0)
ggsave("./cs1.pdf",p1,width = 16,height = 14)
p2 = p1 +
new_scale_color() +
geom_point(aes(X1, X2,color =model), data = dat,size = 4) +
scale_colour_manual(values = col) +
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())
ggsave("./cs2.pdf",p2,width = 16,height = 14)
# plot(igraph,layout=sub_net_layout,vertex.size=2)
# p = ggplot(dat) +
# geom_point(aes(X1, X2,fill = model),pch = 21)
# p
四、参考文献
[1] Wen,Tao, Penghao Xie, Shengdie Yang, Guoqing Niu, Xiaoyu Liu, Zhexu Ding, Chao Xue, Yong-Xin Liu, Qirong Shen, and Jun Yuan. 2022. “ ggClusterNet: An R package for microbiome network analysis and modularity-based multiple network layouts.” iMeta 1, e32.
[2] Multiple_layout,github.com/taowenmicro/ggClusterNet/wiki/Multiple_layout
五、相关信息
!!!本文内容由小编总结互联网和文献内容总结整理,如若侵权,联系立即删除!
!!!有需要的小伙伴评论区获取今天的测试代码和实例数据。
📌示例代码中提供了数据和代码,小编已经测试,可直接运行。
以上就是本节所有内容。
如果这篇文章对您有用,请帮忙一键三连(点赞、收藏、评论、分享),让该文章帮助到更多的小伙伴。