首页 > 其他分享 >R:microtable包计算相对丰度堆叠柱状图

R:microtable包计算相对丰度堆叠柱状图

时间:2024-06-06 09:01:10浏览次数:23  
标签:abund microtable library dataset 堆叠 柱状图 丰度 计算

rm(list = ls())
setwd("C:\\Users\\Administrator\\Desktop\\New_microtable") #设置工作目录
library(microeco)
library(magrittr)
library(dplyr)
library(tibble)

feature_table <- read.table('Bac_genus.txt', header = TRUE, row.names = 1, sep = "\t", fill = TRUE) #特征表
# 检查并处理缺失值
if (any(is.na(feature_table))) {
  feature_table[is.na(feature_table)] <- 0  # 将缺失值填充为零
}
sample_table <- read.table('sample_table.txt', header = TRUE, row.names = 1, sep = "\t") #样本表
tax_table <- read.table('tax_table_g.txt', header = TRUE, row.names = 1, sep = "\t", fill = TRUE) #分类表

dataset <- microtable$new(sample_table = sample_table,
                          otu_table = feature_table, 
                          tax_table = tax_table)

dataset$tidy_dataset() #整理和预处理数据集
#数据清洗:移除或填补缺失值、异常值等。
#数据标准化:确保数据符合一定的格式,比如统一的数据类型。
#数据整合:如果有多个表格,确保它们之间的链接正确无误。

dataset$sample_sums() %>% range #计算并查看样本总数的范围

dataset$rarefy_samples(sample.size = 1000000) #执行重采样,标准化样本中的测序深度

dataset$sample_sums() %>% range #计算并查看标准化后样本总数的范围


dataset$cal_abund() #计算每个分类等级的分类群丰度
#class(dataset$taxa_abund)
dataset$taxa_abund$Genus[1:5, 1:5]

t1 <- trans_abund$new(dataset = dataset, taxrank = "Genus", ntaxa = 10, groupmean = "Subgroup")
#t1 <- trans_abund$new(dataset = dataset, taxrank = "Genus", ntaxa = 10, groupmean = "Group")
#t1 <- trans_abund$new(dataset = dataset, taxrank = "Genus", ntaxa = 10)

write.table(t1$data_abund, file = "trans_abund_data_DAS.txt", sep = "\t", row.names = TRUE, col.names = NA, quote = FALSE)

########################################################################################

# 清空当前环境中的所有对象
rm (list = ls ())
setwd("C:\\Users\\Administrator\\Desktop\\New_microtable") #设置工作目录
library(ggplot2)   # 用于绘图
library(ggalluvial) # 用于绘制柱状图背后的条带
library(grid) # 用于访问unit函数
ra <- as.matrix(read.table("Genus_top15.txt", row.names =1, 
                           header = F, sep = "\t")) # 读入相对丰度数据并转换为矩阵方便后续数据整理

group <- c("B73", "Mo17")  # 品种变量
code <- c("B73_DAS28","B73_DAS42","B73_DAS56","B73_DAS70","Mo17_DAS28","Mo17_DAS42","Mo17_DAS56","Mo17_DAS70") # 8组处理变量
code_rep <- rep(code, each = 15)
taxa_rep <- rep(rownames(ra), 8)
cultivar_rep <- rep(group, each = 60)
abundance_vec <- as.vector(ra)
dat <- data.frame(code = code_rep,
                  Phylum = taxa_rep,
                  cultivar = cultivar_rep,
                  abundance = abundance_vec)

dat$Phylum <- factor(dat$Phylum, levels = c("Streptomyces","Sphingobium","Sphingomonas","Bradyrhizobium",
                                            "Variovorax","Lysobacter","Pseudomonas","Acidovorax","Nocardioides",
                                            "Burkholderia","Stenotrophomonas","Mesorhizobium","Microbacterium","Rhizobium","Roseateles"))
