人工设置网络模块位置2-大尺度空间网络布局应用
写在前面
多变形排布,将不同的模块按照多变形排布进行。模块越大的部分,其半径越大,是按照模块内节点数量判断到多变形中心的距离。
微生物网络
输入文件
#--导入所需R包#-------
library(igraph)
library(sna)
library(ggplot2)
library(ggClusterNet)
library(phyloseq)
#-----导入数据#-------
ps = readRDS("../ori_data/ps_liu.rds")
ps
corMicro函数用于计算相关
按照丰度过滤微生物表格,并却计算相关矩阵,按照指定的阈值挑选矩阵中展示的数值。
#-----微生物网络构建参数设置#----
#-提取丰度前百分之多少的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 = "pearson")
#--提取相关矩阵
cor = result[[1]]
# head(cor)
制作分组
这是网络布局的基础,无论是什么聚类布局,都需要制作一个分组文件,这个文件有两列,一列是节点,一列是分组信息,这个分组信息名称为:group。
#-提取tax注释文件,用于分组#-----
vegan_otu <- function(physeq){
OTU <- otu_table(physeq)
if(taxa_are_rows(OTU)){
OTU <- t(OTU)
}
return(as(OTU,"matrix"))
}
ps_net = result[[3]]
otu_table = as.data.frame(t(vegan_otu(ps_net)))
vegan_tax <- function(physeq){
tax <- tax_table(physeq)
return(as(tax,"matrix"))
}
tax_table = as.data.frame(vegan_tax(ps_net))
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)
head(netClu)
人工指定分组
##-----人工置顶半径大小和圆心位置
#--这里我设置r都是相同的,也可以设置不同,然后包装成一个向量就可以了#-------
xs = as.data.frame(table(netClu$group))
r = rep(4,length(xs$Freq))
r
#----准备圆心坐标,往往与你的设计有关#---------
# 有多少个模块就提供多少个坐标
#--指定坐标吮顺序按照一下指定
xs$Var1
#-------人工准备坐标
ax1 = c(80,40)
ax2 = c(90,30)
ax3 = c(110,40)
ax4 = c(130,50)
ax5 = c(110,10)
da = rbind(ax1,ax2,ax3,ax4,ax5)
rArtifCluster根据分组,人工指定布局
?ArtifCluster
#--------计算布局#---------
#-------计算网络布局-得到节点坐标=node#---------
result2 = ArtifCluster(cor = cor,nodeGroup =netClu,r = r,da =da)
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)
地图
library(maptools)
library(sf)
china_map <- rgdal::readOGR('../ori_data/mapdata/China//bou2_4p.shp')
head(fortify(china_map))
chinmap = fortify(china_map)
# chinmap = dplyr::filter(chinmap, group == 0.1)
head(chinmap)
p = ggplot()+geom_polygon(data = chinmap,aes(x=long,y=lat,group=group),colour='grey',fill = "grey80")+#,fill = group
scale_fill_discrete(guide = FALSE)
nine <- sf::st_read('../ori_data/mapdata/add_map//国界九段线.shp')
nine
p
p1 = p +geom_sf(data = nine,aes( geometry = `geometry`),color = "grey30")
p1
northear <- sf::st_read('../ori_data/mapdata/add_map//南海诸岛.shp')
p2 = p1 +geom_sf(data = northear,aes( geometry = `geometry`),color = "grey30")
p2
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 = as.factor(Phylum),size = mean),pch = 21, data = nodes) + scale_colour_brewer(palette = "Set1") +
scale_x_continuous(breaks = NULL) + scale_y_continuous(breaks = NULL)
出图
head(nodes)
pnet <- p2 + 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 = as.factor(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
ggsave("./8.png",pnet,width = 12,height = 16)
# pnet <- pnet + geom_text_repel(aes(X1, X2,label=elements),size=4, data = plotcord)
世界地图
worldmap <- sf::st_read('../ori_data/mapdata/world//WorldMap.shp')
worldmap
p = ggplot() + geom_sf(data = worldmap ,aes( geometry = `geometry`),fill = "grey80") +#,fill = `ENG_NAME`
scale_fill_discrete(guide = FALSE)
p
nine <- sf::st_read('../ori_data/mapdata/add_map//国界九段线.shp')
nine
p
p1 = p +geom_sf(data = nine,aes( geometry = `geometry`),color = "grey30")
p1
northear <- sf::st_read('../ori_data/mapdata/add_map//南海诸岛.shp')
p1
结合地图
出图
人工指定分组
##-----人工置顶半径大小和圆心位置
#--这里我设置r都是相同的,也可以设置不同,然后包装成一个向量就可以了#-------
xs = as.data.frame(table(netClu$group))
r = rep(10,length(xs$Freq))
r
#----准备圆心坐标,往往与你的设计有关#---------
# 有多少个模块就提供多少个坐标
#--指定坐标顺序按照一下指定
xs$Var1
#-------人工准备坐标
ax1 = c(-120,50)
ax2 = c(-60,0)
ax3 = c(0,50)
ax4 = c(60,50)
ax5 = c(110,10)
da = rbind(ax1,ax2,ax3,ax4,ax5)
rArtifCluster根据分组,人工指定布局
#--------计算布局#---------
#-------计算网络布局-得到节点坐标=node#---------
result2 = ArtifCluster(cor = cor, nodeGroup =netClu,r = r,da =da)
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)head(nodes)
pnet <- p1 + 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 = as.factor(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
ggsave("./9.png",pnet,width = 12,height = 16)
# pnet <- pnet + geom_text_repel(aes(X1, X2,label=elements),size=4, data = plotcord)