3
mapfile = "map_soil_final3.txt"
map = import_qiime_sample_data(mapfile)
print(map)

tree = read_tree("rep_set.tre")

biom = "otu_table_15000_json.biom"
biomfile = import_biom(biom,parseFunction=parse_taxonomy_default)

testdata = merge_phyloseq(biomfile,tree,map)
print(testdata)

p = plot_bar(testdata, "Order", fill = "Phylum", facet_grid = ~Description) +
             ylab("Percentage of Sequences") 
relative_ab = p + geom_bar(aes(color = Phylum, fill = Phylum),
                           stat = "identity", position = "stack") 
relative_ab

Sampleplot

What I have here are some plots of various taxa. Each one of the bars represents the Order (the names on the x axis) of an organism within a Phlyum (colors). Right now the Order is arranged in alphabetical order but this causes the Phlya to be all over the place. It would nice if I could group the Order together based on Phylum. So essentially all the colors would group together. Can someone assist me with this? Thanks!

https://www.dropbox.com/sh/5xn5si352bgslg0/AADyI_ON39_55qvNdvB167Lga?dl=0

> str(ent10)
Formal class 'phyloseq' [package "phyloseq"] with 5 slots
..@ otu_table:Formal class 'otu_table' [package "phyloseq"] with 2    slots
.. .. ..@ .Data        : num [1:20, 1:73] 0 11 86 237 11 8 16 4 15 19   ...
.. .. .. ..- attr(*, "dimnames")=List of 2
.. .. .. .. ..$ : chr [1:20] "4128270" "2473794" "811074" "4388819" ...
.. .. .. .. ..$ : chr [1:73] "AB17S" "UR6S" "AB4S" "AB8S" ...
.. .. ..@ taxa_are_rows: logi TRUE
..@ tax_table:Formal class 'taxonomyTable' [package "phyloseq"] with 1 slot
.. .. ..@ .Data: chr [1:20, 1:7] "k__Bacteria" "k__Bacteria"  "k__Bacteria" "k__Bacteria" ...
.. .. .. ..- attr(*, "dimnames")=List of 2
.. .. .. .. ..$ : chr [1:20] "4128270" "2473794" "811074" "4388819" ...
.. .. .. .. ..$ : chr [1:7] "Kingdom" "Phylum" "Class" "Order" ...
..@ sam_data :'data.frame': 73 obs. of  4 variables:
Formal class 'sample_data' [package "phyloseq"] with 4 slots
.. .. ..@ .Data    :List of 4
.. .. .. ..$ : Factor w/ 73 levels "AB10S","AB11S",..: 8 70 13 17 9 11   66 15 12 22 ...
.. .. .. ..$ : Factor w/ 73 levels "D1_pb_s.fasta",..: 67 45 34 50 70 26 29 42 30 14 ...
.. .. .. ..$ : Factor w/ 4 levels "Ash_Basins","Pond_B",..: 1 4 1 1 1 1 4 1 1 2 ...
.. .. .. ..$ : Factor w/ 31 levels "D1","D10","D11",..: 29 21 19 23 30 17 17 21 18 13 ...
.. .. ..@ names    : chr [1:4] "X.SampleID" "InputFileName" "Description" "TagCombo"
.. .. ..@ row.names: chr [1:73] "AB17S" "UR6S" "AB4S" "AB8S" ...
.. .. ..@ .S3Class : chr "data.frame"
..@ phy_tree :List of 5
.. ..$ edge       : int [1:38, 1:2] 21 22 23 23 22 24 25 25 24 21 ...
.. ..$ Nnode      : int 19
.. ..$ tip.label  : chr [1:20] "4128270" "2473794" "811074" "4388819" ...
.. ..$ edge.length: num [1:38] 0.00016 0.02274 0.3467 0.80367 0.00564 ...
.. ..$ node.label : chr [1:19] "1.000" "0.815" "0.922" "0.860" ...
.. ..- attr(*, "class")= chr "phylo"
.. ..- attr(*, "order")= chr "cladewise"
..@ refseq   : NULL
www
  • 38,575
  • 12
  • 48
  • 84
user3105519
  • 309
  • 4
  • 10
  • 1
    Please provide some sample data. – neilfws Apr 10 '17 at 05:09
  • I added the rest of the code and sample data @neilfws – user3105519 Apr 10 '17 at 15:07
  • You could reorder the level of the x axis factor by the fill factor. For example, using package *forcats* you could change the order of the levels of `Order` by the order of `Phylum` in `fct_reorder`: It would look something like `with(testdata, fct_reorder(Order, Phylum, sort))`. – aosmith Apr 10 '17 at 15:59
  • @aosmith I get Error: 'fun' must return a single number per group using with(testdata, fct_reorder("Order", "Phylum", sort)) – user3105519 Apr 10 '17 at 17:15
  • Try removing the quotes around the variable names. – aosmith Apr 10 '17 at 17:18
  • I just did. I think it has something to do with testdata being a phyloseq object. The current error is "Error in check_factor(f) : object 'Order' not found. When I run the command 'rank_names(testdata) it returns all the correct taxonomies: '[1] "Kingdom" "Phylum" "Class" "Order" "Family" "Species". Also thanks! @aosmith – user3105519 Apr 10 '17 at 17:37
  • Maybe. I don't know anything about phyloseq objects. The main take-home of my comment was that controlling the order of the factor levels controls the order of the graph. – aosmith Apr 10 '17 at 17:39
  • @aosmith does the additional information I just added help any? As you can see I am trying to coerce "Order" to group together based on "Phylum" it is in. When I checked the class of the object it said it was a character and not a factor. – user3105519 Apr 11 '17 at 20:43

1 Answers1

4

Adding an additional geom_bar layer won't help you. In fact, you don't even need additional ggplot2 code to achieve the organization that you want, because ggplot2 interprets factor levels to decide this, and you can modify these with base R commands. The following is a completely reproducible example, including some extra pretty-label theme-ing at the end, the only place where ggplot2-specific commands are needed. Each of the p or p + ... lines are simply callouts to render the intermediate plot for result, and you could skip these in practice if what you want is just the graphic at the end.

# Preliminaries
rm(list = ls())
library("phyloseq"); packageVersion("phyloseq")
data("GlobalPatterns")
N = 100L
gpN = prune_taxa(names(sort(taxa_sums(GlobalPatterns), decreasing = TRUE)[1:N]), GlobalPatterns)
# Define the initial plot
p = plot_bar(gpN, fill = "Phylum", x = "Order")
p
# Adjust the factor levels. No ggplot2 commands needed. Already what you want.
p$data$Order <- as.character(p$data$Order)
p$data$Order <- factor(x = p$data$Order, 
                       levels = unique(p$data$Order[order(as.character(p$data$Phylum))]))
p
# pretty the axis labels with ggplot2
library("ggplot2")
p + theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust=1))
Paul 'Joey' McMurdie
  • 7,295
  • 5
  • 37
  • 41