今天小果学习一下ggClusterNet这个包,进行共线网络的绘制,代码如下:
- 安装需要的R包
install.packages(“igraph”)
install.packages(“ggplot2”)
install.packages(“phyloseq”)
install.packages(“sna”)
install.packages(“devtools”)
install.packages(“tidyfst”)
devtools::install_github(“taowenmicro/ggClusterNet”)
install.packages(“tidyverse”)
- 导入需要的R包
library(igraph)
library(phyloseq)
library(sna)
library(ggClusterNet)
library(ggplot2)
library(tidyfst)
library(tidyverse)
- 代码展示
#导入示例数据#
data(ps)
#-提取丰度最高的指定数量的otu进行构建网络
#计算相关#
result = corMicro (ps = ps,
N = 150,
method.scale = “TMM”,
r.threshold=0.8,
p.threshold=0.05,
method = “spearman”
)
#提取相关矩阵
cor = result[[1]]
head(cor)
#网络中包含的OTU的phyloseq文件提取
ps_net = result[[3]]
#-导出otu表格
otu_table = ps_net %>%
vegan_otu() %>%
t() %>%
as.data.frame()
#人工构造分组信息:将网络中全部OTU分为五个部分
netClu=data.frame(ID=row.names(otu_table),group=rep(1:5,length(row.names(otu_table)))[1:length(row.names(otu_table))] )
netClu$group = as.factor(netClu$group)
#计算布局#
result2 = PolygonClusterG (cor = cor,nodeGroup =netClu )
node = result2[[1]]
#nodeadd 节点注释的:用otu表格和分组文件进行注释
#nodeadd函数只是提供了简单的用注释函数,用户可以自己在node的表格后面添加各种注释信息
tax_table = ps_net %>%
vegan_tax() %>%
as.data.frame()
#node节点注释#
nodes = nodeadd(plotcord =node,otu_table = otu_table,tax_table = tax_table)
#计算边#
edge = edgeBuild(cor = cor,node = node)
p <- ggplot() + geom_segment(aes(x = X1, y = Y1, xend = X2, yend = Y2,color = as.factor(cor)),
data = edge, size = 0.5) +
geom_point(aes(X1, X2,fill = Phylum,size = mean),pch = 21, data = nodes) +
scale_colour_brewer(palette = “Set1”) +
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(p,“ggcluster1.pdf”)
#分8组
netClu=data.frame(ID=row.names(cor),group=rep(1:8,length(row.names(cor)))[1:length(row.names(cor))] )
netClu$group = as.factor(netClu$group)
result2 = PolygonClusterG (cor = cor,nodeGroup =netClu )
node = result2[[1]]
# node节点注释#
nodes = nodeadd(plotcord =node,otu_table = otu_table,tax_table = tax_table)
#计算边#
edge = edgeBuild(cor = cor,node = node)
### 出图
p1 <- ggplot() + geom_segment(aes(x = X1, y = Y1, xend = X2, yend = Y2,color = as.factor(cor)),
data = edge, size = 0.5) +
geom_point(aes(X1, X2,fill = Phylum,size = mean),pch = 21, data = nodes) +
scale_colour_brewer(palette = “Set1″) +
scale_x_continuous(breaks = NULL) + scale_y_continuous(breaks = NULL) +
# labs( title = paste(layout,”network”,sep = “_”))+
# geom_text_repel(aes(X1, X2,label=Phylum),size=4, data = plotcord)+
# discard default grid + titles in ggplot2
theme(panel.background = element_blank()) +
# theme(legend.position = “none”) +
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(“ggCluster.2.pdf”,p1)