ggClusterNet-多边形可变变径布局网络(randomRrClusterG)

写在前面

多边形排布,将不同的模块按照多边形排布进行。模块越大的部分,其半径越大,是按照模块内节点数量判断到多边形中心的距离。说通俗一点,就是这个函数可以根据模块节点多少设置合适的大小展示,并且不会重叠。

可以有以下几个使用情况:

  • 按照微生物门水平分类节点,然后展示,大圈的OTU数量多,小圈的数量少。

微生物网络

输入文件

#--导入所需R包#-------
library(igraph)
library(network)
library(sna)
library(ggplot2)
library(ggrepel)
library(ggClusterNet)

#-----导入数据#-------
ps = readRDS("../ori_data/ps_liu.rds")

corMicro函数用于计算相关

按照丰度过滤微生物表格,并计算相关矩阵,按照指定的阈值挑选矩阵中展示的数值。这里我们换用spearman计算相关性

#-----微生物网络构建参数设置#----
#-提取丰度前百分之多少的otu进行构建网络
# N = 0.02
# r.threshold=0.6
# p.threshold=0.05

#----------计算相关#----
result = corMicro (ps = ps,N = 0.02,r.threshold=0.8,p.threshold=0.05,method = "spearman")

#--提取相关矩阵
cor = result[[1]]
# head(cor)

制作分组

这是网络布局的基础,无论是什么聚类布局,都需要制作一个分组文件,这个文件有两列,一列是节点,一列是分组信息,这个分组信息名称为:group。

#-提取tax注释文件,用于分组#-----

#-提取tax表格--目的是按照分类等级进行分组聚类绘制网络#----

ps_net = result[[3]]

vegan_tax <- function(physeq){
tax <- tax_table(physeq)

return(as(tax,"matrix"))
}
tax_table = as.data.frame(vegan_tax(ps_net))
group = as.data.frame(tax_table)
head(group)

# colnames(tax) = c("Kingdom","Phylum","Class","Order","Family","Genus","Species")

group$ID = row.names(group)

#--指定分组,也就是otu一列,分组一列
netClu = data.frame(ID = row.names(group),group = group$Phylum)

randomRrClusterG 根据分组,计算布局

这个是按照模块的大小确定半径的,如果模块大小相同(也就是包含的节点一样多),则这个layout就和半径一样,和PolygonClusterG的算法一样了。这个算法主要会计算出来两个半径,一个是聚类小模块的半径,另外一个是模块之间的距离,主要是和圆心的距离。

#--------计算布局#---------
#-------计算网络布局-得到节点坐标=node#---------
result2 = PolygonRrClusterG (cor = cor,nodeGroup =netClu )

node = result2[[1]]
head(node)

nodeadd 节点注释的简单封装,便捷实用otu表格和分组文件进行注释

vegan_otu <- function(physeq){
OTU <- otu_table(physeq)
if(taxa_are_rows(OTU)){
OTU <- t(OTU)
}
return(as(OTU,"matrix"))
}
otu_table = as.data.frame(t(vegan_otu(ps_net)))

#---node节点注释#-----------
nodes = nodeadd(plotcord =node,otu_table = otu_table ,tax_table = tax_table)
head(nodes)
#-----计算边#--------
edge = edgeBuild(cor = cor,plotcord = node)

head(edge)

出图

pnet <- ggplot() + geom_segment(aes(x = X1, y = Y1, xend = X2, yend = Y2,color = as.factor(wei_label)),
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())
pnet

# pnet <- pnet + geom_text_repel(aes(X1, X2,label=elements),size=4, data = plotcord)

制作分组,我们模拟不同的分组查看效果 8个分组

这个是按照模块的大小确定半径的,如果模块大小相同(也就是包含的节点一样多),则这个layout就和半径一样,和PolygonClusterG的算法一样了。

netClu = data.frame(ID = row.names(tax_table),group =rep(1:8,length(row.names(tax_table)))[1:length(row.names(tax_table))] )
netClu$group = as.factor(netClu$group)
head(netClu)

PolygonRrClusterG 根据分组,分布计算布局

PolygonRrClusterG 可以根际分组节点的大小对圆圈半径进行调整,节点越多,圆圈半径越大。

result2 = PolygonRrClusterG (cor = cor,nodeGroup =netClu )

node = result2[[1]]
head(node)

### nodeadd 节点注释的简单封装,便捷实用otu表格和分组文件进行注释

# ---node节点注释#-----------
nodes = nodeadd(plotcord =node,otu_table = otu_table,tax_table = tax_table)
head(nodes)

#-----计算边#--------
edge = edgeBuild(cor = cor,plotcord = node)

head(edge)

### 出图

pnet <- ggplot() + geom_segment(aes(x = X1, y = Y1, xend = X2, yend = Y2,color = as.factor(wei_label)),
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())
pnet

# pnet <- pnet + geom_text_repel(aes(X1, X2,label=elements),size=4, data = plotcord)

这里设置不同的模块大小

我们模拟不同的模块大小来计算。

vegan_tax <- function(physeq){
tax <- tax_table(physeq)

return(as(tax,"matrix"))
}
tax_table = as.data.frame(vegan_tax(ps_net))

n = rep(c(2,2,3,3,3,4,4,4,4,5,5,5,5,5,6,6,6,6,6,6,7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8,8,8,8,8),length(row.names(tax_table)))[1:length(row.names(tax_table))]

netClu = data.frame(ID = row.names(tax_table),group =n)
netClu$group = as.factor(netClu$group)
head(netClu)

randomClusterG 根据分组,随机分布计算布局

result2 = PolygonRrClusterG (cor = cor,nodeGroup =netClu )

node = result2[[1]]
head(node)

### nodeadd 节点注释的简单封装,便捷实用otu表格和分组文件进行注释

# ---node节点注释#-----------
nodes = nodeadd(plotcord =node,otu_table = otu_table,tax_table = tax_table)
head(nodes)

#-----计算边#--------
edge = edgeBuild(cor = cor,plotcord = node)

head(edge)

### 出图

pnet <- ggplot() + geom_segment(aes(x = X1, y = Y1, xend = X2, yend = Y2,color = as.factor(wei_label)),
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())
pnet

# pnet <- pnet + geom_text_repel(aes(X1, X2,label=elements),size=4, data = plotcord)

(0)

相关推荐