0

I have a question about ordering bars in ggplot2. This is my original table, named data:

Enrichment  Term
7.078947368 Chaperonin Cpn60/TCP-1
5.309210526 ATPase, AAA-type
5.22358156  Proteasome
4.822134387 Val, Ile, Leu degradation
4.555128205 Cellular respiration
4.129591837 Carboxylic acid binding
3.814380044 NAD
3.649206349 tRNA metabolism
3.632200699 Mitochondria
3.483333333 Anion transport
3.15454932  Electron carrier activity
3.047916667 Generation of precursor metabolites/energy
-2.045706795    Chromatin modification
-2.611885283    Zinc finger, C2H2-type
-2.844052863    Zinc finger, PHD-type
-3.022150624    Chromatin remodeling complex
-3.231878254    SANT (histone-tail-binding) domain
-3.984612664    Bromodomain
-5.251256281    Development of respiratory system

I used this code:

    ggplot(data, aes(x=Term, y=Enrichment)) +  geom_bar(stat="identity", width=.5) +  coord_flip()

I would like to have the bars ordered like in the table (for the enrichment column) but it automatically orders itself by alphabet. I already tried different thing but since i am a beginner with R I could not figure it out.

Thank you very much!

1 Answers1

1

Try to use reorder(Term, -Enrichment) instead of Term in ggplot.


The complete code:

data <- read.table(text = "Enrichment  Term
7.078947368 'Chaperonin Cpn60/TCP-1'
5.309210526 'ATPase, AAA-type'
5.22358156  Proteasome
4.822134387 'Val, Ile, Leu degradation'
4.555128205 'Cellular respiration'
4.129591837 'Carboxylic acid binding'
3.814380044 NAD
3.649206349 'tRNA metabolism'
3.632200699 Mitochondria
3.483333333 'Anion transport'
3.15454932  'Electron carrier activity'
3.047916667 'Generation of precursor metabolites/energy'
-2.045706795    'Chromatin modification'
-2.611885283    'Zinc finger, C2H2-type'
-2.844052863    'Zinc finger, PHD-type'
-3.022150624    'Chromatin remodeling complex'
-3.231878254    'SANT (histone-tail-binding) domain'
-3.984612664    Bromodomain
-5.251256281    'Development of respiratory system",header = TRUE)


library(ggplot2)
ggplot(data, aes(x=reorder(Term, -Enrichment), y=Enrichment)) +  
  geom_bar(stat="identity", width=.5) +  
  coord_flip()

enter image description here

Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168