Order discrete x scale by frequency/value

前端 未结 5 961
悲哀的现实
悲哀的现实 2020-11-21 10:57

I am making a dodged bar chart using ggplot with discrete x scale, the x axis are now arranged in alphabetical order, but I need to rearrange it so that it is ordered by the

5条回答
  •  粉色の甜心
    2020-11-21 11:57

    The best way for me was using vector with categories in order I need as limits parameter to scale_x_discrete. I think it is pretty simple and straightforward solution.

    ggplot(mtcars, aes(factor(cyl))) + 
      geom_bar() + 
      scale_x_discrete(limits=c(8,4,6))
    

    enter image description here

提交回复
热议问题