骚操作-将sna包中全部layout都要用ggplot模块化展示出来

[toc]

写在前面

为了让cluster更加灵活,我写了一个randomClusterG布局,这个布局根据模块生成坐标,但是坐标是随意生成的,并且不同模块之间不会重叠。每次出图都是不一样的,下面带领大家学习学习。

第一部分:随机计算模块位置,可视化网络

输入文件

#--导入所需R包#-------
library(igraph)
library(ggplot2)
library(ggClusterNet)
library(sna)
#-----导入数据#-------
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注释文件,用于分组#-----

#-提取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)

group$ID = row.names(group)

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

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

#--------计算布局#---------
#-------计算网络布局-得到节点坐标=node#---------
result2 = randomClusterG (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) +
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("0.png",pnet)

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

#--------计算布局#---------
#-------计算网络布局-得到节点坐标=node#---------
result2 = randomClusterG (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) +
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("1.png",pnet)

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

第二部分:sna包中全部的layout都要模块化展示

写在前面

sna包中有好多layout,但是正如大家所知道的,使用起来很少,因为还是太难看了,今天我将这些layout都进行一个模块化输出,希望可以改善一些layout的可视化效果。

微生物网络

输入文件

#--导入所需R包#-------
library(igraph)
library(network)
library(sna)
library(ggplot2)
library(ggrepel)
library(ggClusterNet)
#-----导入数据#-------
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.05,r.threshold=0.6,p.threshold=0.05,method = "pearson")

#--提取相关矩阵
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)

group$ID = row.names(group)

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

### 计算布局: "circle" :布局

# layouts = c("circle","adj","circrand","eigen","random")
#-------计算网络布局-得到节点坐标=node#---------
result2 = ranSNEClusterG (cor= cor,layout ="circle")

node = result2[[1]]
head(node)
# row.names(node) = node$elements

dim(node)

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

ggsave("10.png",pnet)
# pnet <- pnet + geom_text_repel(aes(X1, X2,label=elements),size=4, data = plotcord)

“circrand” 布局

# layouts = c("circle","adj","circrand","eigen","random")
#-------计算网络布局-得到节点坐标=node#---------
result2 = ranSNEClusterG (cor= cor,layout ="circrand")

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

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

ggsave("11.png",pnet)

“eigen”布局

# layouts = c("circle","adj","circrand","eigen","random")
#-------计算网络布局-得到节点坐标=node#---------
result2 = ranSNEClusterG (cor= cor,layout ="eigen")

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

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
ggsave("12.png",pnet)

“random”布局

# layouts = c("circle","adj","circrand","eigen","random")
#-------计算网络布局-得到节点坐标=node#---------
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,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

ggsave("13.png",pnet)

(0)

相关推荐