Closed emilyvansyoc closed 1 year ago
If you have only those 8 genera in your data, then you can try setting tax_order = "name"
But typically (with any taxa collapsed into other) this won't work, as comp_barplot
uses the tax_order setting to also decide which taxa to colour, not just the order.
Below is a hacky workaround, modifying a plot made from example data.
This works because the order of the bars and legend is fixed by using factor variables with a fixed order (done inside comp_barplot
) so if you know the names of those variables, you can convert them back to character, which leads to the desired alphabetical ordering.
Also, the "other" values must be replaced with NA, to ensure this category is not in the middle.
library(microViz)
library(ggplot2)
data(dietswap, package = "microbiome")
p <- dietswap %>%
ps_filter(timepoint == 1, sex == "male") %>%
comp_barplot(
tax_level = "Genus", n_taxa = 8,
sample_order = "bray", label = NULL
)
p
p$data$top %>% head()
p$data$top <- as.character(p$data$top)
p$data$top[p$data$top == "other"] <- NA
p <- p + guides(fill = guide_legend(title = "Genus", reverse = FALSE))
p
You can stop here if you only want the legend itself reordered, but you can also reorder the bars into alphabetical order if you really want
p$data$unique <- as.character(p$data$unique)
p$data$unique[p$data$unique == "other"] <- NA
p
In the future I would like to rewrite comp_barplot, to allow more free customisability like this, and because the current implementation is an overly complicated mess. I'd split it into a few pipeable functions, but I don't have the time currently.
For now, hope this helps for your issue
That’s great, thank you for the help!
Emily Van Syoc, M.S.
Integrative & Biomedical Physiology, Clinical & Translational Sciences Dual-Title PhD Program Huck Institute of Life Sciences @.*** Pronouns: she/her/hers
From: David Barnett @.> Date: Thursday, February 17, 2022 at 4:38 AM To: david-barnett/microViz @.> Cc: Van Syoc, Emily P @.>, Author @.> Subject: Re: [david-barnett/microViz] Feature request - order taxa in comp_barplot alphabetically (Issue #36)
If you have only those 8 genera in your data, then you can try setting tax_order = "name"
But typically (with any taxa collapsed into other) this won't work, as comp_barplot uses the tax_order setting to also decide which taxa to colour, not just the order.
Below is a hacky workaround, modifying a plot made from example data. This works because the order of the bars and legend is fixed by using factor variables with a fixed order (done inside comp_barplot) so if you know the names of those variables, you can convert them back to character, which leads to the desired alphabetical ordering. Also, the "other" values must be replaced with NA, to ensure this category is not in the middle.
library(microViz)
library(ggplot2)
data(dietswap, package = "microbiome")
p <- dietswap %>%
ps_filter(timepoint == 1, sex == "male") %>%
comp_barplot(
tax_level = "Genus", n_taxa = 8,
sample_order = "bray", label = NULL
)
p
p$data$top %>% head()
p$data$top <- as.character(p$data$top)
p$data$top[p$data$top == "other"] <- NA
p <- p + guides(fill = guide_legend(title = "Genus", reverse = FALSE))
p
You can stop here if you only want the legend itself reordered, but you can also reorder the bars into alphabetical order if you really want
p$data$unique <- as.character(p$data$unique)
p$data$unique[p$data$unique == "other"] <- NA
p
In the future I would like to rewrite comp_barplot, to allow more free customisability like this, and because the current implementation is an overly complicated mess. I'd split it into a few pipeable functions, but I don't have the time currently.
For now, hope this helps for your issue
— Reply to this email directly, view it on GitHubhttps://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fdavid-barnett%2FmicroViz%2Fissues%2F36%23issuecomment-1042750537&data=04%7C01%7Cvansyoc%40psu.edu%7C262dba914ebb4eae610a08d9f1f92e84%7C7cf48d453ddb4389a9c1c115526eb52e%7C0%7C0%7C637806874804911347%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=zAhyRopgjbdLvWADx2lRsuoE2YRHgUa1wGNzVpOUEwc%3D&reserved=0, or unsubscribehttps://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FAHTUHXTWC7A3FA63KKN6WX3U3S6XLANCNFSM5OTFANOQ&data=04%7C01%7Cvansyoc%40psu.edu%7C262dba914ebb4eae610a08d9f1f92e84%7C7cf48d453ddb4389a9c1c115526eb52e%7C0%7C0%7C637806874804911347%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=eEBixCxlwb8StAnCvsz543xewV9bBIhdl27t6blA%2F5o%3D&reserved=0. Triage notifications on the go with GitHub Mobile for iOShttps://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fapps.apple.com%2Fapp%2Fapple-store%2Fid1477376905%3Fct%3Dnotification-email%26mt%3D8%26pt%3D524675&data=04%7C01%7Cvansyoc%40psu.edu%7C262dba914ebb4eae610a08d9f1f92e84%7C7cf48d453ddb4389a9c1c115526eb52e%7C0%7C0%7C637806874804911347%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=3A8%2FIMKzlEuYbP%2F0OkbuC0aLTLrgYcAhBHE7x8kooYo%3D&reserved=0 or Androidhttps://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fplay.google.com%2Fstore%2Fapps%2Fdetails%3Fid%3Dcom.github.android%26referrer%3Dutm_campaign%253Dnotification-email%2526utm_medium%253Demail%2526utm_source%253Dgithub&data=04%7C01%7Cvansyoc%40psu.edu%7C262dba914ebb4eae610a08d9f1f92e84%7C7cf48d453ddb4389a9c1c115526eb52e%7C0%7C0%7C637806874804911347%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=twzVsOPN%2BkIbql1AhoRNQX9ZdoQb9t6P6mI1NP7wbFo%3D&reserved=0. You are receiving this because you authored the thread.Message ID: @.***>
Info on this topic added to the tutorial linked below, custom taxa ordering including alphabetising a selection of top taxa is now made much easier in microViz version 0.9.6
https://david-barnett.github.io/microViz/articles/web-only/compositions.html#alphabetical-top-taxa
Example of how you can now do this quite easily
library(microViz)
stopifnot(packageVersion("microViz") >= "0.9.6")
data("shao19")
nTaxa <- 7
# regular sorting, not alphabetical
shao19 %>%
ps_filter(family_role == "mother", age > 40) %>%
comp_barplot(
tax_level = "genus", n_taxa = nTaxa,
merge_other = FALSE, other_name = "Other",
label = NULL
) +
ggplot2::ggtitle("Regular taxa sorting")
# set up for alphabetical sorting
topTaxa <- shao19 %>%
ps_filter(family_role == "mother", age > 40) %>%
tax_top(n = nTaxa, rank = "genus") %>%
sort() # this makes them alphabetical
# plot with alphabetical sorting
shao19 %>%
ps_filter(family_role == "mother", age > 40) %>%
tax_sort(by = sum, at = "genus", tree_warn = FALSE) %>% # this orders all genera by abundance
comp_barplot(
tax_order = topTaxa, # this brings the named taxa to the front
tax_level = "genus", n_taxa = nTaxa,
merge_other = FALSE, other_name = "Other",
label = NULL
) +
ggplot2::ggtitle("Alphabetical (top) taxa sorting")
Hello again - The default behavior of a ggplot2 barplot on melted OTU table is to order the taxa in the legend alphabetically, which (IMO) makes this much easier to read. Can this be added as an option in the comp_barplot, or is it possible to add as a ggplot2 layer? Thanks!
Example of ordered plot in ggplot2 with alphabetically ordered taxa in the legend-![image](https://user-images.githubusercontent.com/31933406/154380122-2532fa00-34a6-466e-8374-47a37dffc92d.png)
Example of default comp_barplot -![image](https://user-images.githubusercontent.com/31933406/154380299-026e1b29-fdc5-46e4-9cfb-fdaae6fe2af0.png)