ggplot(dat, aes(x = code, y = abundance, fill = Phylum))+
  geom_bar(stat = "identity", width = 0.6, alpha = 1)+ 
  geom_flow(aes(alluvium = Phylum), alpha = 0.4) + 
  scale_fill_manual(values = c(
    "#F2A2C7", "#E9C46A", "#88C695", "#F4E3A1", "#76B4BD", "#C9A8C5", "#6D6DB5", "#5BA8A0", 
    "#9CAD60", "#C0C0C0", "#BC8F8F", "#76B4BD", "#6AB47B", "#89CFF0", "#8A2BE2"
  ))+
  theme_bw()+ 
  facet_grid(.~cultivar, scales = "free_x", space = "free_x")+ 
  xlab("")+ 
  ylab("Relative abundance (%)")+ 
  theme(panel.grid.major.x = element_blank(),
        axis.text.x = element_text(size = rel(1.3), angle = 0, face = "plain", color = "black", family = "Times New Roman"),
        axis.text.y = element_text(size=rel(1.8), face = "plain", color = "black", family = "Times New Roman"),
        legend.text = element_text(size = rel(1), family = "Times New Roman"),
        strip.text.x = element_text(size = rel(2), face = "bold", family = "Times New Roman"),
        strip.background = element_rect(fill = "lightblue", size = 1),
        panel.border = element_rect(colour = "black", fill = NA, size = 1.2),
        axis.title.y = element_text(size = 18, family = "Times New Roman"),
        legend.position = "bottom",
        legend.title = element_blank(),
        legend.key.size = unit(0.6, "cm"),
        legend.spacing.x = unit(0.1, "cm"),
        legend.box.margin = margin(t = -20, unit = "pt")) + 
  guides(fill = guide_legend(ncol = 7)) + 
  scale_y_continuous(expand = c(0, 0.01))

ggsave("Abundance2.png", width = 10, height = 7, dpi = 1200) # 图片导出,导出为pdf文件,设置图片长和宽

 

标签:abund,microtable,library,dataset,堆叠,柱状图,丰度,计算
From: https://www.cnblogs.com/wzbzk/p/18234372

相关文章

  • 代码随想录算法训练营Day60 | 84.柱状图中最大的矩形 | Python | 个人记录向
    注:今天是代码随想录训练营的最后一天啦!!!本文目录84.柱状图中最大的矩形做题看文章以往忽略的知识点小结个人体会84.柱状图中最大的矩形代码随想录:84.柱状图中最大的矩形Leetcode:84.柱状图中最大的矩形做题无思路。看文章与42.接雨水很像,42.接雨水是找每个......
  • 60天【代码随想录算法训练营34期】第十章 单调栈part03 (84.柱状图中最大的矩形)
    84.柱状图中最大的矩形classSolution:deflargestRectangleArea(self,heights:List[int])->int:s=[0]result=0heights.insert(0,0)heights.append(0)foriinrange(1,len(height......
  • 折线图加柱状图
    在data函数中定义数据myopiaList或从后端接口中拿到数据是个可以实时更新改变的echarts视图myopiaList:[24,50,70];initLineChart(){constmyopiaData=Array.isArray(this.lineChartConfig.series)?this.lineChartConfig.series:[];......
  • amCharts绘制折线图和柱状图混合
    代码案例<!DOCTYPEhtml><html><head><scriptsrc="https://cdn.amcharts.com/lib/5/index.js"></script><scriptsrc="https://cdn.amcharts.com/lib/5/xy.js"></script><scriptsrc=&qu......
  • amCharts绘制堆叠面积图
    代码案例<!DOCTYPEhtml><html><head><scriptsrc="https://cdn.amcharts.com/lib/5/index.js"></script><scriptsrc="https://cdn.amcharts.com/lib/5/xy.js"></script><scriptsrc=&qu......
  • 封装通用 ECharts 图表组件(四):分格柱状图实现
    在数据可视化的世界中,ECharts以其强大的功能和灵活性,成为了开发者们的首选图表库之一。继我们之前探讨的环形图封装之后,本文将带领大家进入另一个实用且美观的图表类型——分格柱状图的封装实现。分格柱状图简介分格柱状图是一种特殊的柱状图,它通过将每个柱子分割成多个小格......
  • 统计不同文件夹中的文件数量,并绘制相应的柱状图。
    一、数据类型每个文件夹下都是这种文件,虽然可以通过手动数出来了解文件数量,但为了更直观地看到每个文件夹的文件数量,可以使用图表来表示,这样会更加清晰。效果展示:  二、代码实现 importosimportmatplotlib.pyplotaspltfolder_names=['0','1','2','3']......
  • echarts-柱状图翻转 适合排名展示 越小越大,越大越小
    先上效果(折线图也可,看代码中标注*的位置):代码:dataList=[1,2,9,8,10,14,3];//初始数值*dataList1=[];//翻转后的数值*vardd=2;//系数用来防止计算后为0不显示*maxValue=dataList[0];//*maxValue=Math.max(...dataList);//取最大值*[dataList1]......
  • 力扣-84. 柱状图中最大的矩形
    1.题目介绍题目地址(84.柱状图中最大的矩形-力扣(LeetCode))https://leetcode.cn/problems/largest-rectangle-in-histogram/题目描述给定n个非负整数,用来表示柱状图中各个柱子的高度。每个柱子彼此相邻,且宽度为1。求在该柱状图中,能够勾勒出来的矩形的最大面积。 示......
  • POI 重叠、并列柱状图(条形图),显示数据,自定义颜色
    1、pom.xml<dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>4.1.2</version></dependency><dependency><groupId>org.apache.poi</groupId><artifac......