david-barnett / microViz

R package for microbiome data visualization and statistics. Uses phyloseq, vegan and the tidyverse. Docker image available.
https://david-barnett.github.io/microViz/
GNU General Public License v3.0
94 stars 10 forks source link

labeling issue with comp_barplot() and facet_grid() #58

Closed laurendaddi closed 1 year ago

laurendaddi commented 2 years ago

I am having trouble getting proper labeling of my x axis when making a bar plot. I am faceting by timepoint, and since I have some subjects who do not have samples for every timepoint, I want there to be a blank space if there is a missing sample, so that you can still read across the faceted plots and easily follow each subject over time.

It seems to me like facet_grid() is the only function that will get me what I need in terms of having blank spaces for empty samples. (facet_wrap(), facet_nested(), and the built in "facet_by=" in comp_barplot() did not work).

However, if I use facet_grid() and specify aes(x=Subject), the subject numbers are not appearing on the axis. If I comment out the aes() and instead use "label=" in comp_barplot(), this results in a hugely messy graph because it repeats each subject on the axis.

Not sure I explained that well. Here is my code, and an example of the plot using aes() specifications and using "label=" specifications.

genusplot <- phygenus_plot %>% comp_barplot( tax_level = "Genus", n_taxa = 24, label = "Subject", sample_order = samples ) + geom_bar(stat = "identity")+ coord_flip() + facet_grid(~Timepoint, scales = "free") +

aes(x = Subject) +

guides(fill = guide_legend(reverse = TRUE, keywidth = 0.4, keyheight = 0.4, nrow=5)) + labs(y = "Relative Abundance", fill = "Genus") + theme_classic() + theme(plot.title = element_text(hjust=0.5, size = 15), axis.text.x = element_text(size = 9, hjust = 0.5, colour=1), axis.text.y = element_text(size = 9, colour=1), axis.title.x = element_text(size=13), axis.title.y = element_text(size=13), legend.text = element_text(size = 8), legend.key.size = unit(0.4, "cm"), legend.key.width = unit(0.4,"cm"), legend.position = "bottom", strip.text.x = element_text(size=10), strip.text.y = element_text(size=10), panel.border = element_rect(colour = "black", fill=NA), axis.text = element_text(colour = 1, size = 9) ) genusplot

image image

david-barnett commented 1 year ago

Hi, sorry for slow reply:

This is a bit of a hacky workaround for now, you were almost there

Code below using some example data:

The last trick was to set the x axis labelling function again, as this is modified within comp_barplot and needs to be respecified again after the changing the x aesthetic mapping to subject

# create a phyloseq with multiple samples per participant, with some missing samples
library(microViz)
library(ggplot2)
data("dietswap", package = "microbiome")

psq <- dietswap %>% 
  ps_filter(!sample %in% paste0("Sample-", seq(1, 100, by = 2))) %>%
  ps_filter(timepoint %in% 1:2) 

psq %>% 
  ps_arrange(subject) %>% 
  comp_barplot(tax_level = "Genus", 
               label = "subject",
               sample_order = "asis") + 
  facet_wrap(facets = vars(timepoint), scales = "fixed") +
  coord_flip() +
  aes(x = subject) +
  scale_x_discrete(label = identity, limits = rev)

