Closed laurendaddi closed 2 years 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)
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
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