Reorder geom_bar or anything else in ggplot by the value of your choosing

You still find people on StackOverflow counseling to redefine the factors of a data.frame to reorder elements of a ggplot graphic. In 2020, this is crazy. Who still uses factors, anyways? (And who still uses data.frames when you have data.table?)

Anyways, if you want to reorder geom_bar or anything else in ggplot by the value of your choosing, there is a one-word solution, called “reorder”. You can use this function when defining the x value in the aes() parameters. Example:

ggplot(data) +  geom_bar(aes(x=reorder(feature,frequency),y=frequency),stat="identity")

Find the only valuable example on StackOverflow regarding this here.




  •  
  •  
  •  
  •  
  •  

Leave a comment

Your email address will not be published. Required fields are marked *