So this is the missing line (the label arg is critical, and the limits arg is just set to flip the subjects' order)

scale_x_discrete(label = identity, limits = rev)

image

david-barnett commented 1 year ago

Properly resolved since 0.9.7 e.g.

# create a phyloseq with multiple samples per participant, with some missing samples
library(microViz)
#> 
#> microViz version 0.9.7 - Copyright (C) 2022 David Barnett
#> * Website: https://david-barnett.github.io/microViz/
#> * Useful? For citation info, run: citation('microViz')
#> * Silence: suppressPackageStartupMessages(library(microViz))
library(ggplot2)
data("dietswap", package = "microbiome")

psq <- dietswap %>% 
  ps_filter(timepoint %in% 1:2) %>% 
  ps_filter(dplyr::row_number() %in% sample(1:100, size = 50)) # drop random samples for example

psq %>% 
  ps_arrange(desc(subject)) %>% 
  ps_mutate(subject = factor(subject, levels = unique(subject))) %>% # fix order
  comp_barplot(
    tax_level = "Genus", sample_order = "asis",
    x = "subject" # this is the new feature since 0.9.7
  ) + 
  facet_wrap(facets = vars(timepoint), scales = "fixed") +
  coord_flip()

Created on 2022-10-25 with reprex v2.0.2

Session info ``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.2.1 (2022-06-23) #> os macOS Big Sur ... 10.16 #> system x86_64, darwin17.0 #> ui X11 #> language (EN) #> collate en_GB.UTF-8 #> ctype en_GB.UTF-8 #> tz Europe/Amsterdam #> date 2022-10-25 #> pandoc 2.18 @ /Applications/RStudio.app/Contents/MacOS/quarto/bin/tools/ (via rmarkdown) #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date (UTC) lib source #> ade4 1.7-19 2022-04-19 [1] RSPM (R 4.2.0) #> ape 5.6-2 2022-03-02 [1] RSPM (R 4.2.0) #> assertthat 0.2.1 2019-03-21 [1] CRAN (R 4.2.0) #> Biobase 2.56.0 2022-04-26 [1] Bioconductor #> BiocGenerics 0.42.0 2022-04-26 [1] Bioconductor #> biomformat 1.24.0 2022-04-26 [1] Bioconductor #> Biostrings 2.64.1 2022-08-18 [1] Bioconductor #> bitops 1.0-7 2021-04-24 [1] CRAN (R 4.2.0) #> cli 3.4.1 2022-09-23 [1] CRAN (R 4.2.0) #> cluster 2.1.3 2022-03-28 [1] CRAN (R 4.2.1) #> codetools 0.2-18 2020-11-04 [1] CRAN (R 4.2.1) #> colorspace 2.0-3 2022-02-21 [1] CRAN (R 4.2.0) #> crayon 1.5.2 2022-09-29 [1] CRAN (R 4.2.0) #> curl 4.3.2 2021-06-23 [1] RSPM (R 4.2.0) #> data.table 1.14.4 2022-10-17 [1] CRAN (R 4.2.0) #> DBI 1.1.3 2022-06-18 [1] CRAN (R 4.2.0) #> digest 0.6.30 2022-10-18 [1] CRAN (R 4.2.1) #> dplyr 1.0.10 2022-09-01 [1] CRAN (R 4.2.0) #> ellipsis 0.3.2 2021-04-29 [1] CRAN (R 4.2.0) #> evaluate 0.15 2022-02-18 [1] RSPM (R 4.2.0) #> fansi 1.0.3 2022-03-24 [1] CRAN (R 4.2.0) #> farver 2.1.1 2022-07-06 [1] CRAN (R 4.2.0) #> fastmap 1.1.0 2021-01-25 [1] RSPM (R 4.2.0) #> foreach 1.5.2 2022-02-02 [1] RSPM (R 4.2.0) #> fs 1.5.2 2021-12-08 [1] RSPM (R 4.2.0) #> generics 0.1.3 2022-07-05 [1] CRAN (R 4.2.0) #> GenomeInfoDb 1.32.4 2022-09-06 [1] Bioconductor #> GenomeInfoDbData 1.2.8 2022-05-12 [1] Bioconductor #> ggplot2 * 3.3.6 2022-05-03 [1] CRAN (R 4.2.0) #> glue 1.6.2 2022-02-24 [1] CRAN (R 4.2.0) #> gtable 0.3.1 2022-09-01 [1] CRAN (R 4.2.0) #> highr 0.9 2021-04-16 [1] RSPM (R 4.2.0) #> htmltools 0.5.3 2022-07-18 [1] CRAN (R 4.2.0) #> httr 1.4.3 2022-05-04 [1] RSPM (R 4.2.0) #> igraph 1.3.5 2022-09-22 [1] CRAN (R 4.2.0) #> IRanges 2.30.1 2022-08-18 [1] Bioconductor #> iterators 1.0.14 2022-02-05 [1] RSPM (R 4.2.0) #> jsonlite 1.8.3 2022-10-21 [1] CRAN (R 4.2.0) #> knitr 1.39 2022-04-26 [1] RSPM (R 4.2.0) #> labeling 0.4.2 2020-10-20 [1] CRAN (R 4.2.0) #> lattice 0.20-45 2021-09-22 [1] CRAN (R 4.2.1) #> lifecycle 1.0.3 2022-10-07 [1] CRAN (R 4.2.0) #> magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.2.0) #> MASS 7.3-57 2022-04-22 [1] CRAN (R 4.2.1) #> Matrix 1.4-1 2022-03-23 [1] CRAN (R 4.2.1) #> mgcv 1.8-40 2022-03-29 [1] CRAN (R 4.2.1) #> microbiome 1.18.0 2022-04-26 [1] Bioconductor #> microViz * 0.9.7 2022-10-25 [1] Github (david-barnett/microViz@0bbf609) #> mime 0.12 2021-09-28 [1] RSPM (R 4.2.0) #> multtest 2.52.0 2022-04-26 [1] Bioconductor #> munsell 0.5.0 2018-06-12 [1] CRAN (R 4.2.0) #> nlme 3.1-157 2022-03-25 [1] CRAN (R 4.2.1) #> permute 0.9-7 2022-01-27 [1] RSPM (R 4.2.0) #> phyloseq 1.40.0 2022-04-26 [1] Bioconductor #> pillar 1.8.1 2022-08-19 [1] CRAN (R 4.2.0) #> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.2.0) #> plyr 1.8.7 2022-03-24 [1] CRAN (R 4.2.0) #> purrr 0.3.5 2022-10-06 [1] CRAN (R 4.2.0) #> R.cache 0.15.0 2021-04-30 [1] CRAN (R 4.2.0) #> R.methodsS3 1.8.1 2020-08-26 [1] CRAN (R 4.2.0) #> R.oo 1.24.0 2020-08-26 [1] CRAN (R 4.2.0) #> R.utils 2.11.0 2021-09-26 [1] CRAN (R 4.2.0) #> R6 2.5.1 2021-08-19 [1] CRAN (R 4.2.0) #> Rcpp 1.0.9 2022-07-08 [1] CRAN (R 4.2.0) #> RCurl 1.98-1.9 2022-10-03 [1] CRAN (R 4.2.0) #> reprex 2.0.2 2022-08-17 [1] CRAN (R 4.2.0) #> reshape2 1.4.4 2020-04-09 [1] CRAN (R 4.2.0) #> rhdf5 2.40.0 2022-04-26 [1] Bioconductor #> rhdf5filters 1.8.0 2022-04-26 [1] Bioconductor #> Rhdf5lib 1.18.2 2022-05-17 [1] Bioconductor #> rlang 1.0.6 2022-09-24 [1] CRAN (R 4.2.0) #> rmarkdown 2.14 2022-04-25 [1] CRAN (R 4.2.0) #> rstudioapi 0.13 2020-11-12 [1] RSPM (R 4.2.0) #> Rtsne 0.16 2022-04-17 [1] RSPM (R 4.2.0) #> S4Vectors 0.34.0 2022-04-26 [1] Bioconductor #> scales 1.2.1 2022-08-20 [1] CRAN (R 4.2.0) #> sessioninfo 1.2.2 2021-12-06 [1] RSPM (R 4.2.0) #> stringi 1.7.8 2022-07-11 [1] CRAN (R 4.2.0) #> stringr 1.4.1 2022-08-20 [1] CRAN (R 4.2.0) #> styler 1.7.0 2022-03-13 [1] CRAN (R 4.2.0) #> survival 3.3-1 2022-03-03 [1] CRAN (R 4.2.1) #> tibble 3.1.8 2022-07-22 [1] CRAN (R 4.2.0) #> tidyr 1.2.1 2022-09-08 [1] CRAN (R 4.2.0) #> tidyselect 1.2.0 2022-10-10 [1] CRAN (R 4.2.0) #> utf8 1.2.2 2021-07-24 [1] CRAN (R 4.2.0) #> vctrs 0.5.0 2022-10-22 [1] CRAN (R 4.2.0) #> vegan 2.6-4 2022-10-11 [1] CRAN (R 4.2.0) #> withr 2.5.0 2022-03-03 [1] CRAN (R 4.2.0) #> xfun 0.31 2022-05-10 [1] RSPM (R 4.2.0) #> xml2 1.3.3 2021-11-30 [1] RSPM (R 4.2.0) #> XVector 0.36.0 2022-04-26 [1] Bioconductor #> yaml 2.3.6 2022-10-18 [1] CRAN (R 4.2.1) #> zlibbioc 1.42.0 2022-04-26 [1] Bioconductor #> #> [1] /Library/Frameworks/R.framework/Versions/4.2/Resources/library #> #> ────────────────────────────────────────────────────────────────────────────── ```