0

I have a table showing genes, with numbers. I made a bar chart showing each gene with it's number but I'm failing to do so in an ordered way.

This is the code :

TOPGENES = data.frame(Gene.Name = c('ABCD2','ZC3H12D','THEMIS','RASAL3','LAX1','TMC8','SLAMF6','SAMD3', 
                                   'PPP1R16A','POLB','NUGGC','NLRC3','MFAP1','LCP2','CCR4','CD52','KCNA3','IL21R','FHL1',
                                   'EVI2B','ZNF831','ZDHHC9','WIPF1','WAS','UROD','TRAT1','TNFSF8',
                                   'TCF4','IL6','TMEM156','JAK1','CD274','TP53I13','BRAF',
                                   'CXCR3','CXCR6','CXCL13','CXCL10','CXCL9','FGFR3','KRAS','CDK4'),
                      Accurance = c(8,8,8,8,8,8,8,7, 
                                    
                                    7,7,7,7,7,7,7,7,7,7,7,
                                    
                                    7,6,6,6,6,6,6,6,
                                    
                                    6,6,6,6,4,4,4,4,3,3,3,2,2,2,2))

theTable <- within(TOPGENES, 
                   Gene.Name <- factor(Gene.Name, 
                                      levels=names(sort(table(Gene.Name), 
                                                        decreasing=TRUE))))

ggplot(data=theTable, aes(x=Gene.Name, y=Accurance)) +
  geom_bar(stat="identity", width = 0.7 , fill = 'steelblue') +  theme(axis.text.x = element_text(angle = 70, vjust = 0.8, hjust=0.8))

The plot it produces:

enter image description here

How can I order it in a decreasing manner?

Programming Noob
  • 1,232
  • 3
  • 14

1 Answers1

0

You can use reorder like this:

library(ggplot2)
ggplot(data=theTable, aes(x=reorder(Gene.Name,-Accurance), y=Accurance)) +
  geom_bar(stat="identity", width = 0.7 , fill = 'steelblue') +  
  theme(axis.text.x = element_text(angle = 70, vjust = 0.8, hjust=0.8)) +
  labs(x = "Gene.Name")

ggplot(data=theTable, aes(x=reorder(Gene.Name,Accurance), y=Accurance)) +
  geom_bar(stat="identity", width = 0.7 , fill = 'steelblue') +  
  theme(axis.text.x = element_text(angle = 70, vjust = 0.8, hjust=0.8)) +
  labs(x = "Gene.Name")

Created on 2022-09-03 with reprex v2.0.2

Quinten
  • 35,235
  • 5
  • 20
  • 53