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
99 stars 10 forks source link

Feature request - order taxa in comp_barplot alphabetically #36

Closed emilyvansyoc closed 1 year ago

emilyvansyoc commented 2 years ago

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

Example of default comp_barplot - image

david-barnett commented 2 years 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

image

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 

image

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

image

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

emilyvansyoc commented 2 years ago

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

[image]https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fuser-images.githubusercontent.com%2F33695702%2F154447633-0bbd3f87-618d-4f73-8937-ce9b5ff6c64f.png&data=04%7C01%7Cvansyoc%40psu.edu%7C262dba914ebb4eae610a08d9f1f92e84%7C7cf48d453ddb4389a9c1c115526eb52e%7C0%7C0%7C637806874804911347%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=OoZ%2F%2FrJ7pCBYgOwazm1ZkOGAPOalVmyZI%2BgABVQjLUM%3D&reserved=0

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

[image]https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fuser-images.githubusercontent.com%2F33695702%2F154447675-1d8e79f4-92b0-4aad-9af8-8116e36c06b8.png&data=04%7C01%7Cvansyoc%40psu.edu%7C262dba914ebb4eae610a08d9f1f92e84%7C7cf48d453ddb4389a9c1c115526eb52e%7C0%7C0%7C637806874804911347%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=Vi%2Flgt%2BEyLsi9USjia7uLkBNTnmlBMKTCFhpXog4Pd0%3D&reserved=0

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

[image]https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fuser-images.githubusercontent.com%2F33695702%2F154447698-ffdcf248-9a96-4ae3-85ea-4175ab80bc75.png&data=04%7C01%7Cvansyoc%40psu.edu%7C262dba914ebb4eae610a08d9f1f92e84%7C7cf48d453ddb4389a9c1c115526eb52e%7C0%7C0%7C637806874804911347%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=oNAqpspB9gDIHP1FIePUMCz%2FL7OfFkX0yPXpv4UpspY%3D&reserved=0

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: @.***>

david-barnett commented 1 year ago

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

david-barnett commented 1 year ago

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")

image image