一、数据准备

数据使用的不同处理土壤样品的微生物组成数据,包含物种丰度,分类单元和样本分组数据。此数据为虚构,可用于练习,请不要作他用。

# 1.1 设置工作路径
#knitr::opts_knit$set(root.dir="D:\\EnvStat\\PCA")# 使用Rmarkdown进行程序运行
Sys.setlocale('LC_ALL','C') # Rmarkdown全局设置
#options(stringsAsFactors=F)# R中环境变量设置,防止字符型变量转换为因子
setwd("D:\\EnvStat\\stacked_map")

# 1.2 读入数据
## 物种组成数据
spetax <- read.table("spetax.csv", header=T, row.names=1, sep=",", comment.char="",stringsAsFactors = TRUE,quote = "")
head(spetax)

## 将物种分类单元注释信息与丰度数据分开
spe <- spetax[-c(1:7)]
tax <- spetax[1:7]
head(spe)
head(tax)

## 样本分组数据
group <- read.table("group.csv", header=T, row.names=1, sep=",", comment.char="",stringsAsFactors = TRUE,quote = "")
group$grazing <- factor(group$grazing,levels = c("CK","LG","MG","HG"))
group

R语言ggplot堆叠图 r语言画堆叠图柱状图_数据分析

图1|物种丰度及分类单元注释信息,spetax.csv。

R语言ggplot堆叠图 r语言画堆叠图柱状图_数据分析_02

图2|样本分组信息,group.csv。

二、 物种组成堆叠图  

在门水平绘制物种组成堆叠柱形图,可以绘制绝对丰度堆叠图,也可以使用相对丰度绘制百分百堆叠图。

2.1 绝对丰度物种组成堆叠柱形图

# 2.1.1 物种组成数据按照门进行汇总
## spe和tax数据表中物种排序一致
library(tidyverse)
phy <- spe %>%
  group_by(tax$Phylum) %>% # 使用tax中的门水平进行分类
  summarise_all(sum) %>%
  rename(Phylum = `tax$Phylum`) %>%
  gather(key="Samples",value = "abun",-Phylum) %>% # 数据形式转换:“宽”转“长”
  left_join(group,by=c("Samples"="name")) %>%
  select(grazing,depth,Phylum,abun) %>%
  group_by(grazing,depth,Phylum) %>% # 求均值
  summarise_all(mean)
  
dim(phy)
head(phy)

# 2.1.2 颜色
library(ggsci)
col=pal_d3("category20")(20)
col2 = pal_d3("category20",alpha = 0.5)(20)
mypal=c(col,col2[-8])

R语言ggplot堆叠图 r语言画堆叠图柱状图_开发语言_03

图3|按门汇总的各处理物种丰度均值数据,phy。long format数据形式可直接用于ggplot2绘图。

# 2.1.3  物种组成堆叠柱形图-绝对丰度
library(ggplot2)
pdf("abs_stack.pdf",width = 12,height = 10,family="Times")
ggplot()+
  geom_bar(data=phy,
           aes(x=grazing,
               weight=abun,
               fill=reorder(Phylum,-abun)),
           position = "stack",# 百分比堆叠图position = "fill"
           width=0.5)+
  facet_grid(.~depth)+
  scale_fill_manual(values = mypal[-18])+
  theme_bw()+
  guides(fill=guide_legend(title = "Phylum",ncol = 2))+
  labs(y = "Absolute abundance",x=NULL)+
  theme(legend.position="right",
        axis.title = element_text(face = "bold", 
                                  size = 12,colour = "black"))+
  theme(axis.text = element_text(face = "bold", 
                                 size = 10,color="black"),
        strip.text.x = element_text(face = "bold", 
                                   size =12,color="black"))+
  theme(panel.grid=element_blank())+
  theme(legend.title = element_text(face = "bold", 
                                   size =12,color="black"),
    legend.text = element_text(face = "bold", 
                                   size =10,color="black"))
dev.off()

R语言ggplot堆叠图 r语言画堆叠图柱状图_数据分析_04

图4|绝对丰度堆叠柱形图,abs_stack.pdf。

2.2 相对丰度物种组成堆叠柱形图

# 2.2.1  物种组成堆叠柱形图-相对丰度
pdf("rel_stack.pdf",width = 12,height = 10,family="Times")
ggplot()+
  geom_bar(data=phy,
           aes(x=grazing,
               weight=abun,
               fill=reorder(Phylum,-abun)),
           position = "fill", # ggplot2会自行计算相对丰度,无需提前计算。
           width=0.5)+
  facet_grid(.~depth)+
  scale_fill_manual(values = mypal[-18])+ # 颜色与绝对丰度堆叠柱形图保持一致
  theme_base()+
  scale_y_continuous(#expand=c(0,0), #设置横坐标轴紧挨柱形图
                     name = "Relative abundance (%)",
                     limits = c(0,1),
                     breaks = seq(0,1,0.25),
                     labels = paste(seq(0,100,25),"%")
                     )+
  guides(fill=guide_legend(title = "Phylum",ncol = 2))+
  labs(x=NULL)+
  theme(legend.position="right",
        axis.title = element_text(face = "bold", 
                                  size = 12,colour = "black"))+
  theme(axis.text = element_text(face = "bold", 
                                 size = 10,color="black"),
        strip.text.x = element_text(face = "bold", 
                                   size =12,color="black"))+
  theme(panel.grid=element_blank())+
  theme(legend.title = element_text(face = "bold", 
                                   size =12,color="black"),
    legend.text = element_text(face = "bold", 
                                   size =10,color="black"))
dev.off()

R语言ggplot堆叠图 r语言画堆叠图柱状图_数据分析_05

图5|绝对丰度堆叠柱形图,rel_stack.pdf。