1

I am attempting to build a chart for some LDA scores I have generated from bacterial abundances.

Here an example of the data:

Taxa <- c('Bacilli', 'Firmicutes', 'Slackia', 'Clostridium')
Level <- c('Class', 'Phylum', 'Genus', 'Genus')
Status <- c('Patient', 'Patient', 'Control', 'Control')
LDA.score <- c(3.5,2.0,-1,-3)
Example <- data.frame(Taxa, Level, Status, LDA.score)

I use this code to make the chart:

ggplot(data=Example, aes(x=Taxa, y=LDA.score, fill=Status)) + geom_bar(stat="identity", position="identity") + coord_flip()

I'd like the bars to be in numerical order so that the bars are grouped into control and patient. However, the resulting bar chart is in alphabetical order according to the x axis.

I have attempted to use reorder() but this doesn't seem to work.

Any help would be appreciated.

Fiona
  • 53
  • 7
  • 1
    `reorder()` works for me: `ggplot(data=Example, aes(x=reorder(Taxa, LDA.score), y=LDA.score, fill=Status)) + geom_bar(stat="identity", position="identity") + coord_flip()` – Pablo Rod Aug 02 '19 at 13:58
  • It suddenly works with `reorder()`. I was trying to apply reorder to the yaxis. Would that explain why it wasn't working? – Fiona Aug 02 '19 at 14:04
  • 1
    Yes---that explains why it wasn't working. Next time, if you show your attempts (e.g., with `reorder()` in this case), it can often help us help you much faster. – Gregor Thomas Aug 02 '19 at 14:05

1 Answers1

1

We could convert the 'Taxa' to factor based on the order of 'LDA.score' and then use that in ggplot

library(dplyr)
library(ggplot2)
Example %>% 
   mutate(Taxa = factor(Taxa, levels = as.character(Taxa)[order(LDA.score)])) %>% 
   ggplot(., aes(x=Taxa, y=LDA.score, fill=Status)) + 
     geom_bar(stat="identity", position="identity") + 
     coord_flip()

-output

enter image description here

akrun
  • 874,273
  • 37
  • 540
  • 662
  • 1
    It works on the example data but I am getting an error with my real data. The real dataset is too large to post on here. This is the error `Error in `levels<-`(`*tmp*`, value = as.character(levels)) : factor level [2] is duplicated` – Fiona Aug 02 '19 at 13:59
  • @Fiona. Okay, I understand the issue here. You may need to get the `mean` by group in `summarise` before doing the `factor` or uses that in `ggplot` `stat_summary` – akrun Aug 02 '19 at 14:02
  • 2
    ...or just use `reorder`, which uses `mean` by default. `reorder(Taxa, LDA.score)` – Gregor Thomas Aug 02 '19 at 14:03