tax_sort: Sort taxa in phyloseq otu_table and tax_table

View source: R/tax_sort.R

tax_sortR Documentation

Sort taxa in phyloseq otu_table and tax_table

Description

Multiple ways of sorting taxa are possible and determined by the by argument. The by argument must be one of:

  • 'rev' to reverse the current order

  • 'name' (sort alphabetically by at)

  • 'asis' to keep current order as is

  • a sample name (descending abundance sorting within that sample)

  • summary stat. function e.g. sum or mean

The at argument must be "names" for sorting unique taxa, or a rank name, for sorting at that rank. at is ignored when by is "rev".

Usage

tax_sort(
  data,
  by = "name",
  at = "names",
  ...,
  tree_warn = TRUE,
  verbose = TRUE,
  trans = "identity",
  use_counts = TRUE,
  counts_warn = TRUE
)

Arguments

data

psExtra or phyloseq

by

how to sort, see description

at

"names" or a taxonomic rank to apply sorting method to, as specified in by.

...

used if summary function given, or pass undetected arg for tax_transform("binary") if by = "prev" or "prevalence"

tree_warn

If phylogenetic tree is present in phyloseq phy_tree slot, taxa cannot be reordered. Default behaviour of tax_sort is to remove the phylogenetic tree and warn about this. tree_warn = FALSE will suppress the warning message, but still remove the tree!

verbose

passed to phyloseq_validate verbose (if TRUE: message about suspicious values in tax_table, and how to fix)

trans

name of transformation to apply to taxa before sorting (taxa are returned un-transformed)

use_counts

use count data if available, instead of transformed data

counts_warn

warn if count data are not available?

Details

Don't forget to pass na.rm = TRUE to ... if using a summary stat function in by

Value

sorted phyloseq or psExtra

Examples

library(phyloseq)
data("dietswap", package = "microbiome")
dietswap

# reverse current order
dietswap %>%
  tax_sort("rev") %>%
  tax_table() %>%
  head(30)

# sort alphabetically by a taxonomic rank (or "names" for taxa_names)
dietswap %>%
  tax_sort(by = "name", at = "Phylum") %>%
  tax_table() %>%
  head(30)

# sequentially sorting by higher ranks
# sets tax_table in nested alphabetical order
dietswap %>%
  tax_sort(at = "names") %>%
  tax_sort(at = "Genus") %>%
  tax_sort(at = "Family") %>%
  tax_sort(at = "Phylum") %>%
  tax_table() %>%
  head(30)

# sort by function e.g. total sum or median abundance
dietswap %>%
  tax_sort(by = sum) %>%
  taxa_names() %>%
  head(20)

# transform to compositional data (proportions) before sorting
# note that abundances are returned untransformed
dietswap %>%
  tax_sort(by = sum, trans = "compositional") %>%
  taxa_names() %>%
  head(20)

# order by descending abundance in a single named sample
dietswap %>%
  tax_sort(by = "Sample-1") %>%
  otu_table() %>%
  .[1:8, 1:4]


# sum order should always equal mean order if non-negative abundances
# don't forget to add na.rm = TRUE if you expect NAs in otu_table somehow
dietswap %>%
  tax_sort(by = sum, na.rm = TRUE) %>%
  taxa_names() %>%
  head(20)

# if your phyloseq object has a phylogenetic tree,
# tax_sort will remove the tree, and warn you about this
# unless you disable that warning with tree_warn = FALSE

# You can sort by abundance at higher taxonomic ranks,
# without losing lower rank info
# e.g. sort (descending) by phyla abundances
dietswap %>%
  tax_sort(by = sum, at = "Phylum") %>%
  tax_table() %>%
  head()

# You can sort by ascending abundance (or prevalence etc) by reversing after
dietswap %>%
  tax_sort(by = "prev", at = "Phylum") %>%
  tax_sort(by = "rev") %>%
  tax_table() %>%
  head()

david-barnett/microViz documentation built on April 17, 2025, 4:25 a.m.