joey711 / phyloseq

phyloseq is a set of classes, wrappers, and tools (in R) to make it easier to import, store, and analyze phylogenetic sequencing data; and to reproducibly share that data and analysis with others. See the phyloseq front page:
http://joey711.github.io/phyloseq/
567 stars 187 forks source link

Creating a relative abundance bar plot by body site #1741

Open bark9299 opened 2 months ago

bark9299 commented 2 months ago

I am trying plot an abundance bar graph where all "body sites are 100% like qiime2 shows. My sample data looks like this: Capture

When i use the following code:

`#phyloseq object
physeq<-qza_to_phyloseq(
  features="table-no-mitochondria-chloroplast-unassigned.qza",
  tree="rooted-tree.qza",
  taxonomy="taxonomy_gg_2022.qza",
  metadata = "metadata.tsv"
)

#remove blank samples
to_remove <- c("sample_3BL-nfw", "sample_Neg_CTRL_1", "sample_Neg_CTRL_2", "sample_Zymo_Pos_CTRL_1" ,"sample_Zymo_Pos_CTRL_2", "sample_Zymo_Pos_CTRL_3", "sample_5BL-nfw", "sample_1BL-EB", "sample_4BL-nfw", "sample_2BL-EB")
physeq_no_blanks <- prune_samples(!(sample_names(physeq) %in% to_remove), physeq)
sample_names(physeq_no_blanks)
otu_table(physeq_no_blanks, taxa_are_rows = TRUE)

#transform relative abundance at phylum rank
phy_norm <- transform_sample_counts(physeq_no_blanks, function(x) x/sum(x))

glom <- tax_glom(phy_norm, taxrank = 'Phylum')
tax_table(glom)

W = plot_bar(glom, x="Body_site", y="Abundance", fill="Phylum") +
  geom_bar(aes(color=Phylum, fill=Phylum), stat="identity", position="stack")
W

It gives me bar_trial_1 1 And my abundances are not all reaching the top of the graph like it is supposed too. I also tried:

glom_smelt <- psmelt(glom)

p <- ggplot(glom_smelt, aes(x = "Body_site", y = Abundance, fill = Phylum)) +
  theme_bw() +
  geom_bar(stat = "identity")
p

And it displays the following graph: stupid I have tried so many different things and cant figure out how to make it like this: Picture1 However when i replace x="Body_site" with x=Sample it will create the correct abundance distribution but with all the sample names at the bottom which is too crowded. Rplot01

I appreciate any help.