首页 > 解决方案 > 如何重新排序 Plot_composition Microbiome 上的结果

问题描述

我得到了一个具有不同门类的情节组合,其他类别如下:

pseq <- Merge_Final_pourcent %>% aggregate_taxa(level = "Phylum")
ps1.com.fam <- microbiome::aggregate_top_taxa(pseq, "Phylum", top = 11)
ps1.com.fam.rel <- microbiome::transform(ps1.com.fam, "compositional")

svg(file="phylum.svg")
plot_composition(ps1.com.fam.rel) + theme(legend.position = "bottom") +
  scale_fill_brewer("Phylum", palette = "Paired") + theme_bw() +
  theme(axis.text.x = element_text(angle = 90)) +
  ggtitle("Relative abundance") + theme(legend.title = element_text(size = 18))
dev.off()

我尝试将类别归为 Other :所以我尝试更改我的 phyloseq 并仅通过 Z_other 转换名称 other 但它也从 phyloseq 对象和情节中消失:

essai<-as.data.frame(tax_table(ps1.com.fam.rel))

essai$unique <- sub("Other", "Z_Other", essai$unique)
essai$Phylum <- sub("Other", "Z_Other", essai$Phylum)
rownames(essai) <- sub("Other", "Z_Other", rownames(essai))
tax_table(ps1.com.fam.rel) <- as.matrix(essai)


essai<-as.data.frame(otu_table(ps1.com.fam.rel))
rownames(otu_table(ps1.com.fam.rel)) <- sub("Other", "Z_Other", rownames(otu_table(ps1.com.fam.rel)))
otu_table(ps1.com.fam.rel) <- as.matrix(essai)

svg(file="0_1phylum_BMP.svg")
plot_composition(ps1.com.fam.rel) + theme(legend.position = "bottom") +
  scale_fill_brewer("Phylum", palette = "Paired") + theme_bw() +
  theme(axis.text.x = element_text(angle = 90)) +
  ggtitle("Relative abundance") + theme(legend.title = element_text(size = 18))
dev.off()

结果

期望

标签: rggplot2

解决方案


我的建议是尝试使用factor. 也许这可能会有所帮助。

  essainoo <- subset(essai, essai$Phylum!="Other")
  neworder <- c(unique(essainoo$Phylum),"Other")
  
  essai$Phylum <- factor(essai$Phylum, levels=neworder)

推荐阅读