KasperSkytte / ampvis2

Tools for visualising microbial community amplicon data
https://kasperskytte.github.io/ampvis2/
GNU General Public License v3.0
66 stars 23 forks source link

Order of labels #81

Closed susheelbhanu closed 4 years ago

susheelbhanu commented 4 years ago

Hi, Thanks for a great package. When I plot the heatmap, the facetting is done based on a column of groups, however, the arrangement is alphabetical. Is there a way to preserve the order of samples/facet groups based on the given order?

That is to avoid alphabetical re-ordering. Here's my code:

amp_heatmap(d,
            group_by = "group",
            facet_by = "timepoint",
            tax_aggregate = "Genus",
            #tax_add = "Phylum",
            tax_show = 25,
            color_vector = c("white", "darkred"),
            plot_colorscale = "sqrt",
            plot_values = FALSE) +
  theme(axis.text.x = element_text(angle = 45, size=10, vjust = 1),
        axis.text.y = element_text(size=8),
        legend.position="right")

Thank you, Susheel

KasperSkytte commented 4 years ago

Hi Susheel

Yes ordered characters in R are called factors. So if the particular variable in your sample metadata is a character, it will be alphabetical. To set a specific order, convert it to factor with appropriate levels. That is exactly what the order_x_by argument does, but when using both group_by and facet_by it gets a little more complicated. A combined grouping variable will be created under the hood, so the values are the separated by a space " ". Try setting factor levels in your metadata first and let me know. You can do that with base R code fx: d$metadata$group <- factor(d$metadata$group, levels = c("A", "B", ...))). Using d$metadata$group <- as.factor(d$metadata$group) instead will automatically order alphabetically.

susheelbhanu commented 4 years ago

Awesome. Thanks a lot. That seems to have done the trick.