简体   繁体   中英

Stack bar generated by Phyloseq

I am using this R package called "phyloseq" to analyze the bioinformatic data.

otumat = matrix(sample(1:100, 100, replace = TRUE), nrow = 10, ncol = 10)
otumat

rownames(otumat) <- paste0("OTU", 1:nrow(otumat))
colnames(otumat) <- paste0("Sample", 1:ncol(otumat))
otumat

taxmat = matrix(sample(letters, 70, replace = TRUE), nrow = nrow(otumat), ncol = 7)
rownames(taxmat) <- rownames(otumat)
colnames(taxmat) <- c("Domain", "Phylum", "Class", "Order", "Family", "Genus", 
                      "Species")
taxmat

library("phyloseq")
OTU = otu_table(otumat, taxa_are_rows = TRUE)
TAX = tax_table(taxmat)
OTU
TAX

physeq = phyloseq(OTU, TAX)
physeq

plot_bar(physeq, fill = "Family")

So the bar graph generated do not stack the same Family together. For example, there are two separate "I" blocks in sample 10. I know phyloseq plot graph using ggplot2. Does any one know what ggplot2 associated codes I can add to the lot_bar(physeq, fill = "Family") to stack the same family together in the bar graph?

在此处输入图片说明

You need to reorder the levels of the factor being used for the x-axis. physeq presumably has a column called "Sample" (don't have the relevant package installed), you need to reorder the levels in this.

It should be possible to use a command like this

physeq$Sample <- factor(physeq$Sample, levels = paste0("Sample", 1:10))

Then it should plot correctly.

You might need to dig to find the relevant part to change

Actually, with respect, the plot_bar function does already do what you're asking:

# preliminaries
rm(list = ls())
library("phyloseq"); packageVersion("phyloseq")
data("GlobalPatterns")
gp.ch = subset_taxa(GlobalPatterns, Phylum == "Chlamydiae")
# the function call that does what you're asking for
plot_bar(gp.ch, fill = "Family")

See the following help tutorial for more details, examples:

https://joey711.github.io/phyloseq/plot_bar-examples.html

You can also specify the x-axis grouping as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM