0

I am trying to graph my phyloseq/deseq2 data output (Log2FoldChange on y and Species name on x) in ggplot and I would like to be able to order them by autotrophs and heterotrophs as well as max-min value. I have a column I added into the Taxa file named "Trophic".

Taxa file snippet

The pipeline turns the phyloseq object into a deseq object, and that is where Log2FoldChange comes in.

deseq output snippet

Then they're combined, which is what I am working with. (Here is the tutorial if you need more)

I have this code, which I think orders the species by highest to lowest Log2FoldChange and comes out with a graph like this.

x = tapply(sigtab$log2FoldChange, sigtab$Species, function(x) max(x)) 
x = sort(x, TRUE)
sigtab$Species = factor(as.character(sigtab$Species), levels=names(x)

However, I am not exactly sure how to modify this in order to have them sorted first by trophic status and then by species and then plotted as such.

I would basically like to be able to have two "sides" of the same graph (auto then hetero), both ordered from max value to min value of Log2FoldChange.

Could anyone please help me figure this out?

Pika Supports Ukraine
  • 3,612
  • 10
  • 26
  • 42
  • You can arrange in order with `dplyr::arrange()` and you can specify whether you want it in desc order otherwise it will be ascending by default. `arrange(data, first, desc(second))` etc. – Croote Mar 07 '19 at 22:26
  • Oh marvelous! So that worked and I was able to correctly order my actual data frame, but when I plot it the species names are still in alphabetical rather than being in the order I just put them in. Is there a special thing I would need to add in order to keep it like that (as in non-alphabetical) for the graph? – Megan Ladds Mar 08 '19 at 03:34
  • [See Here for more discussion on reordering factors for plotting](https://stackoverflow.com/questions/3253641/change-the-order-of-a-discrete-x-scale) – Croote Mar 08 '19 at 06:00
  • 1
    Thank you so much for your help. I was able to fix the graph using this `sigtab<-arrange (sigtab, Trophic, desc(log2FoldChange)) sigtab$Species <- factor(sigtab$Species, levels = sigtab$Species)` – Megan Ladds Mar 11 '19 at 18:58

0 Answers0