Comments (7)
Hi Chris, glad you are finding microViz useful! 😃
For any straightforward solution I could think of, you'll first need to put all your samples into one phyloseq (if not already done).
In some cases, you could use the group_by argument, which will make a list of plots, keeping the ordering and colours the same. With this you can also still use facets to group bars within each plot (seems like your plots have facets currently).
library(microViz)
library(ggplot2)
data(dietswap, package = "microbiome")
dietswap %>%
ps_filter(timepoint == 1, sex == "male") %>%
comp_barplot(
tax_level = "Family", n_taxa = 15,
bar_outline_colour = NA, bar_width = 0.7, label = NULL,
group_by = "nationality", facet_by = "bmi_group"
)
But if you want to separate the ordering of the taxa from the palette assignment itself and/or have different legend levels per community, I thought that would be a little trickier, but it turns out it's not too bad.
I've defined a couple of functions below that I'll probably add to microViz soonish.
# this makes a named palette vector from your combined dataset (considering overall abundance to assign colours)
tax_palette <- function(data, # phyloseq or ps_extra
rank, # e.g. "Genus"
n, # n colours / taxa not including other
by = sum, # method for tax_sort
pal = "brewerPlus", # palette name from distinct_palette
add = c(other = "lightgrey"), # name = value pairs appended to end of output
... # other args passed to tax_sort
) {
taxa <- tax_top(data = data, rank = rank, n = n, by = by, ...)
taxColours <- distinct_palette(n = n, pal = pal, add = NA)
names(taxColours) <- taxa
taxColours <- c(taxColours, add)
return(taxColours)
}
# palette viewer function (unnecessary but maybe handy)
tax_palette_plot <- function(
named_pal_vec # named vector of colours
) {
stopifnot(!anyNA(names(named_pal_vec))) # all colours need names
p <-
named_pal_vec %>%
as.data.frame() %>%
tibble::rownames_to_column("taxon") %>%
dplyr::mutate(taxon = factor(taxon, levels = rev(taxon))) %>%
dplyr::rename("hex" = ".") %>%
ggplot2::ggplot(ggplot2::aes(y = taxon, fill = hex, x = "")) +
ggplot2::geom_raster() +
ggplot2::scale_fill_identity() +
ggplot2::labs(x = NULL, y = NULL) +
ggplot2::theme_minimal()
return(p)
}
myPal <- tax_palette(dietswap, rank = "Genus", pal = "brewerPlus", n = 40)
myPal %>% tax_palette_plot() # just to check the palette
dietswap %>%
ps_filter(nationality == "AFR", timepoint == 1, sex == "male") %>% # small subset of data
comp_barplot(
tax_level = "Genus", n_taxa = 15,
bar_outline_colour = NA, bar_width = 0.7,
palette = myPal, label = NULL
)
dietswap %>%
ps_filter(nationality != "AFR", timepoint == 1, sex == "male") %>% # different subset of data
comp_barplot(
tax_level = "Genus", n_taxa = 15,
bar_outline_colour = NA, bar_width = 0.7,
palette = myPal, label = NULL
)
from microviz.
included since 0.9.1
https://david-barnett.github.io/microViz/news/index.html#microviz-091
from microviz.
closed in favour of project note
from microviz.
Hi @david-barnett ,
First off, I love the microViz package so thanks a ton for sharing with the community!
I'm posting on this closed issue because I think it might be exactly the problem I am having. Please let me know if you'd prefer this to be its own issue and I'd be happy to create one.
I have two datasets that I'm processing the same way but in different R projects. The end goal is to compare the bar plots between the two datasets. The issue is that taxonomy is called separately and they have slight differences when it comes to the most abundant taxa, so when using comp_barplot
with the same n value I get different colors for the same taxa across the datasets.
The last thing I want to do is have to modify the colors manually in Inkscape, so I've tried a number of different ways around this like creating a custom palette that I can use with both, but the results are ugly...to say the least (because the palette has to be large enough to capture all the ASVs)! I've accomplished this so far purely by replacing my newly made palette in place of distinct_palette(n=15, "brewerPlus")
in the palette
flag.
If there is an easier way to do this to keep the aesthetic of your brewerPlus
palette that would be ideal. Any help would be greatly appreciated. Thanks in advance!
from microviz.
@david-barnett This is great, thanks so much for the info. My apologies for the delay in response. I'm trying your workaround now and will report back how it works. Thanks again!
from microviz.
included since 0.9.1 https://david-barnett.github.io/microViz/news/index.html#microviz-091
@david-barnett my apologies for never getting back to you on how this worked for me. It was a resounding success and I want to thank you again. Also, cheers for adding it in the new update. To show my appreciation I also gave you a nod in the acknowledgments in our recent pub that used microViz - I hope that's okay with you. Thanks again!
from microviz.
thanks Chris! glad it worked for you and I appreciate the acknowledgement :)
from microviz.
Related Issues (20)
- comp_barplot of raw abundances HOT 2
- some GUSTAME site links have changed HOT 1
- Logical variable becomes numeric when plotting CCA HOT 2
- how can i order my samples and show their names? HOT 3
- Cannot build PCoA plot using ord_plot HOT 2
- dominant taxa in each sample HOT 2
- phylogenetic tree disappeared after tax_transform() step HOT 2
- Annotated heatmaps - error in sample_anno HOT 4
- Taxa cannot be aggregated at rank: Genus HOT 2
- cor_heatmap taxon_renamer or alternative option for labeling rows HOT 3
- comp_barplot seem to have issues with domain being unknown HOT 2
- Error in access(object, "otu_table", errorIfNULL) : otu_table slot is empty. HOT 2
- Strange behaviour when using comp_barplot with facet_grid HOT 2
- tax.fix(), and "Problematic Genus values detected in tax_table" HOT 3
- [FR] Add sankey/alluvial plots to visualize abundances HOT 2
- Tax_fix no non-unknown values HOT 1
- Labelling samples in ord_plot PCoA HOT 1
- Use of custom transofmration in linear models. HOT 1
- Can I make a correlation heatmap between taxa and categorical variable? HOT 1
- comp_barplot reduce/remove space between axes and bars
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from microviz.