Reorder ggplot2 legend

Hi,

I want to reorder the legend to have "Other" at the end as shown in the pic
I used the following command

Fam <- physeq6 %>% plot_composition(average_by = "TREATMENT") + scale_y_continuous(labels = percent) +
scale_x_discrete(limits=c("Day1","Day5", "Day10", "Day15")) +
ggtitle("") +
scale_fill_brewer("Phylum", palette = "Paired") + theme_bw()

but the legend order came as alphabetical order

the data input is from a phyloseq object,

kindly, is there any solution for this issue with ggplot2?

Thank you

You need to reorder the factor levels of the column that contains those labels. Here is an example of reordering the factor levels of a data frame column using the fct_relevel() function from the forcats package. The levels start out in alphabetical order.

DF <- data.frame(ID = 1:4, FAC = factor(c("E","O","V","A")))
DF$FAC #levels are alphbetical
#> [1] E O V A
#> Levels: A E O V
DF$FAC <- forcats::fct_relevel(DF$FAC, "O", after = Inf)
DF$FAC  #O is now at the end of the levels
#> [1] E O V A
#> Levels: A E V O

Created on 2024-07-16 with reprex v2.0.2

1 Like

Blockquote

Hi @FJCC

I appreciate your reply

if this the my legend factor

FAM = factor(c("Acidaminococcaceae", "Bacteroidaceae", "Enterobacteriaceae",
"Lachnospiraceae", "Oscillospiraceae", "Ruminococcaceae", "Sutterellaceae",
"Synergistaceae", "Tannerellaceae", "Veillonellaceae", "Other"))

how to apply it with the command provided

Fam <- physeq6 %>% plot_composition(average_by = "TREATMENT") + scale_y_continuous(labels = percent) +
scale_x_discrete(limits=c("Day1","Day5", "Day10", "Day15")) +
ggtitle("") +
scale_fill_brewer("Family", palette = "Paired") + theme_bw().

Sorry, I know I should provide reproducible data, but I can't because it is a phyloseq object. I'm using ggplot2 a lot and I can reorder as you mentioned but this does not work with me when using data of a phyloseq.

thanking your help

Looking at the documentation of the plot_composition() function, I see the parameters sample.sort and otu.sort. The description of sample.sort includes

sample.sort 	

Order samples. Various criteria are available:

    NULL or 'none': No sorting

    A single character string: indicate the metadata field to be used for ordering. Or: if this string 
is found from the tax_table, then sort by the corresponding taxonomic group.

    A character vector: sample IDs indicating the sample ordering.

    'neatmap' Order samples based on the neatmap approach. See neatsort. By default, 'NMDS' method 
with 'bray' distance is used. For other options, arrange the samples manually with the function.

The description of otu.sort says

Order taxa. Same options as for the sample.sort argument but instead of metadata, taxonomic table is 
used. Also possible to sort by 'abundance'.

What happens if you set either sample.sort or otu.sort to equal c("Acidaminococcaceae", "Bacteroidaceae", "Enterobacteriaceae", "Lachnospiraceae", "Oscillospiraceae", "Ruminococcaceae", "Sutterellaceae", "Synergistaceae", "Tannerellaceae", "Veillonellaceae", "Other")

Obviously I'm guessing, having never used this function or worked with such data.

You can see the help text for the function by entering ?plot_composition in the console or you can read it at
https://rdrr.io/github/microbiome/microbiome/man/plot_composition.html

1 Like

Thanks @FJCC
I will try it

I'm grateful for your help

Could you run:

glimpse(physeq6)

and post the output here, between a pair of triple backticks, like this?

```
paste output
```

Hi @dromano

Formal class 'phyloseq' [package "phyloseq"] with 5 slots
  ..@ otu_table:Formal class 'otu_table' [package "phyloseq"] with 2 slots
  ..@ tax_table:Formal class 'taxonomyTable' [package "phyloseq"] with 1 slot
  ..@ sam_data :'data.frame':	4 obs. of  11 variables:
Formal class 'sample_data' [package "phyloseq"] with 4 slots
  ..@ phy_tree : NULL
  ..@ refseq   : NULL

Thanks, @Galal — now could do the same with the output you get from running this?

dput(physeq6@sam_data)

sorry for the late response @dromano

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.