microbiome / miaViz

Microbiome Analysis Plotting and Visualization
https://microbiome.github.io/miaViz
Artistic License 2.0
10 stars 12 forks source link

Improve plotAbundance #156

Closed TuomasBorman closed 3 weeks ago

TuomasBorman commented 1 month ago

This PR implements the suggestions made in here: https://github.com/microbiome/miaViz/issues/132

The whole function is now restructured and improved. Even though the function was working, it was too complex to maintain I think. It was not easy to add the suggested things without refactoring the code. That is why the function is now mostly coded again, I think it is much simpler. Moreover, it has many additional capabilities that were not before which makes the function now rather flexible.

Main changes

  1. The code is now simpler. However, all old functionalities are still fully supported.
  2. User can choose manually the order of columns and rows.
  3. Facets are supported now also for columns
  4. Support for paired samples.

Because this has many new features and the Bioconductor release is coming soon, I think it is the wisest to keep this PR open and merge this to the next Bioconductor development version. I can also test this in one of my projects before merging.

Here are few examples on the usage


library(miaViz)
library(patchwork)
data(enterotype, package="mia")
tse <- enterotype
# Apply relative transformation
tse <- agglomerateByPrevalence(tse, prevalence = 0.98)
tse <- transformAssay(tse, method = "relabundance")
tse <- tse[ , complete.cases(colData(tse))]
# Plot with annotation bar
p <- plotAbundance(tse, col.var = "Gender", assay.type = "relabundance")
wrap_plots(p, ncol = 1, heights = c(0.95, 0.05))

image

# Plot with annotation bar and order
p <- plotAbundance(tse, col.var = "Gender", order.col.by = "Gender", assay.type = "relabundance")
wrap_plots(p, ncol = 1, heights = c(0.95, 0.05))

image

# Plot with facets for sample metadata variable
p <- plotAbundance(tse, col.var = "Gender", facet.cols = TRUE, scales = "free_x", assay.type = "relabundance")
p

image

# Plot with facets for multiple sample metadata variables (this requires an additional package that is not dependency)
p <- plotAbundance(tse, col.var = c("Nationality", "Gender"), facet.cols = TRUE, scales = "free_x", assay.type = "relabundance")
p

image

# Add own taxa order
order <- rownames(tse)
order <- c(order[!order%in%c("Bacteroides")], "Bacteroides")
p <- plotAbundance(tse, row.levels = order, assay.type = "relabundance")
p

image

# Prepare data
data("Tengeler2020")
tse <- Tengeler2020
tse <- agglomerateByPrevalence(tse, prevalence = 0.70)
tse <- tse[, 1:12]
colData(tse)[["patient"]] <- rep(paste0("patient", seq_len(4)), ncol(tse) / 4)
colData(tse)[["time_point"]] <- rep(paste0("time", seq_len(20)), each = 4)[1:12]

# Drop patient 1 from 1st time point, patient 2 from 2nd time point and patient 3 and 4 from time point 3
tse <- tse[ , c(2:5, 7:10)]
# Create a plot with paired data for comparison
p <- plotAbundance(
    tse, col.var = "time_point", order.col.by = "patient", as.relative = TRUE,
    paired = TRUE, facet.cols = TRUE, ncol = 1, scales = "free_x")
p

image

antagomir commented 1 month ago

very nice!