Skip to content

Commit

Permalink
fix: fixed erroneous taxon ordering that occurred if comp_barplot tax…
Browse files Browse the repository at this point in the history
…_level is set to "Taxon" - as occurred in the tax_rename documentation!

https://david-barnett.github.io/microViz/reference/tax_rename.html
  • Loading branch information
david-barnett committed May 16, 2023
1 parent 23a30ec commit 2a021f2
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 24 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: microViz
Title: Microbiome Data Analysis and Visualization
Version: 0.10.9
Version: 0.10.10
Authors@R:
person(given = "David",
family = "Barnett",
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# microViz 0.10.10

- Fix: `comp_barplot` taxon ordering is now correct when tax_level is set to "Taxon"

# microViz 0.10.9

- Fix: Small internal fix to avoid renderggiraph deprecation warning in ord_explore.
Expand Down
7 changes: 5 additions & 2 deletions R/comp_barplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ comp_barplot <- function(ps,
x = "SAMPLE",
...) {
ps <- ps_get(ps)
if (identical(tax_level, ".Taxon")) {
stop("'.Taxon' cannot be used as a rank name! You must rename that rank.")
}
if (!rlang::is_scalar_integerish(n_taxa) || n_taxa < 1) {
stop("n_taxa must a positive integer")
}
Expand Down Expand Up @@ -378,7 +381,7 @@ comp_barplotFixed <- function(ps, interactive,
df <- ps_melt(ps)

# set fixed order of stacked taxa bars by creating ordered factor
df[["Taxon"]] <- factor(df[["unique"]], levels = rev(uniqueTaxaOrdered))
df[[".Taxon"]] <- factor(df[["unique"]], levels = rev(uniqueTaxaOrdered))

# # set fixed order of fill colours (for LEGEND ordering!)
df[[tax_level]] <- factor(df[[".top"]], levels = topTaxaOrdered)
Expand All @@ -390,7 +393,7 @@ comp_barplotFixed <- function(ps, interactive,
# build plot
p <- ggplot2::ggplot(data = df, mapping = ggplot2::aes(
x = .data[[x]], y = .data[["Abundance"]],
fill = .data[[tax_level]], group = .data[["Taxon"]]
fill = .data[[tax_level]], group = .data[[".Taxon"]]
))
if (identical(x, "SAMPLE")) p <- p + ggplot2::xlab(NULL)

Expand Down
2 changes: 1 addition & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ install.packages("devtools")
devtools::install_github("david-barnett/microViz")

# To install a specific "release" version of this package, e.g. an old version
devtools::install_github("david-barnett/[email protected].8")
devtools::install_github("david-barnett/[email protected].10")
```

### Installation notes
Expand Down
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ install.packages("devtools")
devtools::install_github("david-barnett/microViz")

# To install a specific "release" version of this package, e.g. an old version
devtools::install_github("david-barnett/[email protected].8")
devtools::install_github("david-barnett/[email protected].10")
```

### Installation notes
Expand All @@ -159,7 +159,7 @@ formally test this.

``` r
library(microViz)
#> microViz version 0.10.9 - Copyright (C) 2023 David Barnett
#> microViz version 0.10.10 - Copyright (C) 2023 David Barnett
#> ! Website: https://david-barnett.github.io/microViz
#> ✔ Useful? For citation details, run: `citation("microViz")`
#> ✖ Silence? `suppressPackageStartupMessages(library(microViz))`
Expand Down Expand Up @@ -331,8 +331,8 @@ aitchison_perm <- aitchison_dists %>%
n_processes = 1, n_perms = 99, # you should use at least 999!
variables = "bmi_group"
)
#> 2023-05-15 21:22:34.548391 - Starting PERMANOVA with 99 perms with 1 processes
#> 2023-05-15 21:22:34.605894 - Finished PERMANOVA
#> 2023-05-16 14:37:53.680031 - Starting PERMANOVA with 99 perms with 1 processes
#> 2023-05-16 14:37:53.786058 - Finished PERMANOVA

# view the permanova results
perm_get(aitchison_perm) %>% as.data.frame()
Expand All @@ -357,8 +357,8 @@ your permanova directly using the `ord_plot` function with constraints
perm2 <- aitchison_dists %>%
dist_permanova(variables = c("weight", "african", "sex"), seed = 321)
#> Dropping samples with missings: 2
#> 2023-05-15 21:22:34.623176 - Starting PERMANOVA with 999 perms with 1 processes
#> 2023-05-15 21:22:36.280034 - Finished PERMANOVA
#> 2023-05-16 14:37:53.808143 - Starting PERMANOVA with 999 perms with 1 processes
#> 2023-05-16 14:37:56.526257 - Finished PERMANOVA
```

We’ll visualise the effect of nationality and bodyweight on sample
Expand Down Expand Up @@ -451,12 +451,12 @@ and by participating in this project you agree to abide by its terms.
``` r
sessionInfo()
#> R version 4.3.0 (2023-04-21)
#> Platform: aarch64-apple-darwin20 (64-bit)
#> Platform: x86_64-apple-darwin20 (64-bit)
#> Running under: macOS Ventura 13.3.1
#>
#> Matrix products: default
#> BLAS: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRblas.0.dylib
#> LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRlapack.dylib; LAPACK version 3.11.0
#> BLAS: /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/lib/libRblas.0.dylib
#> LAPACK: /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/lib/libRlapack.dylib; LAPACK version 3.11.0
#>
#> locale:
#> [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
Expand All @@ -468,8 +468,8 @@ sessionInfo()
#> [1] stats graphics grDevices utils datasets methods base
#>
#> other attached packages:
#> [1] ggplot2_3.4.2 dplyr_1.1.2 phyloseq_1.44.0 microViz_0.10.9
#> [5] testthat_3.1.8 devtools_2.4.5 usethis_2.1.6
#> [1] ggplot2_3.4.2 dplyr_1.1.2 phyloseq_1.44.0 microViz_0.10.10
#> [5] testthat_3.1.8 devtools_2.4.5 usethis_2.1.6
#>
#> loaded via a namespace (and not attached):
#> [1] RColorBrewer_1.1-3 shape_1.4.6 rstudioapi_0.14
Expand All @@ -486,13 +486,13 @@ sessionInfo()
#> [34] shiny_1.7.4 digest_0.6.31 selectr_0.4-2
#> [37] colorspace_2.1-0 S4Vectors_0.38.1 ps_1.7.5
#> [40] pkgload_1.3.2 seriation_1.4.2 vegan_2.6-4
#> [43] labeling_0.4.2 fansi_1.0.4 httr_1.4.5
#> [43] labeling_0.4.2 fansi_1.0.4 httr_1.4.6
#> [46] mgcv_1.8-42 compiler_4.3.0 remotes_2.4.2
#> [49] doParallel_1.0.17 withr_2.5.0 viridis_0.6.3
#> [52] pkgbuild_1.4.0 highr_0.10 MASS_7.3-60
#> [55] sessioninfo_1.2.2 rjson_0.2.21 biomformat_1.28.0
#> [58] permute_0.9-7 tools_4.3.0 ape_5.7-1
#> [61] httpuv_1.6.9 glue_1.6.2 callr_3.7.3
#> [61] httpuv_1.6.11 glue_1.6.2 callr_3.7.3
#> [64] nlme_3.1-162 rhdf5filters_1.12.1 promises_1.2.0.1
#> [67] gridtext_0.1.5 grid_4.3.0 Rtsne_0.16
#> [70] cluster_2.1.4 reshape2_1.4.4 ade4_1.7-22
Expand Down
18 changes: 11 additions & 7 deletions tests/testthat/test-comp_barplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ test_that("comp_barplot throws correct errors", {
object = comp_barplot(dietswap, tax_level = "geenus"),
regexp = "must be the name of a valid rank"
)
expect_error(
object = comp_barplot(dietswap, tax_level = ".Taxon"),
regexp = "'.Taxon' cannot be used as a rank name! You must rename that rank."
)
expect_error(
object = comp_barplot(dietswap, tax_level = "Genus", n_taxa = 1:15),
regexp = "n_taxa must a positive integer"
Expand All @@ -28,14 +32,14 @@ test_that("fixing tax order of comp_barplot works", {
comp_barplot(tax_level = "Phylum", n_taxa = 8, tax_order = fixed_order) +
ggplot2::coord_flip()

expect_equal(levels(p8$data$Taxon), rev(fixed_order))
expect_equal(levels(p8$data$.Taxon), rev(fixed_order))

p7 <- dietswap %>%
ps_filter(timepoint == 1) %>%
comp_barplot(tax_level = "Phylum", n_taxa = 7, tax_order = fixed_order) +
ggplot2::coord_flip()

expect_equal(levels(p7$data$Taxon), rev(c(fixed_order[1:7], "Other")))
expect_equal(levels(p7$data$.Taxon), rev(c(fixed_order[1:7], "Other")))
})

# manual sample ordering -----------------------------------------------------
Expand Down Expand Up @@ -72,7 +76,7 @@ test_that("comp_barplot doesn't change", {

expect_snapshot_csv(
name = "unique_taxa_order",
object = levels(p$data$Taxon)
object = levels(p$data$.Taxon)
)
expect_snapshot_csv(
name = "SAMPLE_order",
Expand All @@ -99,15 +103,15 @@ test_that("comp_barplot taxa ordering at level of 'unique' works", {
tax_level = "unique", n_taxa = 8, sample_order = "asis", tax_order = sum,
merge_other = FALSE
)
expect_snapshot_csv(levels(p$data$Taxon), name = "uniqueTaxonLevelsUnmerged")
expect_snapshot_csv(levels(p$data$.Taxon), name = "uniqueTaxonLevelsUnmerged")

p2 <- dietswap %>%
ps_filter(timepoint == 1) %>%
comp_barplot(
tax_level = "unique", n_taxa = 3, sample_order = "asis", tax_order = sum,
merge_other = TRUE
)
expect_equal(object = levels(p2$data$Taxon), expected = c(
expect_equal(object = levels(p2$data$.Taxon), expected = c(
"Other", "Oscillospira guillermondii et rel.",
"Bacteroides vulgatus et rel.", "Prevotella melaninogenica et rel."
))
Expand All @@ -131,7 +135,7 @@ test_that("list of plots generated by group_by works", {
for (p in names(plots)) {
expect_snapshot_csv(
name = paste0("unique_taxa_order-", p),
object = levels(plots[[p]]$data$Taxon)
object = levels(plots[[p]]$data$.Taxon)
)
expect_snapshot_csv(
name = paste0("SAMPLE_order-", p),
Expand All @@ -151,7 +155,7 @@ test_that("list of plots generated by group_by works", {
object = colnames(plots[[p]]$data),
expected = c(
"OTU", "Sample", "Abundance", "SAMPLE", "bmi_group", "Phylum",
"Family", "Genus", "unique", "Taxon"
"Family", "Genus", "unique", ".Taxon"
)
)
}
Expand Down

0 comments on commit 2a021f2

Please sign in to comment.