0

I am creating a stacked barplot out of a 2x2 table:

fuel.type <- c('diesel', 'gas', 'diesel', 'gas', 'diesel', 'gas', 'gas', 'gas', 'gas')   
make <- c('bmw', 'wv', 'audi', 'bmw', 'audi', 'audi', 'wv', 'wv', 'wv')    
table1 <- table(fuel.type, make)  
barplot(table1, legend = row.names(table1))

I get the following figure:

stacked barplot

How can i get the same plot but sorted in decreasing order??

s__
  • 9,270
  • 3
  • 27
  • 45

1 Answers1

3

You could sort your data, then plot them, in this way:

# here you define a table (your) that is sorted by the total of the columns
table1 <- table1[,order(colSums(-table1))] 
barplot(table1, legend = row.names(table1))

enter image description here

s__
  • 9,270
  • 3
  • 27
  • 45